我一直在做一个项目,需要我的代码获取图像的像素数据并重建它(以另一种形式),但是当我尝试使用像素数据创建图像时,结果发现所有像素都在同一行上
这是我的密码
pixels = (image.getdata())
img_array = np.array(pixels, dtype=np.uint8)
img = Image.fromarray(img_array)
img.save('testrgb.png')
如何逐行读取图像的像素,并根据它们的行将它们排列在不同的列表中
编辑通过使用以下代码,我能够获得所需的结果
pixels = list(image.getdata())
print(pixels[0][0])
pixels2 = []
for i in range(0, height):
pixels2.append(pixels[i * width:(i + 1) * width])
#for pixel_value in pixels:
#print(pixel_value)
img_array = array = np.array(pixels2, dtype=np.uint8)
img = Image.fromarray(array)
img.save('maps/testrgb.png')
从关于^{} 的文件中:
如果您想要实际图像的NumPy数组表示,请直接使用
image
上的np.array
:希望有帮助
相关问题 更多 >
编程相关推荐