

{"id":17529,"date":"2018-06-05T04:00:58","date_gmt":"2018-06-04T22:30:58","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=17529"},"modified":"2026-04-24T17:36:53","modified_gmt":"2026-04-24T12:06:53","slug":"image-processing-with-scipy-and-numpy","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/image-processing-with-scipy-and-numpy\/","title":{"rendered":"Image Processing with SciPy and NumPy in Python"},"content":{"rendered":"<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:149,&quot;href&quot;:&quot;https:\\\/\\\/www.python.org&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20251206090101\\\/https:\\\/\\\/www.python.org\\\/&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-06 12:20:59&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-09 12:44:48&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-12 13:49:48&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-15 14:13:48&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-18 15:26:07&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-21 17:05:18&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-24 19:33:20&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-28 02:44:18&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-31 04:43:13&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-03 07:01:16&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-06 07:15:14&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-09 07:16:21&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-12 10:01:16&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-15 10:07:06&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-18 10:11:43&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-21 10:20:21&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-24 10:47:21&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-27 10:58:10&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-30 10:59:29&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-02 12:28:37&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-05 13:05:41&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-08 15:11:10&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-11 15:46:29&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-14 17:21:34&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-17 18:37:27&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-20 18:52:05&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-23 19:52:29&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-27 01:02:50&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-02 03:50:52&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-05 05:18:10&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-08 06:18:52&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-11 07:24:15&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-14 08:33:37&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-17 08:58:17&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-20 12:26:41&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-23 14:32:34&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-26 16:21:46&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-29 17:22:50&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-01 18:18:54&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-04 18:27:04&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-08 02:33:55&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-11 04:53:57&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-14 06:48:30&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-17 07:17:55&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-20 07:32:43&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-23 09:34:41&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-26 10:13:17&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-29 10:35:31&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-02 11:50:34&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-05 12:07:03&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-08 13:08:24&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-11 14:46:17&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-14 21:24:09&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-05-18 03:08:37&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-21 06:27:39&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-24 07:06:36&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-27 07:30:50&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-30 08:47:47&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-02 09:37:18&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-05 09:43:29&quot;,&quot;http_code&quot;:206}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-06-05 09:43:29&quot;,&quot;http_code&quot;:206},&quot;process&quot;:&quot;done&quot;}]'><\/div>\n<p>In this <strong><a href=\"https:\/\/data-flair.training\/blogs\/python-tutorial-for-beginners\/\">Python tutorial<\/a><\/strong>, we will use Image Processing with SciPy and NumPy. We will deal with reading and writing to an image and displaying the image. We will cover different manipulations and filtering images in Python. Along with this, we will discuss extracting features.<br \/>\nSo, let&#8217;s discuss\u00a0Image Processing with SciPy and NumPy.<\/p>\n<div id=\"attachment_17545\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Image-Processing-with-SciPy-and-NumPy-01.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-17545\" class=\"wp-image-17545 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Image-Processing-with-SciPy-and-NumPy-01.jpg\" alt=\"Image Processing with SciPy and NumPy\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Image-Processing-with-SciPy-and-NumPy-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Image-Processing-with-SciPy-and-NumPy-01-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Image-Processing-with-SciPy-and-NumPy-01-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Image-Processing-with-SciPy-and-NumPy-01-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Image-Processing-with-SciPy-and-NumPy-01-1024x536.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-17545\" class=\"wp-caption-text\">Image Processing with SciPy and NumPy<\/p><\/div>\n<h3>Prerequisite for Image Processing with SciPy and NumPy<\/h3>\n<p>For image processing with SciPy and NumPy, you will need the libraries for this tutorial. We checked in the command prompt whether we already have these:<br \/>\n<strong><a href=\"https:\/\/data-flair.training\/blogs\/range-function-in-python\/\">Let&#8217;s Revise Range Function in Python \u2013 Range() in Python<\/a><\/strong><\/p>\n<pre class=\"EnlighterJSRAW\">C:\\Users\\lifei&gt;pip show scipy<\/pre>\n<p>Name: scipy<br \/>\nVersion: 1.0.0<br \/>\nSummary: SciPy: Scientific Library for Python<br \/>\nHome-page: https:\/\/www.scipy.org<br \/>\nAuthor: SciPy Developers<br \/>\nAuthor-email: scipy-dev@python.org<br \/>\nLicense: BSD<br \/>\nLocation: c:\\users\\lifei\\appdata\\local\\programs\\python\\python36-32\\lib\\site-packages<br \/>\nRequires: numpy<br \/>\nRequired-by:<\/p>\n<pre class=\"EnlighterJSRAW\">C:\\Users\\lifei&gt;pip show numpyName: numpy<\/pre>\n<p>Version: 1.14.1<br \/>\nSummary: NumPy: array processing for numbers, strings, records, and objects.<br \/>\nHome-page: http:\/\/www.numpy.org<br \/>\nAuthor: NumPy Developers<br \/>\nAuthor-email: numpy-discussion@python.org<br \/>\nLicense: BSD<br \/>\nLocation: c:\\users\\lifei\\appdata\\local\\programs\\python\\python36-32\\lib\\site-packages<br \/>\nRequires:<br \/>\nRequired-by: scipy, pandas, matplotlib<br \/>\nIf you don\u2019t already have them installed, use the following commands:<\/p>\n<pre class=\"EnlighterJSRAW\">pip install scipy<\/pre>\n<pre class=\"EnlighterJSRAW\">pip install numpy<\/pre>\n<p>Also, some methods like imsave() did not show up with scipy in our interpreter. So, we used pip to install Pillow. Works for us.<\/p>\n<pre class=\"EnlighterJSRAW\">C:\\Users\\lifei&gt;pip install Pillow<\/pre>\n<p>Collecting Pillow<br \/>\nDownloading https:\/\/files.pythonhosted.org\/packages\/bc\/6d\/40ad1421a015c3710b329e99842ece044492cc5ff51159b5b23558000aad\/Pillow-5.1.0-cp36-cp36m-win32.whl (1.4MB)<br \/>\n100% |<span style=\"font-family: Arial, serif\">\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588<\/span>| 1.4MB 499kB\/s<br \/>\nInstalling collected packages: Pillow<br \/>\nSuccessfully installed Pillow-5.1.0<\/p>\n<pre class=\"EnlighterJSRAW\">C:\\Users\\lifei&gt;pip install matplotlib<\/pre>\n<p>Collecting matplotlib<br \/>\nDownloading https:\/\/files.pythonhosted.org\/packages\/c6\/e0\/1f98872856dcbd042220a142b5a2b4cb9a8f0efe959ae20f6cb73bd9f3e6\/matplotlib-2.2.2-cp36-cp36m-win32.whl (8.5MB)<br \/>\n100% |<span style=\"font-family: Arial, serif\">\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588<\/span>| 8.5MB 448kB\/s<br \/>\nRequirement already satisfied: numpy&gt;=1.7.1 in c:\\users\\lifei\\appdata\\local\\programs\\python\\python36-32\\lib\\site-packages (from matplotlib) (1.14.1)<br \/>\nCollecting pyparsing!=2.0.4,!=2.1.2,!=2.1.6,&gt;=2.0.1 (from matplotlib)<br \/>\nDownloading https:\/\/files.pythonhosted.org\/packages\/6a\/8a\/718fd7d3458f9fab8e67186b00abdd345b639976bc7fb3ae722e1b026a50\/pyparsing-2.2.0-py2.py3-none-any.whl (56kB)<br \/>\n100% |<span style=\"font-family: Arial, serif\">\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588<\/span>| 61kB 222kB\/s<br \/>\nCollecting cycler&gt;=0.10 (from matplotlib)<br \/>\nDownloading https:\/\/files.pythonhosted.org\/packages\/f7\/d2\/e07d3ebb2bd7af696440ce7e754c59dd546ffe1bbe732c8ab68b9c834e61\/cycler-0.10.0-py2.py3-none-any.whl<br \/>\nRequirement already satisfied: pytz in c:\\users\\lifei\\appdata\\local\\programs\\python\\python36-32\\lib\\site-packages (from matplotlib) (2018.3)<br \/>\nCollecting kiwisolver&gt;=1.0.1 (from matplotlib)<br \/>\nDownloading https:\/\/files.pythonhosted.org\/packages\/fd\/59\/8742e2c77c852e09f0d409af42ccc4165120943ba3b52d57a3ddc56cb0ca\/kiwisolver-1.0.1-cp36-none-win32.whl (44kB)<br \/>\n100% |<span style=\"font-family: Arial, serif\">\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588<\/span>| 51kB 610kB\/s<br \/>\nRequirement already satisfied: six&gt;=1.10 in c:\\users\\lifei\\appdata\\local\\programs\\python\\python36-32\\lib\\site-packages (from matplotlib) (1.11.0)<br \/>\nRequirement already satisfied: python-dateutil&gt;=2.1 in c:\\users\\lifei\\appdata\\local\\programs\\python\\python36-32\\lib\\site-packages (from matplotlib) (2.7.2)<br \/>\nRequirement already satisfied: setuptools in c:\\users\\lifei\\appdata\\local\\programs\\python\\python36-32\\lib\\site-packages (from kiwisolver&gt;=1.0.1-&gt;matplotlib) (39.2.0)<br \/>\nInstalling collected packages: pyparsing, cycler, kiwisolver, matplotlib<br \/>\nSuccessfully installed cycler-0.10.0 kiwisolver-1.0.1 matplotlib-2.2.2 pyparsing-2.2.0<br \/>\nLet\u2019s get to the Desktop:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import os\r\n&gt;&gt;&gt; os.chdir('C:\\\\Users\\\\lifei\\\\Desktop')<\/pre>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/methods-in-python-programming\/\">Read About Methods in Python \u2013 Classes, Objects and Functions in Python<\/a><\/strong><\/p>\n<h3 class=\"western\">How to Read and Write to an Image in Python?<\/h3>\n<p>Before we start image processing with SciPy and NumPy, we must be able to open them. This is how we open it:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from scipy import misc\r\n&gt;&gt;&gt; f=misc.face()\r\n&gt;&gt;&gt; misc.imsave('demo.png',f)<\/pre>\n<div id=\"attachment_17546\" style=\"width: 154px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/1-10.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-17546\" class=\"wp-image-17546 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/1-10.png\" alt=\"Image Processing with SciPy and NumPy\" width=\"144\" height=\"121\" \/><\/a><p id=\"caption-attachment-17546\" class=\"wp-caption-text\">Image Processing with SciPy and NumPy<\/p><\/div>\n<p>imsave needs you to have the library PIL installed in your system. It lets you save an array as an image. This creates an image on our Desktop.<\/p>\n<p>Now, we import pyplot from matplotlib.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import matplotlib.pyplot as plt\r\n&gt;&gt;&gt; plt.imshow(f)\r\n&lt;matplotlib.image.AxesImage object at 0x0864E050&gt;\r\n&gt;&gt;&gt; plt.show()<\/pre>\n<p>This makes the following image of a raccoon face show up:<\/p>\n<div id=\"attachment_17892\" style=\"width: 985px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/2-8.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-17892\" class=\"wp-image-17892 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/2-8.png\" alt=\"Image Processing with SciPy and NumPy - Reading and Writing to Images\" width=\"975\" height=\"822\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/2-8.png 975w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/2-8-150x126.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/2-8-300x253.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/2-8-768x647.png 768w\" sizes=\"auto, (max-width: 975px) 100vw, 975px\" \/><\/a><p id=\"caption-attachment-17892\" class=\"wp-caption-text\">Image Processing with SciPy and NumPy &#8211; Reading and Writing to Images<\/p><\/div>\n<p><strong style=\"font-family: Verdana, Geneva, sans-serif\"><a href=\"https:\/\/data-flair.training\/blogs\/python-sets-and-booleans-with-syntax-and-examples\/\">Read About Python Sets and Booleans with Syntax and Examples<\/a><\/strong><\/p>\n<h4 class=\"western\">a. Creating a Python numpy array<\/h4>\n<p>Continuing where we left:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; f=misc.imread('demo.png')\r\n&gt;&gt;&gt; type(f)\r\n&lt;class 'numpy.ndarray'&gt;\r\n&gt;&gt;&gt; f.shape #This gives us the size of the array. The image is 1024x768.\r\n(768, 1024, 3)\r\n&gt;&gt;&gt; f.dtype\r\ndtype('uint8') #This means it is an 8ibit image (0-255)<\/pre>\n<h4 class=\"western\">b. Opening a RAW file in Python<\/h4>\n<p>A RAW file is one with the extension .raw. It is a camera image file holding minimally processed data from an image sensor. The sensor can be of a digital camera, a motion picture film scanner, or an image scanner.<br \/>\nLet\u2019s first create such a file.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; f.tofile('demo.raw')<\/pre>\n<div id=\"attachment_17548\" style=\"width: 103px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/3-6.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-17548\" class=\"wp-image-17548 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/3-6.png\" alt=\"Image Processing with SciPy and NumPy\" width=\"93\" height=\"102\" \/><\/a><p id=\"caption-attachment-17548\" class=\"wp-caption-text\">Open a raw file<\/p><\/div>\n<p>This creates the following icon on the Desktop:<\/p>\n<p>Now, we import another module- np.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import np\r\n&gt;&gt;&gt; fromraw=np.fromfile('demo.raw',dtype=np.uint8)\r\n&gt;&gt;&gt; fromraw.shape\r\n(2359296,)\r\n&gt;&gt;&gt; fromraw.shape=(768, 1024, 3)<\/pre>\n<p>For much larger data, we can use memory mapping:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; memmap=np.memmap('demo.raw',dtype=np.uint8,shape=(768,1024,3))<\/pre>\n<p>This reads data from the file but does not load it into memory.<br \/>\n<strong><a href=\"https:\/\/data-flair.training\/blogs\/python-date-and-time\/\">Let&#8217;s Read About Python Date and Time \u2013 Syntax and examples<\/a><\/strong><\/p>\n<h4 class=\"western\">c. Working on more than one image at once in Python<\/h4>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; for i in range(7):\r\nim=np.random.randint(0,256,10000).reshape((100,100))\r\nmisc.imsave('random_%02d.png'%i,im)\r\n&gt;&gt;&gt; from glob import glob\r\n&gt;&gt;&gt; filelist=glob('random*.png')\r\n&gt;&gt;&gt; filelist.sort()<\/pre>\n<p>Get to your Desktop to find the following image files:<\/p>\n<div id=\"attachment_17549\" style=\"width: 244px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/4-7.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-17549\" class=\"wp-image-17549 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/4-7.png\" alt=\"Image Processing with SciPy and NumPy\" width=\"234\" height=\"914\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/4-7.png 234w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/4-7-77x300.png 77w\" sizes=\"auto, (max-width: 234px) 100vw, 234px\" \/><\/a><p id=\"caption-attachment-17549\" class=\"wp-caption-text\">SciPy and NumPy<\/p><\/div>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/python-strings\/\">Let&#8217;s Learn Python Strings with String Functions and String Operations<\/a><\/strong><\/p>\n<h3 class=\"western\">How to Display an Image in Python?<\/h3>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; f1=misc.face(gray=True) #For a grayscale image\r\n&gt;&gt;&gt; plt.imshow(f1,cmap=plt.cm.gray)\r\n&lt;matplotlib.image.AxesImage object at 0x0919E810&gt;<\/pre>\n<p>We can use min and max values to increase contrast:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; plt.imshow(f1,cmap=plt.cm.gray,vmin=30,vmax=200)\r\n&lt;matplotlib.image.AxesImage object at 0x0919E9B0&gt;\r\n&gt;&gt;&gt; plt.axis('off') #This removes axes and ticks\r\n(-0.5, 1023.5, 767.5, -0.5)<\/pre>\n<h4 class=\"western\">a. Drawing Contour Lines using\u00a0Python<\/h4>\n<p>We call the contour() method to draw contour lines on the image.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; plt.contour(f1,[50,200])\r\n&lt;matplotlib.contour.QuadContourSet object at 0x019954F0&gt;<\/pre>\n<div id=\"attachment_17893\" style=\"width: 1010px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/contour-1.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-17893\" class=\"wp-image-17893 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/contour-1.png\" alt=\"Image Processing with SciPy and NumPy - Drawing Contour Lines\" width=\"1000\" height=\"276\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/contour-1.png 1000w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/contour-1-150x41.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/contour-1-300x83.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/contour-1-768x212.png 768w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/a><p id=\"caption-attachment-17893\" class=\"wp-caption-text\">Image Processing with SciPy and NumPy &#8211; Drawing Contour Lines<\/p><\/div>\n<h4 class=\"western\">b. Interpolation using\u00a0Python<\/h4>\n<p>Adjusting the \u2018interpolation\u2019 argument, we can adjust the intensity variation. By setting it to \u2018bilinear\u2019, we get smooth intensity variations, and by setting it to \u2018nearest\u2019, we get a fine inspection of intensity variations.<br \/>\n<strong><a href=\"https:\/\/data-flair.training\/blogs\/python-decision-making-expressions\/\">Do You Know About Python Decision Making Statements with Syntax and Examples<\/a><\/strong><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; plt.imshow(f1[320:340,510:530],cmap=plt.cm.gray,interpolation='bilinear')\r\n&lt;matplotlib.image.AxesImage object at 0x01F2FB90&gt;\r\n&gt;&gt;&gt; plt.imshow(f1[320:340,510:530],cmap=plt.cm.gray,interpolation='nearest')\r\n&lt;matplotlib.image.AxesImage object at 0x019954B0&gt;<\/pre>\n<div id=\"attachment_17551\" style=\"width: 810px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/interpolation.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-17551\" class=\"wp-image-17551 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/interpolation.png\" alt=\"Image Processing with SciPy and NumPy\" width=\"800\" height=\"400\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/interpolation.png 800w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/interpolation-150x75.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/interpolation-300x150.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/interpolation-768x384.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/a><p id=\"caption-attachment-17551\" class=\"wp-caption-text\">Image Processing with SciPy and NumPy &#8211;\u00a0Interpolation<\/p><\/div>\n<h3 class=\"western\">Basic Manipulations for Images in Python<\/h3>\n<p>Let\u2019s look at images as arrays and use numpy to handle them.<\/p>\n<div id=\"attachment_17553\" style=\"width: 675px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/5-5.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-17553\" class=\"wp-image-17553 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/5-5.png\" alt=\"Image Processing with SciPy and NumPy\" width=\"665\" height=\"341\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/5-5.png 665w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/5-5-150x77.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/5-5-300x154.png 300w\" sizes=\"auto, (max-width: 665px) 100vw, 665px\" \/><\/a><p id=\"caption-attachment-17553\" class=\"wp-caption-text\">Image Processing with SciPy and NumPy &#8211;\u00a0Manipulations for Images<\/p><\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; face=misc.face(gray=True)\r\n&gt;&gt;&gt; face[0,40]\r\n127\r\n&gt;&gt;&gt; face[10:13,20:23]\r\narray([[141, 153, 145],\r\n[133, 134, 125],\r\n[ 96, 92, 94]], dtype=uint8)\r\n&gt;&gt;&gt; face[100:120]=255\r\n&gt;&gt;&gt; lx,ly=face.shape\r\n&gt;&gt;&gt; X,Y=np.ogrid[0:lx,0:ly]\r\n&gt;&gt;&gt; mask=(X-lx\/2)**2+(Y-ly\/2)**2&gt;lx*ly\/4\r\n&gt;&gt;&gt; face[mask]=0\r\n&gt;&gt;&gt; face[range(400),range(400)]=255<\/pre>\n<p>This gives us the following result:<\/p>\n<div id=\"attachment_17891\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/6-4.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-17891\" class=\"wp-image-17891 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/6-4.png\" alt=\"Image Processing with SciPy and NumPy\" width=\"300\" height=\"223\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/6-4.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/6-4-150x112.png 150w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-17891\" class=\"wp-caption-text\">Image Processing with SciPy and NumPy<\/p><\/div>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/python-generators\/\">Have a Look at\u00a0Python Generators and Generator Expressions in Detail<\/a><\/strong><\/p>\n<h4 class=\"western\">a. Statistical Information using\u00a0Python<\/h4>\n<p>We can use the max() and min()functions to return the maximum and minimum along a given axis. The function mean() returns the average of the array elements along the given axis.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; face=misc.face(gray=True)\r\n&gt;&gt;&gt; face.mean()\r\n113.48026784261067\r\n&gt;&gt;&gt; face.max()\r\n250\r\n&gt;&gt;&gt; face.min()\r\n0<\/pre>\n<h4 class=\"western\">b. Geometrical Transformations using\u00a0Python<\/h4>\n<p>We can rotate, crop, and flip an image using scipy.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; face=misc.face(gray=True)\r\n&gt;&gt;&gt; lx,ly=face.shape\r\n&gt;&gt;&gt; crop=face[lx\/\/4:-lx\/\/4,ly\/\/4:-ly\/\/4]\r\n&gt;&gt;&gt; flip=np.flipud(face)\r\n&gt;&gt;&gt; from scipy import ndimage\r\n&gt;&gt;&gt; rotate=ndimage.rotate(face,45)\r\n&gt;&gt;&gt; rotate_noreshape=ndimage.rotate(face,45,reshape=False)<\/pre>\n<div id=\"attachment_17555\" style=\"width: 1260px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/7.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-17555\" class=\"wp-image-17555 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/7.png\" alt=\"Image Processing with SciPy and NumPy\" width=\"1250\" height=\"250\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/7.png 1250w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/7-150x30.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/7-300x60.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/7-768x154.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/7-1024x205.png 1024w\" sizes=\"auto, (max-width: 1250px) 100vw, 1250px\" \/><\/a><p id=\"caption-attachment-17555\" class=\"wp-caption-text\">Image Processing with SciPy and NumPy &#8211;\u00a0Geometrical Transformations<\/p><\/div>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/errors-and-exceptions-in-python\/\">Read More About Errors and Exceptions in Python Programming<\/a><\/strong><\/p>\n<h3 class=\"western\">How to Filter an Image in Python?<\/h3>\n<p>We can also blur\/smooth, sharpen, and denoise images. Through local filters, we can use a function of the values of the neighboring pixels to replace the value of a pixel.<\/p>\n<div id=\"attachment_17556\" style=\"width: 854px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/8-1.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-17556\" class=\"wp-image-17556 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/8-1.png\" alt=\"Image Processing with SciPy and NumPy\" width=\"844\" height=\"276\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/8-1.png 844w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/8-1-150x49.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/8-1-300x98.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/8-1-768x251.png 768w\" sizes=\"auto, (max-width: 844px) 100vw, 844px\" \/><\/a><p id=\"caption-attachment-17556\" class=\"wp-caption-text\">Image Processing with SciPy and NumPy &#8211;\u00a0Filtering Images<\/p><\/div>\n<h4 class=\"western\">a. Blurring\/Smoothing Effect using\u00a0Python<\/h4>\n<p>We can either use a Gaussian filter or a uniform filter:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; face=misc.face(gray=True)\r\n&gt;&gt;&gt; blurred=ndimage.gaussian_filter(face,sigma=3)\r\n&gt;&gt;&gt; way_blurred=ndimage.gaussian_filter(face,sigma=5)\r\n&gt;&gt;&gt; local_mean=ndimage.uniform_filter(face,size=11)<\/pre>\n<div id=\"attachment_17557\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/10.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-17557\" class=\"wp-image-17557 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/10.png\" alt=\"Image Processing with SciPy and NumPy\" width=\"1200\" height=\"400\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/10.png 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/10-150x50.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/10-300x100.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/10-768x256.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/10-1024x341.png 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-17557\" class=\"wp-caption-text\">Image Processing with SciPy and NumPy &#8211;\u00a0Blurring\/Smoothing<\/p><\/div>\n<h4 class=\"western\">b. Sharpening Effect using\u00a0Python<\/h4>\n<p>We can sharpen a blurred image as:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; face=misc.face(gray=True).astype(float)\r\n&gt;&gt;&gt; blurred=ndimage.gaussian_filter(face,3)<\/pre>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/python-generator-vs-iterator\/\">Do you know Comparison Between Python Iterators and Generators<\/a><\/strong><br \/>\nWe can also add a Laplacian approximation to increase the weight of the edges:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; filter_blurred=ndimage.gaussian_filter(blurred,1)\r\n&gt;&gt;&gt; alpha=30\r\n&gt;&gt;&gt; sharpened=blurred+alpha*(blurred-filter_blurred)<\/pre>\n<h4 class=\"western\">c. Denoising Effect using\u00a0Python<\/h4>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; f=misc.face(gray=True)\r\n&gt;&gt;&gt; f=f[230:290,220:320]\r\n&gt;&gt;&gt; noisy=f+0.4*f.std()*np.random.random(f.shape)<\/pre>\n<p>This gives us a noisy face. To smooth the noise and the edges, we use a Gaussian filter:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; gauss_denoised=ndimage.gaussian_filter(noisy,2)<\/pre>\n<p>To preserve the edges, we use a median filter:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; median_denoised=ndimage.median_filter(noisy,3)<\/pre>\n<div id=\"attachment_17558\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/11.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-17558\" class=\"wp-image-17558 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/11.png\" alt=\"Image Processing with SciPy and NumPy\" width=\"1200\" height=\"280\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/11.png 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/11-150x35.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/11-300x70.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/11-768x179.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/11-1024x239.png 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-17558\" class=\"wp-caption-text\">Image Processing with SciPy and NumPy &#8211;\u00a0Denoising<\/p><\/div>\n<p>For figures with straight boundaries and low curvature, a median filter provides a better result:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; im=np.zeros((20,20))\r\n&gt;&gt;&gt; im[5:-5,5:-5]=1\r\n&gt;&gt;&gt; im=ndimage.distance_transform_bf(im)\r\n&gt;&gt;&gt; im_noise=im+0.2*np.random.randn(*im.shape)\r\n&gt;&gt;&gt; im_med=ndimage.median_filter(im_noise,3)<\/pre>\n<div id=\"attachment_17559\" style=\"width: 1610px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/12.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-17559\" class=\"wp-image-17559 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/12.png\" alt=\"Image Processing with SciPy and NumPy\" width=\"1600\" height=\"500\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/12.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/12-150x47.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/12-300x94.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/12-768x240.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/12-1024x320.png 1024w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><p id=\"caption-attachment-17559\" class=\"wp-caption-text\">Image Processing with SciPy and NumPy &#8211;\u00a0Denoising<\/p><\/div>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/python-comment\/\">Do You Know About Python Comment, Indentation and Statement<\/a><\/strong><\/p>\n<h3 class=\"western\">Extracting Features<\/h3>\n<p>We can also extract features in our images, like detecting edges and carrying out segmentation. Let\u2019s see how.<\/p>\n<h4 class=\"western\">a. Edge Detection using\u00a0Python<\/h4>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; im=np.zeros((256,256))\r\n&gt;&gt;&gt; im[64:-64,64:-64]=1\r\n&gt;&gt;&gt; im=ndimage.rotate(im,15,mode='constant')\r\n&gt;&gt;&gt; im=ndimage.gaussian_filter(im,8)<\/pre>\n<p>For high-intensity variations, we can use Sobel, a gradient operator-<\/p>\n<div id=\"attachment_17560\" style=\"width: 1610px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/13.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-17560\" class=\"wp-image-17560 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/13.png\" alt=\"Image Processing with SciPy and NumPy\" width=\"1600\" height=\"500\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/13.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/13-150x47.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/13-300x94.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/13-768x240.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/13-1024x320.png 1024w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><p id=\"caption-attachment-17560\" class=\"wp-caption-text\">Image Processing with SciPy and NumPy &#8211;\u00a0Edge Detection<\/p><\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; sx=ndimage.sobel(im,axis=0,mode='constant')\r\n&gt;&gt;&gt; sy=ndimage.sobel(im,axis=1,mode='constant')\r\n&gt;&gt;&gt; sob=np.hypot(sx,sy)<\/pre>\n<h4 class=\"western\">b. Segmentation using\u00a0Python<\/h4>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; n,l=10,256\r\n&gt;&gt;&gt; im=np.zeros((l,l))\r\n&gt;&gt;&gt; np.random.seed(1)\r\n&gt;&gt;&gt; points=l*np.random.random((2,n**2))\r\n&gt;&gt;&gt; im[(points[0]).astype(np.int),(points[1]).astype(np.int)]=1\r\n&gt;&gt;&gt; im=ndimage.gaussian_filter(im,sigma=l\/(4.*n))\r\n&gt;&gt;&gt;\r\n&gt;&gt;&gt; mask=(im&gt;im.mean()).astype(np.float)\r\n&gt;&gt;&gt; mask+=0.1*im\r\n&gt;&gt;&gt; img=mask+0.2*np.random.randn(*mask.shape)\r\n&gt;&gt;&gt;\r\n&gt;&gt;&gt; hist,bin_edges=np.histogram(img,bins=60)\r\n&gt;&gt;&gt; bin_centers=0.5*(bin_edges[:-1]+bin_edges[1:])\r\n&gt;&gt;&gt;\r\n&gt;&gt;&gt; binary_img=img&gt;0.5<\/pre>\n<div id=\"attachment_17562\" style=\"width: 1110px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/14.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-17562\" class=\"wp-image-17562 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/14.png\" alt=\"Image Processing with SciPy and NumPy\" width=\"1100\" height=\"400\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/14.png 1100w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/14-150x55.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/14-300x109.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/14-768x279.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/14-1024x372.png 1024w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/a><p id=\"caption-attachment-17562\" class=\"wp-caption-text\">Image Processing with SciPy and NumPy<\/p><\/div>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/python-modules-vs-packages\/\">Let&#8217;s Explore Python Modules vs Packages<\/a><\/strong><br \/>\n<span style=\"font-family: Verdana, Geneva, sans-serif;font-weight: inherit\">So, this was all about\u00a0Image Processing with SciPy and NumPy. Hope you like our explanation.<\/span><\/p>\n<h3 class=\"western\">Conclusion<\/h3>\n<p>You\u2019ve turned your computer into a custom photo studio!<\/p>\n<p>We have covered everything from the basic image to how advanced filtering works with NumPy and SciPy. You can now edit photos like an expert, with the help of the tricks we learnt to work on the code.<\/p>\n<p>Furthermore, if you have doubts, feel free to ask in the comment section.<br \/>\nRelated Article-\u00a0<strong><a href=\"https:\/\/data-flair.training\/blogs\/python-web-framework\/\">Python Web Framework\u00a0<\/a><\/strong><br \/>\n<strong><a href=\"https:\/\/www.python.org\/\">For reference<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this Python tutorial, we will use Image Processing with SciPy and NumPy. We will deal with reading and writing to an image and displaying the image. We will cover different manipulations and filtering&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":17545,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[2103,3118,6304,6467,9271,10727,10819,11367,12637,12836],"class_list":["post-17529","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-blurringsmoothing-effect","tag-creating-a-python-numpy-array","tag-how-to-read-and-write-image-in-python","tag-image-processing-with-scipy-and-numpy","tag-opening-a-raw-file","tag-python-numpy","tag-python-scipy","tag-reading-and-writing-to-images","tag-scipy-and-numpy","tag-sharpening-effect"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Image Processing with SciPy and NumPy in Python - DataFlair<\/title>\n<meta name=\"description\" content=\"Image Processing with SciPy and NumPy- Python SciPy,Python NumPy,Image Manipulation, Blurring effect, SHaring Effect in Image,Edge Detection, Pytho Interpolation\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/image-processing-with-scipy-and-numpy\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Image Processing with SciPy and NumPy in Python - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Image Processing with SciPy and NumPy- Python SciPy,Python NumPy,Image Manipulation, Blurring effect, SHaring Effect in Image,Edge Detection, Pytho Interpolation\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/image-processing-with-scipy-and-numpy\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-06-04T22:30:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-24T12:06:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Image-Processing-with-SciPy-and-NumPy-01.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Image Processing with SciPy and NumPy in Python - DataFlair","description":"Image Processing with SciPy and NumPy- Python SciPy,Python NumPy,Image Manipulation, Blurring effect, SHaring Effect in Image,Edge Detection, Pytho Interpolation","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/image-processing-with-scipy-and-numpy\/","og_locale":"en_US","og_type":"article","og_title":"Image Processing with SciPy and NumPy in Python - DataFlair","og_description":"Image Processing with SciPy and NumPy- Python SciPy,Python NumPy,Image Manipulation, Blurring effect, SHaring Effect in Image,Edge Detection, Pytho Interpolation","og_url":"https:\/\/data-flair.training\/blogs\/image-processing-with-scipy-and-numpy\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-06-04T22:30:58+00:00","article_modified_time":"2026-04-24T12:06:53+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Image-Processing-with-SciPy-and-NumPy-01.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/image-processing-with-scipy-and-numpy\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/image-processing-with-scipy-and-numpy\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Image Processing with SciPy and NumPy in Python","datePublished":"2018-06-04T22:30:58+00:00","dateModified":"2026-04-24T12:06:53+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/image-processing-with-scipy-and-numpy\/"},"wordCount":1452,"commentCount":2,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/image-processing-with-scipy-and-numpy\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Image-Processing-with-SciPy-and-NumPy-01.jpg","keywords":["Blurring\/Smoothing Effect","Creating a Python numpy array","How to read and write image in python","Image Processing with SciPy and NumPy","Opening a RAW file","Python NumPy","Python SciPy","Reading and Writing to Images","SciPy and NumPy","Sharpening Effect"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/image-processing-with-scipy-and-numpy\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/image-processing-with-scipy-and-numpy\/","url":"https:\/\/data-flair.training\/blogs\/image-processing-with-scipy-and-numpy\/","name":"Image Processing with SciPy and NumPy in Python - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/image-processing-with-scipy-and-numpy\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/image-processing-with-scipy-and-numpy\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Image-Processing-with-SciPy-and-NumPy-01.jpg","datePublished":"2018-06-04T22:30:58+00:00","dateModified":"2026-04-24T12:06:53+00:00","description":"Image Processing with SciPy and NumPy- Python SciPy,Python NumPy,Image Manipulation, Blurring effect, SHaring Effect in Image,Edge Detection, Pytho Interpolation","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/image-processing-with-scipy-and-numpy\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/image-processing-with-scipy-and-numpy\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/image-processing-with-scipy-and-numpy\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Image-Processing-with-SciPy-and-NumPy-01.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Image-Processing-with-SciPy-and-NumPy-01.jpg","width":1200,"height":628,"caption":"Image Processing with SciPy and NumPy"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/image-processing-with-scipy-and-numpy\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Image Processing with SciPy and NumPy in Python"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/17529","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=17529"}],"version-history":[{"count":10,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/17529\/revisions"}],"predecessor-version":[{"id":147869,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/17529\/revisions\/147869"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/17545"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=17529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=17529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=17529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}