This is the 22nd day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

preface

As is known to all, the Matplotlib module is very powerful. It not only provides pyplot for drawing different charts such as broken lines, bars and scatterpoints, but also patches of commonly used graphics and animation for drawing dynamic diagrams. The details can be found in the previous article.

  • The matplotlib module is divided into three layers of scripting, art and back-end function learning

  • Matplotlib: The matplotlib module provides the pyplo.plot () method for plotting line plots

  • Matplotlib plotting bar charts: The matplotlib module provides the pyplot.bar() method for plotting bar charts

  • Matplotlib: The Matplotlib module provides the Pyplot interactive mode for plotting dynamic plots

  • The matplotlib Animation class draws animations: The matplotylib module provides the Animation class to draw dynamic diagrams

The matplotlib.image class is also provided in the matplotlib module for image loading, scaling and display operations.

Matplotlib. image = matplotlib.image = matplotlib.image = matplotlib.image

1. Matplotlib. Description of the image

Matplotlib. image is designed to load, scale, and display images.

  • Matplotlib. image converts the image load into a three-dimensional array, with each set of data in the list representing a pixel
  • Matplotlib. image can render a generated or imported NUMpy array to generate an image
  • Matplotlib. image can be used to add different gamutes to the image for color processing
  • Matplotlib. image Adds clim to the image to limit RGB, and can also add scale to view
  • Matplotlib. image uses the Interpolation property to add Mosaic, blur etc to the image

2. Methods related to image processing

  • The matplotlib.image module provides class methods

Class method instructions
matplotlib.image._Image(ax) Image abstract class, derived from Artist
matplotlib.image.AxesImage(ax) Add Axes object to the image, one of the subclasses of _Image
matplotlib.image.PcolorImage(ax) Make pcolor style drawings by using irregular grids
matplotlib.image.FigureImage(fig) Add the picture to the canvas
matplotlib.image.BboxImage(bbox) Given the Image class for a particular bbox
matplotlib.image.NonUniformImage(ax) The Image class handles non-uniform images

  • The matplotlib.image module provides the methods

methods role
matplotlib.image.imread(frame) Loading pictures
matplotlib.image.imsave(frame,arr) Save the image file
matplotlib.image.thumbnail(infile,thumfile) Thumbnail images
matplotlib.image.pil_to_array(pilImage) Load PIL image as numpy int array return
matplotlib.image.composite_images(images, renderer) Combine multiple RGBA images into one

3. Image processing steps

In matplotlib module, we can use image class related methods to process images, mainly including the following steps:

  • Import matplotlib.pyplot for drawing graphs and matplotlib.image classes for image processing
import matplotlib.pyplot as plt
import matplotlib.image as mpimage
Copy the code
  • Call pyplot.subplots() to create two Axes objects
fig,(ax1,ax2) = plt.subplots(1.2,sharey=True)
Copy the code
  • Call matplotlib.image.imread(file) to load the image and generate 3d data
img = mpimage.imread("image.png")
Copy the code
  • The three-dimensional data of the image are retrieved. The image pixel channel only displays single-channel data
create_img = img[:,:,0]
Copy the code
  • Call Pyplot.imshow (img) to convert image single channel 3D data into image
ax1.imshow(img)
ax2.imshow(create_img)
Copy the code
  • Call Pyplot.show () to render the image onto the canvas
ax1.set_title("Original")
ax2.set_title("Display single channel")
pyplot.show()
Copy the code
  • Original image comparison shows single channel color scheme results

  • Color the image using the Pyplt.imShow () cmap property
create_img = img[:,:,2]
ax2.imshow(create_img,cmap="Blues_r")
Copy the code

  • Call pyplot.colorbar() to display the colorbar
img = mpimage.imread("image.jpg")
create_img = img[:, :, 2]

fig = plt.figure()
ax1 = fig.add_subplot(1.2.1)
im = plt.imshow(img,cmap="hot")
im.set_clim(0.0.0.7)
ax1.set_title("Original")
plt.colorbar(orientation='horizontal')
ax2 = fig.add_subplot(1.2.2)
im = plt.imshow(create_img,cmap="Blues_r")
im.set_clim(0.0.0.7)
ax2.set_title("Color")
plt.colorbar(orientation="horizontal")
Copy the code

  • Zoom the image and interpolate it by calling thumbnail()
from PIL import Image
Pimg = Image.open("BQ.jpg")

ax2 = fig.add_subplot(1.2.2)
Pimg.thumbnail((64.64), Image.ANTIALIAS)
im = plt.imshow(Pimg)
im.set_clim(0.0.0.7)
ax2.set_title("Interpolation")
plt.colorbar(orientation="horizontal")
Copy the code

  • Blur the image and add the Interpolation attribute
Pimg = Image.open("BQ.jpg")
fig = plt.figure()
ax1 = fig.add_subplot(1.2.1)
Pimg.thumbnail((64.64), Image.ANTIALIAS)
im = plt.imshow(Pimg,interpolation="bicubic")
im.set_clim(0.0.0.7)
ax1.set_title("bicubic")
plt.colorbar(orientation='horizontal')
ax2 = fig.add_subplot(1.2.2)
Pimg.thumbnail((64.64), Image.ANTIALIAS)
im = plt.imshow(Pimg,interpolation="nearest")
im.set_clim(0.0.0.7)
ax2.set_title("nearest")
plt.colorbar(orientation="horizontal")
Copy the code

conclusion

In this issue, we will learn and master the methods of image processing related to matplotlib.image. PIL image library is usually used in combination with PIL image library when the image is blurred.

That’s the content of this episode. Please give us your thumbs up and comments. See you next time