A summary of how to read and write images in Python’s image libraries
Recently, I have been studying the visual aspects of deep learning. I often need to write Python code to build a deep learning model. For example, when writing CNN model-related code, we need to use Python image library to read images and carry out a series of image processing work. My most commonly used image library is of course OpencV, very powerful and easy to use, but OpencV also has some pits, if not careful will cause big trouble. Recently I also look at some others writing code, because personal habits are different, their images was in a deep learning to read different image library, from the opencv to PIL skimage have all sorts of libraries, and so on, some of the library to read in photo storage way also is different, if he is not a good summary the mainstream image, speaking, reading and writing, Later to see the code to write code will meet countless pits. This article summarizes some basic usage of the following major Python image libraries and points to note:
- opencv
- PIL(pillow)
- matplotlib.image
- scipy.misc
- skimage
opencv: cv2.imread
Opencv as my most commonly used image processing library, of course, the first introduction, and more comprehensive introduction. There is no doubt that OpencV is the most comprehensive and powerful library of all the image libraries introduced today. If we just want to master an image library, I think OpencV library is certainly the most suitable.
Picture read operation
Import cv2 import numpy as np Default color map, cv2.IMREAD_GRAYSCALE, Imread ('1.jpg') cv2.imshow(' SRC ',img) print(img.shape) # (h,w,c) print(img.size) Print (img.dtype) print(img) cv2.waitKey()Copy the code
Note that OpencV is already reading a numpy matrix of color images (height, width, number of channels). The data type is uint8.
#gray = cv2.imread('1.jpg', cv2.imread_grayscale) #gray = cv2.imshow('gray',gray) #gray = cv2.imread('1.jpg', cv2.imread_grayscale) Imread ('1.jpg') Gray = cv2.cvtcolor (SRC, cv2.color_bgr2gray) cv2.imshow('gray',gray) print(gry.shape) print(gray.size) print(gray) cv2.waitKey()Copy the code
The above mentioned two ways to obtain grayscale image, the matrix format of the grayscale image read in is (height, width).
Print (img2) None img2 = cv2.imread('2.jpg') print(img2)Copy the code
# How to solve "the problem of reading pictures that do not exist"? Img2 = cv2.imread('2.jpg') if img2 == None: print('fail to load image! ')Copy the code
Picture matrix transformation
Opencv reads images in the matrix format :(height,width,channels). In deep learning, because convolution is applied to different channels, another approach is adopted: channels,height,width. In response to the request, we can do this
#注意到,opencv读入的图片的彩色图是一个channel last的三维矩阵(h,w,c),即(高度,宽度,通道)
#有时候在深度学习中用到的的图片矩阵形式可能是channel first,那我们可以这样转一下
print(img.shape)
img = img.transpose(2,0,1)
print(img.shape)
Copy the code
In the deep learning structures, CNN, often to do the corresponding image data processing, such as images to extended dimensions, such as expanding into (batch_size, channels, height, width).
So for this kind of requirement, we can do this.
Img = np.expand_dims(img, axis=0) print(img.shape) img = np.expand_dims(img, axis=0) print(img.shape) img = np.expand_dims(img, axis=0) print(img.shape)Copy the code
Mentioned above is an extension of the prediction stage prediction with a single image dimensions of operations, if is the stage of training, building a batch, namely conclude this form (batch_size, channels, height, width). I usually like to do that
data_list = []
loop:
im = cv2.imread('xxx.png')
data_list.append(im)
data_arr = np.array(data_list)
Copy the code
So that’s what we want to do.
Image normalization
# Since OpencV reads the image matrix values from 0 to 255, Img3 = cv2.imread('1.jpg') img3 = img3.astype("float") / 255.0 # print(img3)Copy the code
Store image
Cv2. imwrite('test1.jpg',img3) # get all black image because we normalized it # So to get visual image, Img3 = img3 *255 cv2.imwrite('test2.jpg',img3) #Copy the code
Opencv pit BGR
Opencv channels BGR for reading images, not the mainstream RGB! Keep in mind!
Img4 = cv2.imread('1.jpg') img4 = cv2.cvtColor(img4, cv2.color_bgr2rgb)Copy the code
Access to the pixel
Img4 [10 10] # 3Channels print(gray[10 10]) # 1Channel img4[10 10] = [255,255,255] Gray [10 10] = 255 Print (img4 [10, 10]) # 3 channels print (gray [10, 10]) # 1 channelCopy the code
ROI operation
Img4 [200:550,100:450,:] cv2.imshow(' ROI ', ROI) cv2.waitKey()Copy the code
Channel operation
Img5 = cv2.imread('1.jpg') b,g,r = cv2.split(img5) # merge channel img5 = cv2.merge((b,g,r)) # # set all red channel values to 0Copy the code
PIL: PIL. Image. Open
Read the picture
from PIL import Image
import numpy as np
Copy the code
PIL, or The Python Imaging Library, or Pillow, is a popular image Library that is lighter than OpencV, which is why it is so popular.
Image read and write
PIL reads in an image that is an object, not a numpy matrix as we know it.
Img = image.open ('1.jpg') print(img.format) print(img.size) # RGB for true color,RGBA for transparent channel img.show() # show imagesCopy the code
Gray map acquisition
gray = Image.open('1.jpg').convert('L')
gray.show()
Copy the code
Try: img2 = image.open ('2.jpg') except IOError: print('fail to load Image! ')Copy the code
Channel last arr = np.array(img3) print(arr.shape) print(arr.dType) print(arr) print(arr)Copy the code
The transformation of grayscale image is the same as that of color image
arr_gray = np.array(gray)
print(arr_gray.shape)
print(arr_gray.dtype)
print(arr_gray)
Copy the code
Store image
New_im = image.fromarray (arr) new_im.save('3.png')Copy the code
Image manipulation
R, g, b = img.split() img = image.merge ("RGB", (b, g, r)Copy the code
Img = img.copy() #Copy the code
ROI for
Img3 = image.open ('1.jpg') ROI = img3.crop((0,0,300,300)) #Copy the code
Matplotlib: matplotlib. Image. Imread
Matplotlib is a scientific drawing artifact used by many people.
import matplotlib.pyplot as plt
import numpy as np
Copy the code
image = plt.imread('1.jpg')
plt.imshow(image)
plt.show()
Copy the code
Image = plt.imread('1.jpg') plt.imshow(image) plt.axis('off') plt.show()Copy the code
#plt.imread reads a matrix, just like OpencV, but color reads RGB, Print (image.shape) # (h,w,c) print(image.size) print(image.dtype) print(image) print(image)Copy the code
Im_r = image[:,:,0] # red channel plt.imshow(im_r) plt.show() # Plt.imshow (im_r,cmap='Greys_r') plt.show()Copy the code
Import cv2 im2 = cv2.imread('1.jpg') plt.imshow(im2) plt.axis('off') plt.show() # import cv2 im2 = cv2.imread('1.jpg') plt.imshow() Im2 = cv2.cvtcolor (im2, cv2.color_bgr2rgb) plt.imshow(im2) plt.axis('off') plt.show() # so no matter what library is used to read the image, just change the image to a matrix and matplotlib can handle itCopy the code
From PIL import Image im3 = image.open ('1.jpg') im3 = np.array(im3) plt.figure(1) Plt.imshow (im3) plt.axis('off') # Store images, note that the savefig must be before show, otherwise the stored images will be blank plt.savefig('timo.jpg') plt.show()Copy the code
Im_lol1 = plt.imread(' llo.jpg ') im_lol2 = plt.imread('1.jpg') figure = Plt. figure(figsize=(20,10)) # "figsize" parameter: specify the width and height of the drawing object in inches; The DPI parameter specifies the resolution of the drawing object, that is, pixels per inch. The default value is 80. So the chart window created in this example is 8*80 = 640 pixels wide plt.axis("off")# No scale displayed ax = figures.add_subplot (121) # Images are displayed as 1 row 2 columns plt.axis('off') Ax.set_title ('lol image 1')# Add titile ax = figure.add_subplot(122) plt.axis('off') Ax.imshow (im_lol2) ax.set_title('lol image 2')# titile plt.savefig('twp.jpg') plt.show()Copy the code
Scipy. Misc: scipy. Misc. Imread
from scipy import misc
import matplotlib.pyplot as plt
Copy the code
im = misc.imread('1.jpg')
print(im.dtype)
print(im.size)
print(im.shape)
misc.imsave('misc1.png',im)
plt.imshow(im)
plt.show()
print(im)
Copy the code
As you can see, warining indicates that imread and imsave will be deprecated in later versions and tells us to use imageio.imread and imageio.imwrite.
We use the imageio module to read and write images, and the warning is gone.
import imageio im2 = imageio.imread('1.jpg') print(im2.dtype) print(im2.size) print(im2.shape) plt.imshow(im) plt.show() print(im2) imageio.imsave('imageio.png',im2)Copy the code
Skimage: skimage. IO. Imread
From skimage import IO im = IO. Imread ('1.jpg') print(im.shape) # (h,w,c) print(im.dtype) print(im.size) io.imshow(im) io.imsave('sk.png',im) print(im)Copy the code
Images are also read in numpy array form.
Obtaining method of gray map:
Im2 = IO. Imread ('1.jpg',as_grey=True) print(im2.dtype) print(im2.size) print(im2.shape) IO. Imshow (im2) io.imsave('sk_gray.png',im2) io.show() print(im2)Copy the code
It can be seen that the value of the matrix of the gray image is normalized, pay attention!
Grayscale images can also be obtained in this way:
from skimage import color
im3 = io.imread('1.jpg')
im3 = color.rgb2grey(im3)
print(im3.dtype)
print(im3.size)
print(im3.shape)
io.imshow(im3)
io.show()
'''
skimage.color.rgb2grey(rgb)
skimage.color.rgb2hsv(rgb)
skimage.color.rgb2lab(rgb)
skimage.color.gray2rgb(image)
skimage.color.hsv2rgb(hsv)
skimage.color.lab2rgb(lab)
'''
Copy the code
conclusion
- Except for color images read by OpencV, which are stored in BGR order, all image library color images read by OpencV are stored in RGB.
- PIL reads images in img class, other libraries read images in NUMpy matrix.
- The performance of each major image library, the big brother should be OpencV, whether it is speed or the comprehensiveness of picture operation, belong to the existence of crushing, after all, he is a huge CV library. The graph below is a performance comparison of the major image libraries that I stole from Zhihu, and judging from the test results, OpencV did win by a wide margin.