Summary of reading and writing methods of various image libraries in Python (recommended ),

Source: Internet
Author: User
Tags image processing library

Summary of reading and writing methods of various image libraries in Python (recommended ),

Recently, I have been studying deep learning visual related things. 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 the python image library to read images and perform a series of image processing work. Of course, my most commonly used image library is opencv, which is very powerful and easy to use, but opencv also has some pitfalls. If you don't pay attention to it, it will make a lot of trouble. Recently, I am also reading some code written by others. Because my personal habits are different, they use different image libraries for reading images during deep learning, from opencv to PIL to skimage, and so on, some libraries read different image storage methods. If you do not summarize the features of these mainstream image read/write libraries, there will be countless pitfalls in writing code later. This article summarizes some basic usage methods and notes for the following mainstream Python image libraries:

1. opencv
2. PIL (pillow)
3. matplotlib. image
4. scipy. misc
5. skimage

Opencv: cv2.imread

Opencv is my most commonly used image processing library. Of course, it is the first introduction and comprehensive introduction. Undoubtedly, opencv is the most comprehensive and powerful library in all image libraries introduced today. If we only want to master an image library, I think opencv is definitely the most suitable.

Image read Operations

Import cv2import numpy as np # Read image: default color image, cv2.IMREAD _ GRAYSCALE image, cv2.IMREAD _ UNCHANGED contains alpha channel img = cv2.imread('1.jpg ') cv2.imshow ('src', img) print (img. shape) # (h, w, c) print (img. size) # print (img. dtype) print (img) cv2.waitKey ()

It is worth noting that the image read by opencv is already a numpy matrix, and the color image dimension is (height, width, number of channels ). The data type is uint8.

# Gray = cv2.imread('1.jpg ', cv2.IMREAD _ GRAYSCALE) # GRAYSCALE image # cv2.imshow ('Gray', gray) # You can also write it like this: Read the color image first, then convert the GRAYSCALE image src = cv2.imread('1.jpg ') gray = cv2.cvtColor (src, cv2.COLOR _ BGR2GRAY) cv2.imshow ('Gray ', gray) print (gray. shape) print (gray. size) print (gray) cv2.waitKey ()

The two ways to obtain grayscale images are mentioned above. The matrix format of the read grayscale image is (height, width ).

# Note: The image path calculation is incorrect, and Opencv will not remind you, but the result obtained during print img is Noneimg2 = cv2.imread('2.jpg ') print (img2)

# How can I solve the "the image I read does not exist" problem "? # Add a judgment statement. if it is null, perform Exception Processing. img2 = cv2.imread('2.jpg ') if img2 = None: print ('fail to load image! ')

Image matrix transformation

The matrix format of the image read by opencv is: (height, width, channels ). In deep learning, because convolution is applied to different channels, another method is used: (channels, height, width ). To meet this requirement, we can do this.

# Note that the color chart of the image read by opencv is a channel last 3D matrix (h, w, c), that is (height, width, channel) # Sometimes the image matrix format used in deep learning may be channel first, so we can refer to print (img. shape) img = img. transpose (2, 0, 1) print (img. shape)

When constructing CNN in deep learning, we often need to process the corresponding image data, such as extended dimensions (batch_size, channels, height, width ).

We can do this for this requirement.

# Sometimes we need to expand the dimension. For example, if we need to predict the number of images in a single image and add the number of images in the column, we can do this img = np. expand_dims (img, axis = 0) print (img. shape)

The above mentioned is an operation to predict the extended dimension of a single image during the prediction phase. If it is a training phase, a batch is constructed to obtain this form: (batch_size, channels, height, width ). I generally like this.

data_list = [] loop:  im = cv2.imread('xxx.png')  data_list.append(im)data_arr = np.array(data_list)

In this way, we can construct the form we want.

Image Normalization

# Because the image Matrix Values read by opencv are 0 to 255, sometimes we need to normalize them to 0 ~ 1img3 = cv2.imread('1.jpg ') img3 = img3.astype ("float")/255.0 # note that you must first convert the data type to floatprint (img3.dtype) print (img3)

Store images

Cv2.imwrite('test1.jpg ', img3) # obtain the all-black image, because we normalize it # So to get a visual image, we need to restore img3 = img3 * 255cv2.imwrite('test2.jpg' first ', img3) # You can see the color source image.

Opencv big pitfall: BGR

Opencv arranges the channels of the read images in BGR instead of the mainstream RGB! Remember!

# The Matrix read by opencv is BGR. If you want to convert it to RGB, you can convert it to img4 = cv2.imread('1.jpg ') img4 = cv2.cvtColor (img4, cv2.COLOR _ BGR2RGB)

Access Pixel

# Access pixel print (img4 [10, 10]) #3 channelsprint (gray [10, 10]) #1channelimg4 [255,255,255] = [255] gray [] = print (img4 []) #3 channelsprint (gray []) #1 channel

ROI operations

# Roi operation roi = img4 [,:] cv2.imshow ('roi ', roi) cv2.waitKey ()

Channel operations

# Separation channel img5 = cv2.imread('1.jpg ') B, g, r = cv2.split (img5) # merge channel img5 = cv2.merge (B, g, r )) # You can also not split img5 [:,:, 2] = 0 # Set all red channel values to 0

