Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib savefig in jpeg format

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 ?

like image 607
cedm34 Avatar asked Jan 11 '12 21:01

cedm34


People also ask

How do I use Savefig in matplotlib?

Saving a plot on your disk as an image file Now if you want to save matplotlib figures as image files programmatically, then all you need is matplotlib. pyplot. savefig() function. Simply pass the desired filename (and even location) and the figure will be stored on your disk.

How do I save a python file as a PNG?

To save a plot or graph or a figure as png we use the savefig() method.

How do I save an image as a PLT?

Click the chart that you want to save as a picture. Choose Copy from the ribbon, or press CTRL+C on your keyboard . Switch to the application you want to copy the chart to. If you're saving as a separate image file open your favorite graphics editor, such as Microsoft Paint.


1 Answers

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

like image 122
Yann Avatar answered Sep 30 '22 19:09

Yann