Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过枕头创建的图像';s Image.frombytes与预期值不同_Python_Image Processing_Pillow - Fatal编程技术网

Python 通过枕头创建的图像';s Image.frombytes与预期值不同

Python 通过枕头创建的图像';s Image.frombytes与预期值不同,python,image-processing,pillow,Python,Image Processing,Pillow,我开始使用枕头进行图像处理,我需要处理不同的像素 最后,我想从字节创建图像 所以我使用了PIL.Image.frombytes(模式、大小、数据、解码器\u name='raw',*args)函数 你能告诉我应该是什么样的数据吗 我认为它应该是字节字符串,就像RGB模式的R1G1B1R2G2R2R3G3B3…RnGnBn 但我对以下几点有点困惑: from PIL import Image def image_create_load_compare(initial_data): img

我开始使用枕头进行图像处理,我需要处理不同的像素

最后,我想从字节创建图像

所以我使用了
PIL.Image.frombytes(模式、大小、数据、解码器\u name='raw',*args)
函数

你能告诉我应该是什么样的数据吗

我认为它应该是字节字符串,就像RGB模式的R1G1B1R2G2R2R3G3B3…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
==============================
当我检查创建的文件时,具有一个红色像素和三个白色/黑色像素的图像看起来像是从红色到白色或黑色的渐变。
为什么不同颜色文件中的像素与预期不同?

JPEG是一种有损格式。为了减少文件大小,它将改变像素的颜色,使其更可压缩

尝试以无损格式保存临时文件,如png

img.save("temp.png")

img_loaded = Image.open("temp.png")
现在您的测试应该全部输出
True

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'\xff\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff'
is initial and loaded data equal: True
==============================
initial_data: b'\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
data_loaded:  b'\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
is initial and loaded data equal: True
==============================