Python visualized image --Matplotlib

The tutorial uses matplotlibthe plotter command interfaces pyplot. This interface maintains a global state, and can be used easily and quickly try various drawing provided. The other is object-oriented interface, which is also very strong, are generally more suitable for the development of large applications. If you want to learn object-oriented interface to frequently asked questions on the use it is a good start for a page.

1. The image data into the array NumPy

Load image data is supported by Pillow library. Originally, matplotlibonly supports PNG images. If the unit fails to read the command shown below will fall back Pillow.

The image used in this example is a JPG file, but remember Pillow require you to own data.

Here's what we want to fiddle with the picture:

Here Insert Picture Description

Now we begin to load and print image info:

import matplotlib.image as mpimg

img = mpimg.imread('images/mitaomao.jpg')
print(img)

Output:

[[[255 255 255]
  [255 255 255]
  [255 255 255]
  ...
  [255 255 255]
  [255 255 255]
  [255 255 255]]

 [[255 255 255]
  [255 255 255]
  [255 255 255]
  ...
  [255 255 255]
  [255 255 255]
  [255 255 255]]

 [[255 255 255]
  [255 255 255]
  [255 255 255]
  ...
  [255 255 255]
  [255 255 255]
  [255 255 255]]

 ...

 [[255 255 255]
  [255 255 255]
  [255 255 255]
  ...
  [255 255 255]
  [255 255 255]
  [255 255 255]]

 [[255 255 255]
  [255 255 255]
  [255 255 255]
  ...
  [255 255 255]
  [255 255 255]
  [255 255 255]]

 [[255 255 255]
  [255 255 255]
  [255 255 255]
  ...
  [255 255 255]
  [255 255 255]
  [255 255 255]]]

2. The draw for the NumPy array image

Therefore, you save the data in an numparray (which by the introduction of, or generating it). Let us render it now. In Matplotlib, this is the use of imshow()the function performed. Here we will grab plotobjects. The object provides an easy way to process drawing from the prompt.

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

img = mpimg.imread('images/mitaomao.jpg')
imgplot = plt.imshow(img)
plt.show()

Note: Change the path to place your own picture.

Here Insert Picture Description

You can also draw any NumPy array.

3. Color reference scale

Learn what colors represent values ​​very helpful to us. We can do this by adding the color bar.

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

img = mpimg.imread('images/mitaomao.jpg')
imgplot = plt.imshow(img)
plt.colorbar()
plt.show()

Here Insert Picture Description

4. array interpolation scheme

Or color values ​​of the pixels "should be" in accordance with different mathematical interpolation scheme. This happens in a common scenario is to adjust the image size. The number of pixels will change, but you want the same information. Because pixels are discrete, so there is a lack of space. Interpolation is a way to fill this space. That's when you magnify the image, your image will come out sometimes it looks pixelated reasons. When the difference between the original image and the expanded image is large, the effect is more pronounced. Let us load our image and shrink it. We are actually dropping pixels, leaving only a few pixels. Now, when we draw it, the data is enlarged to the size of your screen. Since the old pixel no longer exists, the computer must draw pixels to fill that space.

We will use Pillow library used to load the image to adjust the image size.

import matplotlib.pyplot as plt
from PIL import Image

img = Image.open('images/mitaomao.jpg')
img.thumbnail((64, 64), Image.ANTIALIAS)  # resizes image in-place
imgplot = plt.imshow(img)
plt.show()

Here Insert Picture Description

Here we use the default interpolation, bilinear, because we do not have to imshow()provide any interpolation parameters.

Let's try some other things:

imgplot = plt.imshow(img, interpolation="bicubic")

Here Insert Picture Description

Bi-cubic interpolation commonly used to enlarge photo - people tend to blur rather than over-pixelated.

Published 66 original articles · won praise 101 · views 30000 +

Guess you like

Origin blog.csdn.net/u010705932/article/details/104464711