stay python In addition to using opencv, It can also be used. matplotlib and PIL These two libraries operate on images . I prefer matpoltlib, Because its grammar is more like matlab.
import matplotlib.pyplot as plt # plt Used to display pictures
import matplotlib.image as mpimg # mpimg For reading pictures
import numpy as np
lena = mpimg.imread('lena.png') # Read the code in the same directory lena.png
# here lena It's already a np.array 了 , It can be handled arbitrarily
lena.shape #(512, 512, 3)
plt.imshow(lena) # display picture
plt.axis('off') # No axes are displayed
plt.show()
# Show the first channel of the picture
lena_1 = lena[:,:,0]
plt.imshow('lena_1')
plt.show()
# At this point, you will find that the heat map is displayed , Not the grayscale image we expected , You can add cmap Parameters , 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' It's a heat map
plt.show()
matplotlib There is no suitable function in the to put RGB Convert image to 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)
# It can also be used. plt.imshow(gray, cmap = plt.get_cmap('gray'))
plt.imshow(gray, cmap='Greys_r')
plt.axis('off')
plt.show()
We need to use scipy
from scipy import misc
lena_new_sz = misc.imresize(lena, 0.5) # If the second parameter is an integer , Is the percentage , If it is tuple, Is the size of the output image
plt.imshow(lena_new_sz)
plt.axis('off')
plt.show()
5.1 preservation matplotlib The image drawn
This method is suitable for saving any matplotlib The image drawn , Equivalent to one screencapture.
''' No one answers the problems encountered in learning ? Xiaobian created a Python Exchange of learning QQ Group :153708845 Looking for small partners who share the same aspiration , Help each other , There are also good video tutorials and PDF e-book ! '''
plt.imshow(lena_new_sz)
plt.axis('off')
plt.savefig('lena_new_sz.png')
5.2 take array Save as image
from scipy import misc
misc.imsave('lena_new_sz.png', lena_new_sz)
5.3 Save directly array
After reading, you can still display the image according to the method of displaying the array above , This method will not cause complete loss of image quality
np.save('lena_new_sz', lena_new_sz) # Will automatically add... After the saved name .npy
img = np.load('lena_new_sz.npy') # Read the previously saved array
from PIL import Image
im = Image.open('lena.png')
im.show()
im_array = np.array(im)
# It can also be used. np.asarray(im) The difference is that np.array() It's a deep copy ,np.asarray() Is a shallow copy
Call directly Image Class save Method
from PIL import Image
I = Image.open('lena.png')
I.save('new_lena.png')
Here the matplotlib.image Read in image array , Note that the array read in here is float32 Type , The scope is 0-1, and PIL.Image The data is uinit8 Type , The scope is 0-255, So we need to convert :
''' No one answers the problems encountered in learning ? Xiaobian created a Python Exchange of learning QQ Group :153708845 Looking for small partners who share the same aspiration , Help each other , There are also good video tutorials and PDF e-book ! '''
import matplotlib.image as mpimg
from PIL import Image
lena = mpimg.imread('lena.png') # The data read here is float32 Type , The scope is 0-1
im = Image.fromarray(np.uinit8(lena*255))
im.show()
from PIL import Image
I = Image.open('lena.png')
I.show()
L = I.convert('L')
L.show()