This article mainly introduces two methods for reading and displaying images in python. I think this is a good example. I will share it with you and give you a reference. Let's take a look at it together. in addition to using opencv in python, you can also use matplotlib and PIL to operate images. I prefer matpoltlib because its syntax is more like matlab.
I. matplotlib
1. show images
Import matplotlib. pyplot as plt # plt is used to display the image import matplotlib. image as mpimg # mpimg is used to read the image import numpy as np lena = mpimg.imread('lena.png ') # read the lena.png in the same directory as the code # at this time, lena is already an np. array, which can be processed arbitrarily. lena. shape # (512,512, 3) plt. imshow (lena) # display the image plt. axis ('off') # axis plt is not displayed. show ()
2. display a channel
# Lena_1 = lena [:,:, 0] plt. imshow ('Lena _ 1') plt. show () # at this time, we will find that the heat map is displayed, rather than the expected grayscale map. you can add the cmap parameter by using the following methods: 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
Matplotlib does not have a proper function to convert an RGB image to a grayscale image. you can customize one according to the formula:
Def rgb2gray (rgb): return np. dot (rgb [...,: 3], [0.299, 0.587, 0.114]) gray = rgb2gray (lena) # You can also use plt. imshow (gray, cmap = plt. get_cmap ('gray ') plt. imshow (gray, cmap = 'greys _ r') plt. axis ('off') plt. show ()
4. scale down an image
Scipy is used here
From scipy import misclena_new_sz = misc. imresize (lena, 0.5) # percentage if the second parameter is an integer, and tuple is the size plt of the output image. 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 image, which is equivalent to a screencapture.
plt.imshow(lena_new_sz)plt.axis('off')plt.savefig('lena_new_sz.png')
5.2 save the 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, you can still display the image based on the preceding display array. this method will not cause any loss to the image quality.
Np. save ('Lena _ new_sz ', lena_new_sz) #. npyimg = np. load ('Lena _ new_sz.2d-') will be automatically added after the saved name # read the previously saved array
II. PIL
1. show images
from PIL import Imageim = Image.open('lena.png')im.show()
2. convert a PIL Image to a numpy array
Im_array = np. array (im) # You can also use np. asarray (im). The difference is that np. array () is a deep copy, and np. asarray () is a shallow copy.
3. Save the PIL image
Directly call the save method of the Image class
from PIL import ImageI = Image.open('lena.png')I.save('new_lena.png')
4. convert numpy array to PIL image
Matplotlib is used here. the image reads the image array. Note that the array read here is of the float32 type and the range is 0-1, while PIL. image data is of the uinit8 type and ranges from 0 to 255. Therefore, the conversion is required:
Import matplotlib. image as mpimgfrom PIL import Imagelena = mpimg.imread('lena.png ') # The data read here is of the float32 type and the range is 0-1im = Image. 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 all of the content in this article. I hope it will be helpful for everyone's learning and support for PHP's Chinese web.
For more information about how to read and display images in python, refer to the PHP Chinese website!