简体   繁体   English

Python Mac Matplotlib读取错误

[英]Python mac matplotlib imread error

I'm trying to read a jpg image using matplotlib's pyplot. 我正在尝试使用matplotlib的pyplot读取jpg图像。

This is the error I get when using the imread command: 这是使用imread命令时遇到的错误:

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? 看来它正在从PIL中调用某些已删除的内容...我可以将matplotlib更改为使用Pillow而不是PIL吗? Or can I do something else? 还是我可以做点别的? I have the most recent version of matplotlib installed. 我安装了最新版本的matplotlib。

Thanks in advance! 提前致谢!

You should probably just use pillow directly, since matplotlib just falls back to PIL anyway for anything other than PNG files. 您可能应该直接使用pillow ,因为对于除PNG文件以外的任何内容, matplotlib退回到PIL 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) matplotlib只能本地读取PNG,但是如果安装了PIL,它将使用它来加载图像并返回一个数组(如果可能)

To load a JPG with pillow : 要用pillow加载JPG:

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

This way you cut out the middle man and control the image loading directly. 这样,您可以裁剪中间人并直接控制图像加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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