如何将PIL图像转换为字节格式,这与普通字节格式相似

时间:2018-09-19 05:53:46

标签: python image-processing

抱歉,我无法在主题中清楚地说明。 我使用read()以字节形式读取整个图像,也使用PIL的tobytes()读取同一图像。但是对我来说,图像字节看起来不一样。您能否建议如何使用PIL的package实用程序使用read()生成相同的字节?从原始编码到utf-8

代码示例:

path3 = r'path'
with io.open(path3, 'rb') as image_file:
    content1 = image_file.read()
b'\xff\xd8\x ...

使用PIL:

with io.open(path3, 'rb') as image_file:
    content1 = Image.open(image_file).tobytes()
b'\xbf\x91\xc0\xbf\x91\xc0\xbe\x90\xbf\xbe'

在我的用例中:

from pdf2image import convert_from_bytes
images = convert_from_bytes(open('pp.pdf', 'rb').read())
b=images[0].read() # since this returns list format
AttributeError: 'PpmImageFile' object has no attribute 'read'

是否可能具有与read()相同的字节格式?

1 个答案:

答案 0 :(得分:0)

PIL所做的不仅仅是读取图像文件的字节。它正在从JPG或PNG或您提供的任何格式解压缩。它的tobytes函数返回所有像素值。

在第一个代码段中,您只是读取压缩图像文件的字节。除非您使用的是BMP之类的未压缩文件格式,否则它们总是不同的。