5 Best Ways to Redraw an Image Using Python’s Matplotlib

πŸ’‘ Problem Formulation: You’ve got an image on your hard drive or in memory, and you want to modify or redraw it using Pythonβ€”specifically, with the help of the Matplotlib library. Whether you’re working on data visualisation, image processing, or just need to display an image within your Python environment, Matplotlib offers versatile tools for this task. An example input could be a PNG image file, and the desired output would be the same image rendered within a Python script, now capable of being modified and saved anew.

Method 1: Using imshow() to Display an Image

Matplotlib’s imshow() function is a quick and straightforward way to display image data. It is versatile enough to work with different image formats, accepts arrays as input, and can work seamlessly with other libraries like NumPy.

Here’s an example:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

img = mpimg.imread('image.png')
plt.imshow(img)
plt.show()

The output is the image ‘image.png’ displayed in a Matplotlib window.

In this code snippet, we import Matplotlib’s plotting module and image module. We then read the image file using mpimg.imread() and display it with plt.imshow(). The plt.show() commands renders the image in a GUI window.

Method 2: Modifying Image Data Before Redrawing

Before redrawing, you might want to modify the image data. The image data can be manipulated as a NumPy array, where you can apply transformations or filters.

Here’s an example:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np

img = mpimg.imread('image.png')
lum_img = img[:, :, 0]
plt.imshow(lum_img, cmap='hot')
plt.colorbar()
plt.show()

The output is a redrawing of ‘image.png’ with a colormap applied, showing variations in luminosity, accompanied by a colorbar.

We first read the image and select only the first channel representing luminosity. The plt.imshow() function is then used with the ‘hot’ colormap to depict differences in luminance. Finally, we add a colorbar for reference.

Method 3: Redrawing an Image with Subplots

Matplotlib enables drawing multiple images in one figure using subplots. This is helpful when you want to compare images side-by-side.

Here’s an example:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

img = mpimg.imread('image.png')
fig, axarr = plt.subplots(1, 2)
axarr[0].imshow(img)
axarr[1].imshow(img, cmap='gray')
plt.show()

The output features two images: the original and a grayscale version, displayed side by side in the same figure.

This code creates a figure with two subplots (axarr). It reads the image and displays it in the first subplot as is, and in the second subplot with a grayscale colormap.

Method 4: Saving the Modified Image to Disk

After making changes to an image, you might want to save the updated image back to disk. Matplotlib provides the savefig() function, which can be used after all plotting commands.

Here’s an example:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

img = mpimg.imread('image.png')
plt.imshow(img, cmap='gray')
plt.savefig('modified_image.png')

The result is a file named ‘modified_image.png’ saved to the current working directory, containing the modified image in grayscale.

This code snippet reads the original image, applies a grayscale colormap through plt.imshow(), and then saves the modified image to disk with plt.savefig().

Bonus One-Liner Method 5: Redraw and Save in One Line

If you’re looking for efficiency and want to read, redraw, and save an image using a one-liner, Matplotlib makes it possible with method chaining.

Here’s an example:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

mpimg.imsave('modified_image.png', mpimg.imread('image.png'), cmap='viridis')

This generates ‘modified_image.png’ with the ‘viridis’ colormap applied.

This one-liner reads the original image with mpimg.imread(), then immediately saves it with a different colormap ‘viridis’ using mpimg.imsave().

Summary/Discussion

  • Method 1: Using imshow(). Strengths: Simple and quick. Weaknesses: Limited by default transformations.
  • Method 2: Modifying Image Data. Strengths: Allows for advanced image modification. Weaknesses: Requires understanding of image data manipulation.
  • Method 3: Redrawing with Subplots. Strengths: Good for comparative analysis. Weaknesses: Layout customization can be slightly complex.
  • Method 4: Saving Modified Images. Strengths: Easy saving of results to disk. Weaknesses: Involves extra step if only displaying is needed.
  • Bonus Method 5: One-Liner Redraw-Save. Strengths: Extremely efficient. Weaknesses: Less transparency in the process for beginners.