使用PIL模块出现UnicodeDecodeError错误

4

I am getting this error message:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

从这段代码开始:

from PIL import Image
import os

image_name = "mypic"
count = 0

for filename in os.listdir('pictures'):
    if filename.endswith('.jpg'):
        image_file = open('pictures/' +filename)
        image = Image.open(image_file)

        # next 3 lines strip exif
        data = list(image.getdata())
        image_without_exif = Image.new(image.mode, image.size)
        image_without_exif.putdata(data)

        image_without_exif.save('new-pictures/' + image_name + str(count) + '.jpg')
        count = count + 1;

不太清楚为什么,因为昨天这还能用...

3个回答

3
这是因为open试图将文件作为文本来读取。您可以通过直接使用Image.open()打开路径来解决这个问题。
img = Image.open('pictures/' + filename)

这是因为PIL会在内部为您处理相关操作;请查看此处的文档以获取更多信息:
https://pillow.readthedocs.io/en/latest/reference/Image.html#PIL.Image.open 另外,使用Image.open作为上下文管理器来打开和关闭图像可能更有意义(这里有一个很好的解释)。
with Image.open('pictures/' + filename) as img:
    # process img
# image file closed now after leaving context scope

3

我认为你需要以二进制模式打开文件:

image_file = open('pictures/' +filename, 'rb')

如果需要直接操作文件(例如隐写术),这种方法也可以使用,并且可能是更好的路线。同样,PIL.Image将接受任何类似于字节的对象(例如BytesIO),该对象“必须实现read()、seek()和tell()方法,并以二进制模式打开”文档 - ti7

0

当使用open(filename)函数而没有任何其他参数时,您会以“文本”模式打开文件。

Python将在读取文件时假定文件包含文本。当它找到一个值为255(0xFF)的字节时,会感到困惑,因为没有文本字符与该字节匹配。

要解决这个问题,请以字节模式打开文件:

open(filename, "b")

这告诉Python不要假设它包含文本,文件句柄将只提供原始字节。

因为这是一个常见的用例,PIL已经内置了通过文件名打开图像的功能:

Image.open(filename)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接