简体   繁体   中英

Python mac matplotlib imread error

I'm trying to read a jpg image using matplotlib's pyplot.

This is the error I get when using the imread command:

Traceback (most recent call last):
File "/Users/rugheid/Dropbox/Documents/Rugen Dropbox/School/Universiteit/3e jaar/P&O/Git Repository/backend/image_processing/image_processor.py", line 27, in <module>
img = pyplot.imread(io.BytesIO(jpg_bytes), format='jpg')
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/pyplot.py", line 2177, in imread
return _imread(*args, **kwargs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/image.py", line 1242, in imread
im = pilread(fname)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/image.py", line 1226, in pilread
return pil_to_array(image)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/image.py", line 1328, in pil_to_array
x = toarray(im)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/image.py", line 1313, in toarray
x_str = im.tostring('raw', im.mode)
File "/Library/Python/2.7/site-packages/PIL/Image.py", line 695, in tostring
"Please call tobytes() instead.")
Exception: tostring() has been removed. Please call tobytes() instead.

It seems it's calling something from PIL that has been removed... Can I change matplotlib to use Pillow instead of PIL? Or can I do something else? I have the most recent version of matplotlib installed.

Thanks in advance!

You should probably just use pillow directly, since matplotlib just falls back to PIL anyway for anything other than PNG files. From the documentation :

matplotlib can only read PNGs natively, but if PIL is installed, it will use it to load the image and return an array (if possible)

To load a JPG with pillow :

from PIL import Image
im = Image.open('myimage.jpg')

This way you cut out the middle man and control the image loading directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM