【问题标题】:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte when reading an imageUnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte when reading an image
【发布时间】:2017-05-05 10:33:14
【问题描述】:

当我尝试如下读取图像时

with open('/home/image/482_PARTERRE_55682.jpg', 'r') as f:
    imagebin=f.read()

我得到以下错误:

Traceback (most recent call last):
  File "<input>", line 2, in <module>
  File "/home/vincent/anaconda3/envs/my_env/lib/python3.5/codecs.py", line 321, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

我不明白我的错误。

【问题讨论】:

    标签: image file python-3.5 filepath


    【解决方案1】:

    您应该以二进制模式打开图像,即“rb”

    >>> from PIL import Image
    >>> fh = open(".sample.jpg", 'rb')
    >>> img = Image.open(fh)
    

    【讨论】:

      【解决方案2】:

      使用 open('/home/image/482_PARTERRE_55682.jpg', 'rb') 作为 f: imagebin=f.read()

      【讨论】:

        猜你喜欢
        • 2020-01-31
        • 1970-01-01
        • 2021-11-24
        • 2022-09-26
        • 2020-12-26
        • 1970-01-01
        • 2018-10-15
        • 1970-01-01
        • 2014-09-27
        相关资源
        最近更新 更多