python - Python:将图像转换为像素列表的反向?
问题描述
im = Image.open('segmentace_zkusebni_pi.jpg')
pixel = list(im.getdata())
获取图像中每个像素的 RGB 列表。对于我的论文(不是编程,数学),在我对像素的 RGB 值进行一些计算之后,我还需要反转这个过程。所以我需要将 3 元素向量(可变像素)列表转换为图像。有什么办法吗?非常感谢
解决方案
import numpy as np
im = Image.open('segmentace_zkusebni_pi.jpg')
n = np.asarray(im)
shape = n.shape
pixel = list(im.getdata())
# pixel = n.reshape((shape[0]*shape[1], shape[2])).tolist()
# do your work, but make sure the image dimension is kept same
# preferably you should work with numpy array itself
..
..
im2 = Image.fromarray((np.array(pixel, dtype='uint8')).reshape(shape))
im2.show()