Four common methods of python image access record

1. PIL (Python Imaging Library)#pythonimage processing library

from PIL import Image 
import numpy as np 					#数值计算扩展包

I = Image.open("1.jpg") 			#读入图片
I.show()                         	#显示图片
print(I)                            #看看PIL读入后是什么格式 
I_array = np.array(I)               #转成数组形式
print(I_array.shape)
I.save("2.jpg")      				#此处应注意保存的对象是PIL读入的格式而不是转换后的数组

Running results: Insert picture description here
Insert picture description here
two, matplotlib #python画库

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np

I = mpimg.imread("1.jpg")       
print(I.shape)               #从运行输出看在读入图片时已经存为array格式

plt.imshow(I)                #任何有关图像的操作前都需要此函数对array数据进行预处理
						 	 #如果没有这个处理,下面显示或者保存的图像都是空白的
plt.savefig("2.jpg")		 #保存为图像
plt.show()     			   	 #显示图像

Running results: Insert picture description hereInsert picture description here
Three, Scipy #A scientific computing expansion package

import scipy.misc

I = scipy.misc.imread("1.jpg")	#读取图片
print(I.shape)					#也是array格式
								#显示图像可以用是上面的方法
scipy.misc.imsave("2.jpg", I)	#保存图片

Operation result:
Insert picture description here
Four, OpenCV-python #Call OpenCV API

import cv2 as cv          
I = cv.imread("1.jpg")

cv.namedWindow("1",cv.WINDOW_AUTOSIZE) #新开个窗口,自动适应大小
cv.imshow("1",I)
cv.imwrite("2.jpg", I)  #保存图片
cv.waitKey(0)           #在delaytime时间内,按键盘, 返回所按键的ASCII值;若未在
	                    #delaytime时间内按任何键, 返回-1; 其中,dalaytime: 单位ms;当            
	                    #delaytime为0时,表示forever,永不退回.
cv.destroyAllWindows()  #这句好像应该有,但目前我不知道啥用

#安装openc库时一定要注意不是python-opencv...     pip install opencv-python


operation result:Insert picture description here

Guess you like

Origin blog.csdn.net/qq_41872271/article/details/104856337