PIL: PIL. Image. open

Image reading

from PIL import Imageimport numpy as np

PIL is the Python Imaging Library, also known as Pillow. It is a popular image Library, which is lighter than opencv. For this reason, PIL is favored by the masses.

Image read/write

The image read by PIL is an object, not a well-known numpy matrix.

Img = Image.open('1.jpg ') print (img. format) print (img. size) # note that the channel (w, h) print (img. mode) # L is a grayscale image, RGB is a true color, RGBA is added with a transparent channel img. show () # show Images

Grayscale image acquisition

gray = Image.open('1.jpg').convert('L')gray.show()

# If the image cannot be read, an exception IOError will be thrown. We can capture it and handle the exception. try: img2 = Image.open('2.jpg ') failed t IOError: print ('fail to load image! ')

# The image read by pillow is not a matrix. We convert the image to a matrix. channel lastarr = np. array (img3) print (arr. shape) print (arr. dtype) print (arr)

The conversion of grayscale images is the same as that of colored images.

arr_gray = np.array(gray)print(arr_gray.shape)print(arr_gray.dtype)print(arr_gray)

Store images

# Convert the Matrix to image new_im = image.fromarray(arr1_new_im.save('3.png ')

Image operations

# Separation merge Channel r, g, B = img. split () img = Image. merge ("RGB", (B, g, r ))
Img = img. copy () # copy an image

ROI acquisition

Img3 = Image.open('1.jpg ') roi = img3.crop (300,300,) # (top left x, top left y, bottom right x, bottom right y) Coordinate roi. show ()

Matplotlib: matplotlib. image. imread

Matplotlib is a scientific plotting artifact, used by many people.

import matplotlib.pyplot as pltimport numpy as np
image = plt.imread('1.jpg')plt.imshow(image)plt.show()

# You can also disable image = plt.imread('1.jpg ') plt. imshow (image) plt. axis ('off') plt. show () on x and Y axes ()

# Plt. imread reads a matrix, which is the same as opencv, but color graphs read RGB, which is different from opencv in print (image. shape) # (h, w, c) print (image. size) print (image. dtype) print (image)

Im_r = image [:,:, 0] # Red Channel plt. imshow (im_r) 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 to solve the plt. imshow (im_r, cmap = 'greys _ R') plt. show ()

# Use import cv2im2 = cv2.imread('1.jpg ') plt with opencv. imshow (im2) plt. axis ('off') plt. show () # I found that the color of the image is strange. Of course, the reason is that the RGB sequence we mentioned earlier is different. Just turn it on. im2 = cv2.cvtColor (im2, cv2.COLOR _ BGR2RGB) plt. imshow (im2) plt. axis ('off') plt. show () # No matter which library is used to read the image, you only need to change the image to a matrix, then matplotlib can process it.

# Try again pillow and matplotlib in combination with from PIL import Imageim3 = Image.open('1.jpg ') im3 = ') plt. show ()

# Finally, We will summarize matplotlib's most basic image display skills with a comprehensive example. im_lol1 = plt.imread('lol.jpg ') im_lol2 = plt.imread('1.jpg') figure = plt. figure (figsize = (20, 10) # adjust the size of the displayed image ''' figsize parameter: Specify the width and height of the drawing object, in inches. The dpi parameter specifies the resolution of the Drawing Object, the number of pixels per inch. The default value is 80. Therefore, the window width of the chart created in this example is 8*80 = 640 pixels ''' plt. axis ("off") # do not display the scale ax = figure. add_subplot (121) # The image Displays plt in one row and two columns. axis ('off') ax. imshow (im_lol1) # The first figure ax. set_title ('lol image 1') # Add titile ax = figure to the image. add_subplot (122) plt. axis ('off') ax. imshow (im_lol2) ax. set_title ('lol image 2') # Add titile plt.savefig('twp.jpg ') plt to the image. show ()

Scipy. misc: scipy. misc. imread

from scipy import miscimport matplotlib.pyplot as plt
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)

We can see that there is warining, prompting us that imread and imsave will be discarded in later versions, so we can use imageio. imread and imageio. imwrite.

Based on her prompt, we use the imageio module to read and write images, so that warning does not.

import imageioim2 = 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)

Skimage: skimage. io. imread

From skimage import ioim = io.imread('1.jpg ') print (im. shape) # ', im) print (im)

Images are also read in the form of numpy array.

Grayscale image acquisition method:

Im2 = io.imread('1.jpg ', as_grey = True) then', im2) io. show () print (im2)

As you can see, the matrix value of the grayscale image is normalized. Pay attention to this!

You can also obtain the grayscale image in this way:

from skimage import colorim3 = 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)'''

Summary

  1. In addition to color images read by opencv, all other image libraries read color images in RGB format.
  2. In addition to the img class, all images read by other libraries are in the numpy matrix.
  3. The performance of major image libraries, such as opencv, is the existence of compaction in terms of speed and comprehensive image operations. After all, it is a huge dedicated cv library. The figure below shows a comparison of the performance of mainstream image libraries from zhihuiche. From the test results, opencv did win too much.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.