【发布时间】:2015-05-21 20:37:05
【问题描述】:
我开始使用 Pillow for image-processing,我需要使用单独的像素。
最后我想从字节创建图像。
所以我使用PIL.Image.frombytes(mode, size, data, decoder_name='raw', *args)函数。
您能否建议数据应该是什么?
我认为它应该是 RGB 模式的 R1G1B1R2G2B2R3G3B3...RnGnBn 之类的字节字符串。
但我对以下内容有点困惑:
from PIL import Image
def image_create_load_compare(initial_data):
img = Image.frombytes('RGB', (4, 1), initial_data)
img.save("temp.jpg")
img_loaded = Image.open("temp.jpg")
data_loaded = img_loaded.tobytes()
print("initial_data: " + str(initial_data))
print("data_loaded: " + str(data_loaded))
print("is initial and loaded data equal: " + str(initial_data == data_loaded))
print("="*30)
# 4 black pixels
black_bytes = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
image_create_load_compare(black_bytes)
# 4 white pixels
white_bytes = b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'
image_create_load_compare(white_bytes)
# 1 red pixel, 3 white pixels
red_white_bytes = b'\xff\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff'
image_create_load_compare(red_white_bytes)
# 1 red pixel, 3 black pixels
red_black_bytes = b'\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
image_create_load_compare(red_black_bytes)
还有输出:
initial_data: b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
data_loaded: b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
is initial and loaded data equal: True
==============================
initial_data: b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'
data_loaded: b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'
is initial and loaded data equal: True
==============================
initial_data: b'\xff\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff'
data_loaded: b'\xda\x11\x07\xff\xe2\xe3\xf4\xfd\xff\xe0\xff\xff'
is initial and loaded data equal: False
==============================
initial_data: b'\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
data_loaded: b'\xda\x11\x07E\x00\x00\x00\x05\x16\x00\n\x1e'
is initial and loaded data equal: False
==============================
当我检查创建的文件时,带有一个红色像素和三个白色/黑色像素的图像看起来像是从红色到白色或黑色的渐变。 为什么不同颜色文件的像素与预期不同?
【问题讨论】:
标签: python image-processing pillow