当前位置 : 主页 > 网页制作 > HTTP/TCP >

PIL对象和numpy三维数组的互相转换

来源:互联网 收集:自由互联 发布时间:2021-06-16
from PIL import Image import numpy as npimg1 =Image.open( " f:\cat.jpg " ) # pix_data=np.array(img1.getdata()) # print(pix_data) # np.savetxt("f:\pix_data.txt",pix_data) pix_data2 =np.array(img1.getdata()).reshape(img1.size[0],img1.size[1],
from PIL import Image
import numpy as np
img1=Image.open("f:\cat.jpg")

#pix_data=np.array(img1.getdata())
#print(pix_data)
#np.savetxt("f:\pix_data.txt",pix_data)

pix_data2=np.array(img1.getdata()).reshape(img1.size[0],img1.size[1],3)

pix_data3=pix_data2[0:200,0:,0:2]

im=Image.fromarray(np.uint8(pix_data3))

im.show()

参考:https://stackoverflow.com/questions/384759/how-to-convert-a-pil-image-into-a-numpy-array

网友评论