

{"id":117914,"date":"2023-12-21T18:00:10","date_gmt":"2023-12-21T12:30:10","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=117914"},"modified":"2023-12-21T18:54:29","modified_gmt":"2023-12-21T13:24:29","slug":"working-with-images-in-python-using-matplotlib","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/working-with-images-in-python-using-matplotlib\/","title":{"rendered":"Working with Images in Python using Matplotlib"},"content":{"rendered":"<p>The fields of computer vision, medical imaging, and digital media all rely heavily on advances in image processing. Improving or gaining insight from digital pictures requires analysing, improving, and modifying them. This tutorial focuses on utilising the robust Matplotlib module in Python, which offers a full suite of tools for dealing with pictures.<\/p>\n<h2>Getting images to load and show up<\/h2>\n<h3>Importing the Necessary Libraries<\/h3>\n<p>It&#8217;s important to import the appropriate libraries before getting started with image processing. In addition to NumPy and OpenCV, Matplotlib also provides a wide variety of tools and methods for manipulating visual data. Maintaining Python compatibility is also essential.<\/p>\n<p><strong>Take a look at this code snippet for an example of library import:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import matplotlib.pyplot as plott\r\nimport numpy as numpyy\r\nfrom matplotlib.image import imread\r\n<\/pre>\n<h3>Loading Images<\/h3>\n<p>The first step in dealing with pictures in Python is to import them from files. The imread function in Matplotlib makes it easy to load pictures saved in formats like JPEG and PNG. Successful picture loading requires familiarity with many image formats and the ability to convert between them.<\/p>\n<p><strong>A sample of Matplotlib code to load an image with the name \u201c3d-plots-in-matplotlib.jpeg\u201d is as follows:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">from matplotlib.image import imread\r\n\r\n\r\nimage_path = '\/content\/3d-plots-in-matplotlib.jpeg' #This is the path of image\r\n\r\n\r\nimg = imread(image_path)\r\n<\/pre>\n<h3>Viewing Figures<\/h3>\n<p>After an image is loaded, the imshow function in Matplotlib may be used to display it. The picture is plotted so you can see what&#8217;s within thanks to this feature. Image visualisation may be improved by modifying display settings like colormap and interpolation.<\/p>\n<p><strong>Take this sample of code for showing an image in Matplotlib as an example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># Display image\r\nplott.imshow(img)\r\n\r\n\r\n# Customize display options\r\nplott.imshow(img, cmap='gray', interpolation='nearest')\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Viewing-Figures.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125285 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Viewing-Figures.webp\" alt=\"Viewing Figures\" width=\"552\" height=\"306\" \/><\/a><\/p>\n<h3>Transformation and Manipulation of Images in Matplotlib<\/h3>\n<h4>Image Reduction and Enlargement<\/h4>\n<p>Image processing often involves actions like resizing and scaling pictures. The resize function in Matplotlib lets you modify an image&#8217;s width, height, and aspect ratio. This is helpful for tailoring photos to unique specifications or intended displays.<\/p>\n<p><strong>A sample of Matplotlib code to resize a picture is as follows:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import matplotlib.pyplot as plott\r\nfrom skimage.transform import resize\r\n\r\n\r\n# Define the desired height and width\r\nchanged_height = 300\r\nchanged_width = 400\r\n\r\n\r\n# Resize image\r\nresized_image = resize(img, (changed_height, changed_width))\r\nplott.imshow(resized_image)\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Image-Reduction-and-Enlargement.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125286 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Image-Reduction-and-Enlargement.webp\" alt=\"Image Reduction and Enlargement\" width=\"548\" height=\"418\" \/><\/a><\/p>\n<h4>Cropping and Identifying Key Areas<\/h4>\n<p>There are times when you need to zero down on a very precise part of a picture. Selecting and extracting regions of interest (ROI) from a picture is made possible using Matplotlib&#8217;s slicing and indexing features. This opens the door to future investigation or focus on certain aspects.<\/p>\n<p><strong>Take the following Matplotlib code snippet as an example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># Assuming the variables b1, b2, a1, and a2 are the coordinates of the cropping region\r\nb1 = 100\r\nb2 = 300\r\na1 = 200\r\na2 = 400\r\n\r\n\r\n# Crop image\r\ncropped_image = img[b1:b2, a1:a2]\r\nplott.imshow(cropped_image)\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Cropping-and-Identifying-Key-Areas.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125287 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Cropping-and-Identifying-Key-Areas.webp\" alt=\"Cropping and Identifying Key Areas\" width=\"425\" height=\"417\" \/><\/a><\/p>\n<h4>Flipping and rotating images<\/h4>\n<p>In order to alter an image&#8217;s orientation or viewpoint, popular transformations include rotation and flipping. The rotate function in Matplotlib allows for the picture to be rotated in a variety of ways. Images may be flipped horizontally or vertically for mirrored effects or to get new insights.<\/p>\n<p><strong>To illustrate how to rotate an image using Matplotlib, consider the following code snippet:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># Rotate image\r\nrotated_image = numpyy.rot90(img, k=1)  # 90-degree rotation\r\nplott.imshow(rotated_image)\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Flipping-and-rotating-images.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125288 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Flipping-and-rotating-images.webp\" alt=\"Flipping and rotating images\" width=\"256\" height=\"418\" \/><\/a><\/p>\n<h3>Enhancing and filtering images in Matplotlib<\/h3>\n<h4>Incorporating Filters<\/h4>\n<p>Filters are essential in image processing since they alter the values of the pixels of a picture to get the desired result. Matplotlib has a number of filter functions that may be used to blur or sharpen pictures. Filters may be used to improve picture quality, get rid of unwanted noise, and get images ready for further research and visualisation.<\/p>\n<p><strong>Think about this Matplotlib code snippet for adding a Gaussian blur to an image:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">from skimage.filters import gaussian\r\n\r\n\r\n# Apply Gaussian filter to image\r\nfiltered_image = gaussian(img, sigma=2)\r\nplott.imshow(filtered_image)\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Incorporating-Filters.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125289 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Incorporating-Filters.webp\" alt=\"Incorporating Filters \" width=\"552\" height=\"306\" \/><\/a><\/p>\n<h4>Making Changes to the Hue, Contrast, and Saturation<\/h4>\n<p>Brightness, contrast, and saturation adjustments may have a large influence on an image&#8217;s overall aesthetic appeal and readability. Matplotlib has methods to change these attributes, so you may tweak the necessary settings to make details in the picture more visible.<\/p>\n<p><strong>For instance, consider this Matplotlib code snippet for modifying the image&#8217;s hue, saturation, and brightness:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import matplotlib.pyplot as plott\r\nfrom skimage import exposure\r\n\r\n\r\n# Load the image\r\nimg = plott.imread('\/content\/3d-plots-in-matplotlib.jpeg')\r\n\r\n\r\n# Adjust brightness, contrast, and saturation\r\nbrightness_factor = 1.2\r\ncontrast_factor = 1.5\r\nsaturation_factor = 1.3\r\n\r\n\r\n# Adjust brightness using gamma correction\r\nadjusted_img = exposure.adjust_gamma(img, brightness_factor)\r\n\r\n\r\n# Adjust contrast\r\nadjusted_img = exposure.rescale_intensity(adjusted_img, in_range='image', out_range=(0, 1))\r\nadjusted_img = exposure.adjust_gamma(adjusted_img, 1 \/ contrast_factor)\r\n\r\n\r\n# Adjust saturation\r\nadjusted_img = exposure.adjust_gamma(adjusted_img, 1 \/ saturation_factor)\r\n\r\n\r\n# Display the original and adjusted images\r\nfig, axes = plott.subplots(1, 2, figsize=(10, 5))\r\n\r\n\r\naxes[0].imshow(img)\r\naxes[0].set_title('Original Image DataFlair')\r\naxes[0].axis('off')\r\n\r\n\r\naxes[1].imshow(adjusted_img)\r\naxes[1].set_title('Adjusted Image DataFlair')\r\naxes[1].axis('off')\r\n\r\n\r\nplott.show()\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Making-Changes-tthe-Hue-Contrast-and-Saturation.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125290 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Making-Changes-tthe-Hue-Contrast-and-Saturation.webp\" alt=\"Making Changes the Hue Contrast and Saturation\" width=\"794\" height=\"225\" \/><\/a><\/p>\n<p>This code uses the skimage method adjust_gamma() to tweak the image&#8217;s contrast. Next, we change the brightness using the rescale_intensity() method, and last, we tweak the saturation with another use to the adjust_gamma() function.<\/p>\n<h3>Matplotlib for Image Processing<\/h3>\n<p>Although Matplotlib isn&#8217;t designed primarily for image processing, its capabilities may be expanded to carry out simple picture alteration operations. We may display pictures in plots using the imshow() method that is provided by Matplotlib. NumPy arrays may also be used to change an image&#8217;s pixel values.<\/p>\n<h4>Image negative<\/h4>\n<p><strong>Let&#8217;s begin with a basic example of using Matplotlib to perform the image negative procedure on an image;<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import matplotlib.pyplot as plott\r\nimport matplotlib.image as mpimg\r\nimport numpy as numpyy\r\n\r\n\r\n# Load the image\r\nimage = mpimg.imread('\/image2.jpg')\r\n\r\n\r\n# Perform image negative\r\nnegative_image = 1 - image\r\n\r\n\r\n# Display the original and negative images\r\nplott.figure(figsize=(10, 5))\r\nplott.subplot(1, 2, 1)\r\nplott.imshow(image)\r\nplott.title('Original Image DataFlair')\r\n\r\n\r\nplott.subplot(1, 2, 2)\r\nplott.imshow(negative_image)\r\nplott.title('Negative Image DataFlair')\r\n\r\n\r\nplott.show()\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Image-negative.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125291 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Image-negative.webp\" alt=\"Image negative\" width=\"831\" height=\"299\" \/><\/a><\/p>\n<p>In this example, we use mpimg.imread() to load an image, and then we apply the image negative operations by reducing the value of each pixel by 1. Then, using plt.subplot() and plt.imshow(), we show both the positive and negative pictures side by side.<\/p>\n<h3>Modern Techniques for Image Processing in Matplotlib<\/h3>\n<p>While Matplotlib is capable of handling simple image processing, we may combine it with additional libraries like Scipy, OpenCV, or PIL (Python Imaging Library) to do more complex tasks. Let&#8217;s look at some sophisticated Scipy image processing methods.<\/p>\n<h4>Scipy to blur an image.<\/h4>\n<p>When decreasing noise or obscuring crucial information, blurring a picture may be helpful. To apply Gaussian blurring to pictures, Scipy offers the ndimage.gaussian_filter() method.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import matplotlib.pyplot as plott\r\nimport matplotlib.image as mpimg\r\nimport numpy as numpyy\r\nfrom scipy.ndimage import gaussian_filter\r\n\r\n\r\n# Load the image\r\nimage = mpimg.imread('\/image2.jpg')\r\n\r\n\r\n# Apply Gaussian blur with sigma=3\r\nblurred_image = gaussian_filter(image, sigma=3)\r\n\r\n\r\n# Display the original and blurred images\r\nplott.figure(figsize=(10, 5))\r\nplott.subplot(1, 2, 1)\r\nplott.imshow(image)\r\nplott.title('Original Image DataFlair')\r\n\r\n\r\nplott.subplot(1, 2, 2)\r\nplott.imshow(blurred_image)\r\nplott.title('Blurred Image DataFlair')\r\n\r\n\r\nplott.show()\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Scipy-to-blur-an-image.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125292 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Scipy-to-blur-an-image.webp\" alt=\"Scipy to blur an image\" width=\"831\" height=\"299\" \/><\/a><\/p>\n<p>To add Gaussian blurring to the picture in this example, we utilise the gaussian_filter() function from Scipy&#8217;s ndimage package. The sigma parameter regulates the level of blurring.<\/p>\n<h4>Scipy to Detect Image Edges<\/h4>\n<p>Edge detection is a key image processing method used to identify the borders and edges of objects in a picture. For edge detection, Scipy offers the ndimage.sobel() method.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import matplotlib.pyplot as plott\r\nimport matplotlib.image as mpimg\r\nimport numpy as numpyy\r\nfrom scipy.ndimage import sobel\r\n\r\n\r\n# Load the image\r\nimage = mpimg.imread('\/image2.jpg')\r\n\r\n\r\n# Apply edge detection using Sobel filter\r\nedge_image = sobel(image)\r\n\r\n\r\n# Display the original and edge-detected images\r\nplott.figure(figsize=(10, 5))\r\nplott.subplot(1, 2, 1)\r\nplott.imshow(image)\r\nplott.title('Original Image DataFlair')\r\n\r\n\r\nplott.subplot(1, 2, 2)\r\nplott.imshow(edge_image)\r\nplott.title('Edge-Detected Image DataFlair')\r\n\r\n\r\nplott.show()\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Scipy-to-Detect-Image-Edges.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125293 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Scipy-to-Detect-Image-Edges.webp\" alt=\"Scipy to Detect Image Edges\" width=\"831\" height=\"299\" \/><\/a><\/p>\n<p>In this example, edge detection is applied to the picture using the sobel() function from the ndimage module of Scipy. The gradient magnitude is calculated by the function, emphasising the edges of the picture.<\/p>\n<h3>Tones and Shades<\/h3>\n<p>The brightness, contrast, and saturation of a picture may be changed using tint, shade, and tone colour modification methods.<\/p>\n<h4>Image Shading<\/h4>\n<p>A picture is shaded by adding black or white to make it look darker or lighter. By modifying the pixel values, Matplotlib allows us to add shade.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import matplotlib.pyplot as plott\r\nimport matplotlib.image as mpimg\r\nimport numpy as numpyy\r\n\r\n\r\n# Load the image\r\nimage = mpimg.imread('\/image3.png')\r\n\r\n\r\n# Define the shading factor (0.5 for lighter, 1.5 for darker)\r\nshading_factor = 0.5\r\n\r\n\r\n# Apply shading by adjusting pixel values\r\nshaded_image = numpyy.clip(image * shading_factor, 0, 1)\r\n\r\n\r\n# Display the original and shaded images\r\nplott.figure(figsize=(10, 5))\r\nplott.subplot(1, 2, 1)\r\nplott.imshow(image)\r\nplott.title('Original Image DataFlair')\r\n\r\n\r\nplott.subplot(1, 2, 2)\r\nplott.imshow(shaded_image)\r\nplott.title('Shaded Image DataFlair')\r\n\r\n\r\nplott.show()\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Image-Shading.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125294 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Image-Shading.webp\" alt=\"Image Shading \" width=\"831\" height=\"300\" \/><\/a><\/p>\n<p>In this illustration, we first establish a shading factor (for instance, 0.5 for lighter shading, 1.5 for deeper shading), and then we apply the shading by modifying the pixel values in accordance with the shading factor.<\/p>\n<h4>Image gradation<\/h4>\n<p>Toning involves changing an image&#8217;s saturation, giving it a desaturated or sepia tone. By changing the picture&#8217;s colour space to HSV and modifying the saturation channel, we may use Matplotlib to tone an image.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import matplotlib.pyplot as plott\r\nimport matplotlib.image as mpimg\r\nimport cv2\r\nimport numpy as numpyy\r\n\r\n\r\n# Load the image\r\nimage = mpimg.imread('\/image3.png')\r\n\r\n\r\n# Convert the image to BGR format (required by cv2)\r\nbgr_image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)\r\n\r\n\r\n# Define the toning factor (0 for desaturated, 1 for original, 2 for sepia-toned)\r\ntoning_factor = 2\r\n\r\n\r\n# Convert the image to LAB color space\r\nlab_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2Lab)\r\n\r\n\r\n# Split the LAB image into L, A, and B channels\r\nl_channel, a_channel, b_channel = cv2.split(lab_image)\r\n\r\n\r\n# Multiply the A and B channels by the toning factor\r\na_channel = numpyy.clip(a_channel * toning_factor, 0, 255)\r\nb_channel = numpyy.clip(b_channel * toning_factor, 0, 255)\r\n\r\n\r\n# Merge the modified channels back into the LAB image\r\nlab_toned_image = cv2.merge((l_channel, a_channel, b_channel))\r\n\r\n\r\n# Convert the LAB image back to BGR color space\r\nbgr_toned_image = cv2.cvtColor(lab_toned_image, cv2.COLOR_Lab2BGR)\r\n\r\n\r\n# Convert the BGR image back to RGB format (required by matplotlib)\r\ntoned_image = cv2.cvtColor(bgr_toned_image, cv2.COLOR_BGR2RGB)\r\n\r\n\r\n# Display the original and toned images\r\nplott.figure(figsize=(10, 5))\r\nplott.subplot(1, 2, 1)\r\nplott.imshow(image)\r\nplott.title('Original Image DataFlair')\r\n\r\n\r\nplott.subplot(1, 2, 2)\r\nplott.imshow(toned_image)\r\nplott.title('Toned Image DataFlair')\r\n\r\n\r\nplott.show()\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Image-gradation.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125295 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Image-gradation.webp\" alt=\"Image gradation\" width=\"831\" height=\"300\" \/><\/a><\/p>\n<p>The colorsys.rgb_to_hsv() function is used in this example to convert the picture to the HSV colour space. The picture is then returned to the RGB colour space using colorsys.hsv_to_rgb() after the saturation channel has been adjusted depending on the toning factor.<\/p>\n<h3>Image Export and Storage<\/h3>\n<h4>Storing Pictures in a File<\/h4>\n<p>After finishing editing your pictures, you may wish to save them in files for further review or distribution. Matplotlib&#8217;s imsave function enables you to save pictures in a number of different file formats, including JPEG and PNG. The act of storing photographs in files preserves their longevity and makes them accessible in a variety of contexts.<\/p>\n<p><strong>Take the following Matplotlib code snippet as an example of how to save an image:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">from matplotlib.image import imsave\r\n\r\n\r\nimsave('processed_image.jpg', adjusted_img)\r\n<\/pre>\n<h4>Saving Images for Publication or the Web<\/h4>\n<p>File size, resolution, and picture quality are all significant considerations when exporting photographs for use on the web or in print. Matplotlib gives you the option of exporting pictures in optimised formats like JPEG or PNG, allowing you to strike a compromise between these factors. Optimising photos for their intended use maximises visual quality while decreasing file size.<\/p>\n<p><strong>Take a look at this code snippet as an example of how to export a picture in JPEG format with a fixed compression level using Matplotlib:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">from PIL import Image\r\n\r\n\r\n# Convert the adjusted image to the PIL Image format\r\npil_image = Image.fromarray((adjusted_img * 255).astype(numpyy.uint8))\r\n\r\n\r\n# Save the image in an optimized JPEG format with the desired quality\r\npil_image.save('optimized_image.jpg', format='JPEG', quality=90)\r\n\r\n<\/pre>\n<h3>Conclusion<\/h3>\n<p>In conclusion, the power of image processing is unleashed, and new doors are opened when dealing with pictures in Python using Matplotlib. To improve picture quality, get rid of noise, and get photographs ready for different uses, you may apply filters, tweak image attributes, and save\/export images. Matplotlib&#8217;s flexibility allows you to experiment with several image processing methods and get novel insights into your data. Cheers, and have fun with the code and the images!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The fields of computer vision, medical imaging, and digital media all rely heavily on advances in image processing. Improving or gaining insight from digital pictures requires analysing, improving, and modifying them. This tutorial focuses&#46;&#46;&#46;<\/p>\n","protected":false},"author":86671,"featured_media":117916,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27777],"tags":[29099,8601,29088,29100],"class_list":["post-117914","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-matplotlib-tutorials","tag-image-processing-in-matplotlib","tag-matplotlib","tag-matplotlib-tutorials","tag-working-with-images-in-python-matplotlib"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Working with Images in Python using Matplotlib - DataFlair<\/title>\n<meta name=\"description\" content=\"The power of image processing is unleashed and new doors are opened when dealing with pictures in Python using Matplotlib.\" \/>\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\/working-with-images-in-python-using-matplotlib\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Working with Images in Python using Matplotlib - DataFlair\" \/>\n<meta property=\"og:description\" content=\"The power of image processing is unleashed and new doors are opened when dealing with pictures in Python using Matplotlib.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/working-with-images-in-python-using-matplotlib\/\" \/>\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=\"2023-12-21T12:30:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-21T13:24:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/working-with-images-in-python-using-matplptlib.webp\" \/>\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\/webp\" \/>\n<meta name=\"author\" content=\"TechVidvan 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=\"TechVidvan 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":"Working with Images in Python using Matplotlib - DataFlair","description":"The power of image processing is unleashed and new doors are opened when dealing with pictures in Python using Matplotlib.","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\/working-with-images-in-python-using-matplotlib\/","og_locale":"en_US","og_type":"article","og_title":"Working with Images in Python using Matplotlib - DataFlair","og_description":"The power of image processing is unleashed and new doors are opened when dealing with pictures in Python using Matplotlib.","og_url":"https:\/\/data-flair.training\/blogs\/working-with-images-in-python-using-matplotlib\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2023-12-21T12:30:10+00:00","article_modified_time":"2023-12-21T13:24:29+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/working-with-images-in-python-using-matplptlib.webp","type":"image\/webp"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/working-with-images-in-python-using-matplotlib\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/working-with-images-in-python-using-matplotlib\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/0e594f928e31fc96628ac40f6ae74f49"},"headline":"Working with Images in Python using Matplotlib","datePublished":"2023-12-21T12:30:10+00:00","dateModified":"2023-12-21T13:24:29+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/working-with-images-in-python-using-matplotlib\/"},"wordCount":1340,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/working-with-images-in-python-using-matplotlib\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/working-with-images-in-python-using-matplptlib.webp","keywords":["image processing in matplotlib","Matplotlib","matplotlib tutorials","working with images in python matplotlib"],"articleSection":["Matplotlib Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/working-with-images-in-python-using-matplotlib\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/working-with-images-in-python-using-matplotlib\/","url":"https:\/\/data-flair.training\/blogs\/working-with-images-in-python-using-matplotlib\/","name":"Working with Images in Python using Matplotlib - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/working-with-images-in-python-using-matplotlib\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/working-with-images-in-python-using-matplotlib\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/working-with-images-in-python-using-matplptlib.webp","datePublished":"2023-12-21T12:30:10+00:00","dateModified":"2023-12-21T13:24:29+00:00","description":"The power of image processing is unleashed and new doors are opened when dealing with pictures in Python using Matplotlib.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/working-with-images-in-python-using-matplotlib\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/working-with-images-in-python-using-matplotlib\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/working-with-images-in-python-using-matplotlib\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/working-with-images-in-python-using-matplptlib.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/working-with-images-in-python-using-matplptlib.webp","width":1200,"height":628,"caption":"working with images in python using matplptlib"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/working-with-images-in-python-using-matplotlib\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Matplotlib Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/matplotlib-tutorials\/"},{"@type":"ListItem","position":3,"name":"Working with Images in Python using Matplotlib"}]},{"@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\/0e594f928e31fc96628ac40f6ae74f49","name":"TechVidvan Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","caption":"TechVidvan Team"},"description":"TechVidvan Team provides high-quality content &amp; courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.","url":"https:\/\/data-flair.training\/blogs\/author\/test001\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/117914","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\/86671"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=117914"}],"version-history":[{"count":7,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/117914\/revisions"}],"predecessor-version":[{"id":132293,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/117914\/revisions\/132293"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/117916"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=117914"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=117914"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=117914"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}