python implements two methods for reading and displaying images

  • 2020-05-19 05:13:51
  • OfStack

In addition to opencv, you can use matplotlib and PIL libraries to manipulate images in python. I prefer matpoltlib because its syntax is more like matlab.

1. matplotlib

1. Show pictures


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') #  The read is the same as the code 1 In the directory  lena.png
#  At this time  lena  Has been a 1 a  np.array  I can do whatever I want with it 
lena.shape #(512, 512, 3)

plt.imshow(lena) #  Display images 
plt.axis('off') #  No axes are displayed 
plt.show()

2. Display a channel


#  Show the number of the picture 1 A channel 
lena_1 = lena[:,:,0]
plt.imshow('lena_1')
plt.show()
#  Now you can see that what you're showing is a heat map, not the gray scale that we expected, and you can add it  cmap  Parameter can be added in the following ways: 
plt.imshow('lena_1', cmap='Greys_r')
plt.show()

img = plt.imshow('lena_1')
img.set_cmap('gray') # 'hot'  Is the heat figure 
plt.show()

3. Convert RGB to grayscale

There is no appropriate function in matplotlib to convert the RGB graph to grayscale, and one can be customized 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. Put and shrink the image

I'm going to use scipy here


from scipy import misc
lena_new_sz = misc.imresize(lena, 0.5) #  The first 2 If it is an integer, it is a percentage, if it is tuple , is 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 image drawn by matplotlib, equivalent to one screencapture.


plt.imshow(lena_new_sz)
plt.axis('off')
plt.savefig('lena_new_sz.png')

5.2 save array as an image


from scipy import misc
misc.imsave('lena_new_sz.png', lena_new_sz)

5.3 save array directly

After reading, the image can still be displayed in accordance with the previous array display method, this method will not cause a loss of image quality


np.save('lena_new_sz', lena_new_sz) #  Will be automatically added after the saved name .npy
img = np.load('lena_new_sz.npy') #  Read the previously saved array 

2. PIL

1. Show pictures


from PIL import Image
im = Image.open('lena.png')
im.show()

2. Convert PIL Image images into numpy array


im_array = np.array(im)
#  You can also use  np.asarray(im)  The difference is that  np.array()  It's a deep copy, np.asarray()  Is a shallow copy 

3. Save the PIL image

Directly call the save method of the Image class


from PIL import Image
I = Image.open('lena.png')
I.save('new_lena.png')

4. Convert numpy array to PIL image

Here, matplotlib.image is used to read into the image array. Notice that the array read into here is float32, with a range of 0-1, while PIL.Image is uinit8, with a range of 0-255.


#  Show the number of the picture 1 A channel 
lena_1 = lena[:,:,0]
plt.imshow('lena_1')
plt.show()
#  Now you can see that what you're showing is a heat map, not the gray scale that we expected, and you can add it  cmap  Parameter can be added in the following ways: 
plt.imshow('lena_1', cmap='Greys_r')
plt.show()

img = plt.imshow('lena_1')
img.set_cmap('gray') # 'hot'  Is the heat figure 
plt.show()
0

5. Convert RGB to grayscale


#  Show the number of the picture 1 A channel 
lena_1 = lena[:,:,0]
plt.imshow('lena_1')
plt.show()
#  Now you can see that what you're showing is a heat map, not the gray scale that we expected, and you can add it  cmap  Parameter can be added in the following ways: 
plt.imshow('lena_1', cmap='Greys_r')
plt.show()

img = plt.imshow('lena_1')
img.set_cmap('gray') # 'hot'  Is the heat figure 
plt.show()
1

Related articles: