我想从NumPy数组创建一个PIL图像。以下是我的尝试:
# Create a NumPy array, which has four elements. The top-left should be pure red, the top-right should be pure blue, the bottom-left should be pure green, and the bottom-right should be yellow
pixels = np.array([[[255, 0, 0], [0, 255, 0]], [[0, 0, 255], [255, 255, 0]]])
# Create a PIL image from the NumPy array
image = Image.fromarray(pixels, 'RGB')
# Print out the pixel values
print image.getpixel((0, 0))
print image.getpixel((0, 1))
print image.getpixel((1, 0))
print image.getpixel((1, 1))
# Save the image
image.save('image.png')
但是,打印出来的内容如下:
(255, 0, 0)
(0, 0, 0)
(0, 0, 0)
(0, 0, 0)
保存的图像左上角为纯红色,但其他像素均为黑色。为什么其他像素没有保留我在NumPy数组中分配给它们的颜色?
谢谢!
RGB
模式应为8位值,因此只要转换数组就可以解决问题:相关问题 更多 >
编程相关推荐