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, MATPLOTLIB1. Show pictures
ImportMatplotlib.pyplot as Plt#plt for displaying picturesImportMatplotlib.image as Mpimg#mpimg for reading picturesImportNumPy as NPLena= Mpimg.imread ('Lena.png')#Lena.png of Reading and code in the same directory#at this time Lena is already a np.array, you can handle it arbitrarilyLena.shape#(3)Plt.imshow (Lena)#Show PicturesPlt.axis ('off')#do not display axesPlt.show ()2. Show a channel
# display the first channel of a picture lena_1 = lena[:,:,0]plt.imshow ('lena_1') plt.show () # At this point you will find that the thermal map is displayed, not our expected grayscale image, 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= Rgb2gray (Lena) # can also be used Plt.imshow (Gray, CMap = 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 Import # The second parameter, if it is an integer, is a percentage, or, if it is a tuple, the size of the output image 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 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'# is automatically appended with the saved name. NPY= np.load (' lena_new_sz.npy'# Read the previously saved array
Second, PIL 1. Show pictures
from Import = Image.open ('lena.png') im.show ()
2. Convert PIL image image to NumPy array
Im_array = Np.array (IM)
# can also be used 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 Import = 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 Mpimg from Import = mpimg.imread ('lena.png'# The data read in here is float32 type, the range is 0-1im = Image.fromarray (Np.uinit8 (lena*255)) im.show ()
Two ways Python reads and displays pictures