我试图使用matplotlib的pyplot读取jpg图像。
这是我在使用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.
它似乎从PIL中调用了一些已删除的内容......我可以更改matplotlib以使用Pillow而不是PIL吗?或者我可以做点别的吗?我安装了最新版本的matplotlib。
提前致谢!
答案 0 :(得分:0)
你应该直接使用pillow
,因为除了PNG文件以外,matplotlib
只会回到PIL
。来自the documentation:
matplotlib只能本地读取PNG,但是如果安装了PIL,它将使用它来加载图像并返回一个数组(如果可能的话)
使用pillow
加载JPG:
from PIL import Image
im = Image.open('myimage.jpg')
这样你可以剪掉中间人并直接控制图像加载。