程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Two methods of reading and displaying pictures in Python

編輯:Python

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.

One 、matplotlib

1. display picture

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()

2. Show a channel

# 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()

3. take RGB Turn to grayscale

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()

4. Zoom in and out of the image

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. Save image

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 

Two 、PIL

1. display picture

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

2. take PIL Image Convert the picture to numpy Array

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 

3. preservation PIL picture

Call directly Image Class save Method

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

4. take numpy Array to PIL picture

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()

5. RGB Convert to grayscale

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

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved