matplotlib savefig in jpeg format

cedm34 picture cedm34 · Jan 11, 2012 · Viewed 123.7k times · Source

I am using matplotlib (within pylab) to display figures. And I want to save them in .jpg format. When I simply use the savefig command with jpg extension this returns :

ValueError: Format "jpg" is not supported.

Supported formats: emf, eps, pdf, png, ps, raw, rgba, svg, svgz.

Is there a way to perform this ?

Answer

Yann picture Yann · Jan 11, 2012

You can save an image as 'png' and use the python imaging library (PIL) to convert this file to 'jpg':

import Image
import matplotlib.pyplot as plt

plt.plot(range(10))
plt.savefig('testplot.png')
Image.open('testplot.png').save('testplot.jpg','JPEG')

The original:

enter image description here

The JPEG image:

enter image description here