繁体   English   中英

matplotlib中的savefig JPG的IOError

[英]IOError for savefig JPG in matplotlib

我正在尝试保存JPG并遇到问题。 下面给出了“ IOError:写入图像文件时编码器错误-2”

import matplotlib.pyplot as plt
plt.plot([1, 2])
plt.savefig('image.jpg')

它与png兼容,所以我尝试:

pip install pillow

如此处建议的那样但它已安装在Canopy中。 似乎它可能与错误有关。 我尝试使用直接路径,但这也不起作用。

完整错误消息如下

Wrong JPEG library version: library is 62, caller expects 70
---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
/tmp/tmpMQF4j1.py in <module>()
      1 import matplotlib.pyplot as plt
      2 plt.plot([1, 2])
----> 3 plt.savefig('image.jpg')

/home/keith/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/pyplot.pyc in savefig(*args, **kwargs)
    575 def savefig(*args, **kwargs):
    576     fig = gcf()
--> 577     res = fig.savefig(*args, **kwargs)
    578     draw()   # need this if 'transparent=True' to reset colors
    579     return res

/home/keith/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/figure.pyc in savefig(self, *args, **kwargs)
   1468             self.set_frameon(frameon)
   1469 
-> 1470         self.canvas.print_figure(*args, **kwargs)
   1471 
   1472         if frameon:

/home/keith/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/backends/backend_qt5agg.pyc in print_figure(self, *args, **kwargs)
    159 
    160     def print_figure(self, *args, **kwargs):
--> 161         FigureCanvasAgg.print_figure(self, *args, **kwargs)
    162         self.draw()
    163 

/home/keith/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/backend_bases.pyc in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
   2192                 orientation=orientation,
   2193                 bbox_inches_restore=_bbox_inches_restore,
-> 2194                 **kwargs)
   2195         finally:
   2196             if bbox_inches and restore_bbox:

/home/keith/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyc in print_jpg(self, filename_or_obj, *args, **kwargs)
    577                 options['quality'] = rcParams['savefig.jpeg_quality']
    578 
--> 579             return image.save(filename_or_obj, format='jpeg', **options)
    580         print_jpeg = print_jpg
    581 

/home/keith/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/PIL/Image.pyc in save(self, fp, format, **params)
   1691 
   1692         try:
-> 1693             save_handler(self, fp, filename)
   1694         finally:
   1695             # do what we can to clean up

/home/keith/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/PIL/JpegImagePlugin.pyc in _save(im, fp, filename)
    695     bufsize = max(ImageFile.MAXBLOCK, bufsize, len(info.get("exif", b"")) + 5)
    696 
--> 697     ImageFile._save(im, fp, [("jpeg", (0, 0)+im.size, 0, rawmode)], bufsize)
    698 
    699 

/home/keith/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/PIL/ImageFile.pyc in _save(im, fp, tile, bufsize)
    495             s = e.encode_to_file(fh, bufsize)
    496             if s < 0:
--> 497                 raise IOError("encoder error %d when writing image file" % s)
    498             e.cleanup()
    499     try:

IOError: encoder error -2 when writing image file 

我直接的答案是尝试

pip install pillow --upgrade

这将升级您的PIL版本-可能因此需要获得编码器版本70,而不是62,因为错误的第一行表明有必要

尽管我不熟悉Canopy,但我确实浏览了他们的网站,并且看来他们实现了自己的软件包管理器 (或至少包装了一个)。 本文似乎解释了如何使用它。

因此,我首先PIL那里寻找PILpillow ,然后单击选项进行升级。 仅当上面的命令行版本不起作用/升级不可用/特定订阅中的软件包管理器不可用时,才使用上面的命令行版本。

程序包管理器有一个Canopy功能,它知道已安装程序包但不使用它。 这意味着,如果您尝试为Canopy终端安装或升级枕头,它将无能为力。 要访问枕头,您必须通过包装管理器进行操作。

  1. 工具->程序包管理器
  2. 在搜索栏中输入枕头
  3. 点击枕头
  4. 点击安装

在此机盖无法使用之前,已安装了枕头。

暂无
暂无

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

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