Pillow: UnicodeDecodeError when opening image python3

Created on 21 Dec 2015  路  6Comments  路  Source: python-pillow/Pillow

Steps to reproduce:

>>> from PIL import Image
>>> f = open(".wallpaper.jpg")
>>> img = Image.open(f)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python3.4/site-packages/PIL/Image.py", line 2256, in open
    prefix = fp.read(16)
  File "/usr/lib64/python3.4/codecs.py", line 319, 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
PILLOW_VERSION = '3.0.0'
VERSION = '1.1.7'

Affects a few images tested.
Works fine in python2 and I've tried reinstalling python3-pillow from package manager (dnf, fedora 23).

Anything I can do to help debug?

Image attached, just in case.
.wallpaper.jpg

Most helpful comment

You need to open the file in binary mode:

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

All 6 comments

Possibly related, I get the following error if I try to install with pip or pip3:
ValueError: --enable-jpeg requested but jpeg not found, aborting.

I have openjpeg-devel and turbojpeg-devel installed. Not sure what it wants.

Unrelated, just needed libjpeg-turbo-devel (of course).

Nope: unrelated. Tested with pip3 pillow and I get the same error.

You need to open the file in binary mode:

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

On Sun, Dec 27, 2015 at 03:30:37AM -0800, wiredfool wrote:

You need to open the file in binary mode:

Thanks!

Thanks a lot for your answer @wiredfool

Saved my day

Thanks a lot @wiredfool!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

anonymous530 picture anonymous530  路  3Comments

hxzhao527 picture hxzhao527  路  4Comments

Larivact picture Larivact  路  4Comments

etc0de picture etc0de  路  4Comments

SysoevDV picture SysoevDV  路  3Comments