Change JPG compression ratio in savefig

Frank Breitling <fbreitling@...1017...> writes:

I am using the GTKAgg backend which was the only one I found with
support for JPEG.

I think that backend inherits its support from the GTK backend, which
uses GDK for saving JPEG files, and doesn't do anything with those
options.

I just realized that the Agg support depends on PIL. If you have that
installed, this script should produce a really low-quality JPEG file:

···

----------------------------------------------------------------------
#!/usr/bin/env python
import matplotlib
matplotlib.use('agg')
import pylab

pylab.plot([3,1,4,1,5,9,2], lw=4)
pylab.savefig('foo.jpeg', quality=1)
----------------------------------------------------------------------

Although the options quality='95' and quality=1 didn't produce an error
they didn't have any effect either on a Ubuntu system.
Is there anything else I could try?

If you do have PIL, you could use the non-interactive agg backend. If
you do need to use an interactive backend that doesn't have support for
these options, you could look at the implementation of
FigureCanvasBase.print_jpg and replicate its functionality.

Besides, it would be very useful to find these options in the
documentation or the help if there is a situation in which they work.

The options are documented, but it's not very easy to find them:

http://matplotlib.sourceforge.net/api/backend_bases_api.html#matplotlib.backend_bases.FigureCanvasBase.print_jpeg

--
Jouni K. Sepp�nen
http://www.iki.fi/jks

Hi Jouni,

Frank Breitling<fbreitling@...1017...> writes:

I am using the GTKAgg backend which was the only one I found with
support for JPEG.

I think that backend inherits its support from the GTK backend, which
uses GDK for saving JPEG files, and doesn't do anything with those
options.

I just realized that the Agg support depends on PIL. If you have that
installed, this script should produce a really low-quality JPEG file:

----------------------------------------------------------------------
#!/usr/bin/env python
import matplotlib
matplotlib.use('agg')
import pylab

pylab.plot([3,1,4,1,5,9,2], lw=4)
pylab.savefig('foo.jpeg', quality=1)
----------------------------------------------------------------------

I have the PIL module installed on my system (if this is what you mean) but when I run your example I see

  lfe001:~$ jpg.py
Traceback (most recent call last):
   File "./jpg.py", line 7, in <module>
     pylab.savefig('foo.jpeg', quality=1)
   File "/data/sys/opt/pythonlibs/lib/python2.5/site-packages/matplotlib/pyplot.py", line 356, in savefig
     return fig.savefig(*args, **kwargs)
   File "/data/sys/opt/pythonlibs/lib/python2.5/site-packages/matplotlib/figure.py", line 1032, in savefig
     self.canvas.print_figure(*args, **kwargs)
   File "/data/sys/opt/pythonlibs/lib/python2.5/site-packages/matplotlib/backend_bases.py", line 1420, in print_figure
     '%s.' % (format, ', '.join(formats)))
ValueError: Format "jpeg" is not supported.
Supported formats: emf, eps, pdf, png, ps, raw, rgba, svg, svgz.

If you do have PIL, you could use the non-interactive agg backend. If you do need to use an interactive backend that doesn't have support for these options, you could look at the implementation of FigureCanvasBase.print_jpg and replicate its functionality.

Sounds complicated. I don't know how to do that.

Besides, it would be very useful to find these options in the
documentation or the help if there is a situation in which they work.

The options are documented, but it's not very easy to find them:

http://matplotlib.sourceforge.net/api/backend_bases_api.html#matplotlib.backend_bases.FigureCanvasBase.print_jpeg

Well, I think it should also be listed at http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.savefig .

Frank

···

Am 06.09.2011 18:10, schrieb Jouni K. Sepp�nen: