IOError for savefig JPG in matplotlib

1.6k views Asked by At

I am trying to save a JPG and having issues. The following gives "IOError: encoder error -2 when writing image file"

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

It works with png so I tried to:

pip install pillow

as suggested here but it was already installed in Canopy. It seems that it could be related to this bug. I tried using a direct path but that did not work either.

FULL ERROR MESSAGE FOLLOWS

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 
2

There are 2 answers

0
Keith On BEST ANSWER

There is a Canopy feature of the Package Manager where it knows that the package is installed but does not use it. This means that if you try to install or upgrade pillow for the Canopy terminal it will not be able to do anything. To give access to pillow you must do it through the package manager.

  1. Tools-> Package Manager
  2. Type pillow in the search bar
  3. click on pillow
  4. click install

While pillow was installed before this Canopy did not have access.

3
J Richard Snape On

My immediate answer would be try

pip install pillow --upgrade

This would upgrade your PIL version - probably therefore getting encoder version 70 rather than 62 as the first line of the error indicates is necessary

BUT

although I'm not familiar with Canopy, I did have a browse around their website and it appears that they implement their own package manager (or at least wrap one). This article seems to explain how to use it.

So, I'd first have a look in there and find PIL or pillow and click the option to upgrade. Use the command line version above only if that doesn't work / upgrade is not available / package manager is not available with your particular subscription.