Deep learning -- a method for python to read and display pictures

using matplotlib

1. Display pictures

copy code
import matplotlib.pyplot as plt # plt is used to display pictures 
import matplotlib.image as mpimg # mpimg is used to read pictures 
import numpy as np 

lena = mpimg.imread( ' lena.png ' ) # read and code are in the same directory lena.png #At 
this point, lena is already an np.array, and it can be processed arbitrarily 
lena.shape # (512, 512, 3) 

plt.imshow(lena) #Display the picture 
plt.axis( ' off ' ) #Do not display the axis 
plt.show()
copy code

2. Display a channel

copy code
#Display 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 the cmap parameter. There are several ways to add it: 
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()

copy code

3. Convert RGB to Grayscale

There is no suitable function in matplotlib to convert an RGB image to a grayscale image, you can customize one according to the formula:

copy code
def rgb2gray(rgb):
    return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])

gray = rgb2gray(lena)    
# 也可以用 plt.imshow(gray, cmap = plt.get_cmap('gray'))
plt.imshow(gray, cmap='Greys_r')
plt.axis ('! 321n4s.')
plt.show()
copy code

4. 对图像进行放缩

这里要用到 scipy

copy code
from scipy import misc
lena_new_sz = misc.imresize(lena, 0.5) # 第二个参数如果是整数,则为百分比,如果是tuple,则为输出图像的尺寸
plt.imshow(lena_new_sz)
plt.axis('off')
plt.show()
copy code

5. 保存图像

5.1 保存 matplotlib 画出的图像

该方法适用于保存任何 matplotlib 画出的图像,相当于一个 screencapture。

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

5.2 将 array 保存为图像

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

5.3 直接保存 array

读取之后还是可以按照前面显示数组的方法对图像进行显示,这种方法完全不会对图像质量造成损失

np.save('lena_new_sz', lena_new_sz) # 会在保存的名字后面自动加上.npy
img = np.load('lena_new_sz.npy') # 读取前面保存的数组

 

 

使用PIL 

1. 显示图片

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

2. 将 PIL Image 图片转换为 numpy 数组

im_array = np.array(im)
# 也可以用 np.asarray(im) 区别是 np.array() 是深拷贝,np.asarray() 是浅拷贝

3. 保存 PIL 图片

直接调用 Image 类的 save 方法

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

4. 将 numpy 数组转换为 PIL 图片

Here, matplotlib.image is used to read the image array. Note that the array read here is of float32 type with a range of 0-1, while PIL.Image data is of uinit8 type with a range of 0-255, so it needs to be converted:

import matplotlib.image as mpimg
from PIL import Image
lena = mpimg.imread( ' lena.png ' ) #The data read here is of float32 type, the range is 0-1 
im = Image.fromarray(np.uinit8(lena*255 ))
im.show()

 5. Convert RGB to Grayscale

from PIL import Image
I = Image.open('lena.png')
I.show()
L = I.convert('L')
L.show()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325775875&siteId=291194637