【问题标题】:IOError for savefig JPG in matplotlibmatplotlib 中 savefig JPG 的 IOError
【发布时间】:2015-06-12 19:23:56
【问题描述】:

我正在尝试保存 JPG 并遇到问题。下面给出“IOError: encoder error -2 when writing image file”

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

它适用于 png,所以我尝试:

pip install pillow

按照here 的建议,但它已经安装在 Canopy 中。似乎它可能与this 错误有关。我尝试使用直接路径,但这也不起作用。

完整的错误消息如下

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 

【问题讨论】:

  • 在 Windows 7、WinPython 3.3.5 和 matplotlib 1.4.3 上运行良好。
  • 我怀疑是 Canopy 问题。
  • 这几乎肯定与您的 PIL 版本有关。谷歌完整的错误信息,你会看到很多关于它的信息。粘贴更多(全部)回溯会很好 - 主要是为了确认错误是从“/site-packages/PIL/ImageFile.py”抛出的。另外 - 放置你的版本会很有用:OS、python 和 matplotlib(如果你能找到版本号,还有 PIL)以帮助重现错误。不过,我的预感是,您将不得不想办法获得更新的 PIL 版本。
  • 你似乎是对的。我添加了完整的消息。我正在使用 Canopy 1.5.3.3103 和一些希望不相关的附加组件。

标签: python matplotlib jpeg canopy


【解决方案1】:

我的直接答案是尝试

pip install pillow --upgrade

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

但是

虽然我不熟悉 Canopy,但我确实浏览了他们的网站,似乎他们实现了他们的 own package manager(或至少包含一个)。 This article似乎解释了如何使用它。

所以,我先进去看看,找到PILpillow,然后点击升级选项。仅当上述命令行版本不起作用/升级不可用/包管理器对您的特定订阅不可用时才使用。

【讨论】:

  • 是的,就像我说的那样,已经尝试过了。该命令返回“要求已经是最新的:./Enthought/Canopy_64bit/User/lib/python2.7/site-packages 中的枕头”
  • 好吧,你没有说你在 pip 中使用了 --upgrade 选项,所以我不知道。您是否尝试过,或者树冠自己的包管理器,或两者都尝试?
  • 非常好 - 我现在看到了答案 - 很高兴你把它整理好了 :)
【解决方案2】:

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

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

虽然在此 Canopy 无法访问之前安装了枕头。

【讨论】:

    猜你喜欢
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    • 2016-08-08
    • 2013-07-01
    • 2012-10-15
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多