Python implements two ways to read and display pictures

Source: Internet
Author: User
In Python, in addition to using OPENCV, you can also use the matplotlib and PIL the two libraries to manipulate the picture. I prefer matpoltlib, because its syntax is more like MATLAB.

First, Matplotlib

1. Show pictures

Import Matplotlib.pyplot as Plt # PLT for displaying pictures import matplotlib.image as mpimg # mpimg for reading pictures import NumPy as np Lena = Mpimg . Imread (' lena.png ') # Read and code in the same directory lena.png# at this time Lena is already a np.array, you can handle it arbitrarily Lena.shape # (, 3) plt.imshow (len A) # show Picture Plt.axis (' off ') # does not display axis plt.show ()


2. Show a channel

# Display the first channel of the picture lena_1 = Lena[:,:,0]plt.imshow (' lena_1 ') plt.show () # At this point you will find that the thermal map is displayed, not the grayscale image we expected, you can add the CMap parameter, There are several ways to add: Plt.imshow (' lena_1 ', cmap= ' Greys_r ') plt.show () img = plt.imshow (' lena_1 ') img.set_cmap (' Gray ') # ' hot ' Is the Heat map plt.show ()

3. Convert RGB to Grayscale

There is no proper function in matplotlib to convert an RGB graph to a grayscale graph, which can be customized according to the formula:

def rgb2gray (RGB):  return Np.dot (Rgb[...,:3], [0.299, 0.587, 0.114]) Gray = Rgb2gray (Lena) # can also be used with plt.imshow (Gray, CMA p = plt.get_cmap (' Gray ')) plt.imshow (Gray, cmap= ' Greys_r ') plt.axis (' Off ') plt.show ()

4. Zoom in on the image

We're going to use scipy here.

From scipy Import MISCLENA_NEW_SZ = Misc.imresize (Lena, 0.5) # The second argument is a percentage if it is an integer, or the size of the output image if it is a tuple plt.imshow (lena_new_ SZ) Plt.axis (' Off ') plt.show ()

5. Save the image

5.1 Save the image drawn by matplotlib

This method is suitable for saving any matplotlib-drawn image, which is equivalent to a screencapture.

Plt.imshow (LENA_NEW_SZ) plt.axis (' Off ') plt.savefig (' Lena_new_sz.png ')

5.2 Saving an array as an image

From scipy import miscmisc.imsave (' Lena_new_sz.png ', LENA_NEW_SZ)

5.3 Save Array directly

After reading, the image can be displayed according to the previous array method, which does not lose the image quality at all

Np.save (' LENA_NEW_SZ ', LENA_NEW_SZ) # will be added automatically after the saved name. npyimg = Np.load (' lena_new_sz.npy ') # Read the previously saved array

Second, PIL

1. Show pictures

From PIL Import Imageim = Image.open (' lena.png ') im.show ()

2. Convert PIL image image to NumPy array

Im_array = Np.array (IM) # can also be used with the np.asarray (IM) difference is np.array () is a deep copy, Np.asarray () is a shallow copy

3. Save PIL Pictures

Call the Save method of the Image class directly

From PIL Import imagei = Image.open (' lena.png ') i.save (' New_lena.png ')

4. Convert an NumPy array to a PIL picture

Here the matplotlib.image is read into the image array, note that the array read in here is float32 type, the range is 0-1, and PIL. The Image data is of type UINIT8, the range is 0-255, so the conversion is done:

Import Matplotlib.image as Mpimgfrom PIL import Imagelena = Mpimg.imread (' lena.png ') # The data read in here is float32 type, the range is 0-1im = Imag E.fromarray (Np.uinit8 (lena*255)) Im.show ()

5. Convert RGB to Grayscale

From PIL Import imagei = Image.open (' lena.png ') i.show () L = I.convert (' l ') l.show ()

The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we have a lot of support topic.alibabacloud.com.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.