cedm34
cedm34

Reputation: 513

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 ?

Upvotes: 44

Views: 179415

Answers (7)

Dre
Dre

Reputation: 2182

I'm not sure about all versions of Matplotlib, but in the official documentation for v3.5.0 savfig allows you to pass settings through to the underlying Pillow library which anyway does the image saving. So if you want a jpg with specific compression settings for example:

import matplotlib.pyplot as plt
plt.plot(...)  # Plot stuff
plt.savefig('filename.jpg', pil_kwargs={
    'quality': 20,
    'subsampling': 10
})

This should give you a highly compressed jpg as the output.

Upvotes: 1

simon
simon

Reputation: 5203

Just for completeness, if you also want to control the quality (i.e. compression level) of the saved result, it seems to get a bit more complicated, as directly passing plt.savefig(..., quality=5) does not seem to have an effect on the output size and quality. So, on the one hand, one could either go the way of saving the result as a png first, then reloading it with PIL, then saving it again as a jpeg, using PIL's quality parameter – similar to what is suggested in Yann's answer.

On the other hand, one can avoid this deviation of loading and saving, by using BytesIO (following the answer to this question):

from io import BytesIO
import matplotlib.pyplot as plt
from PIL import Image

buf = BytesIO()

plt.plot(...)  # Plot something here
plt.savefig(buf)
Image.open(buf).convert("RGB").save("testplot.jpg", quality=5)

Upvotes: 1

divenex
divenex

Reputation: 17228

To clarify and update @neo useful answer and the original question. A clean solution consists of installing Pillow, which is an updated version of the Python Imaging Library (PIL). This is done using

pip install pillow

Once Pillow is installed, the standard Matplotlib commands

import matplotlib.pyplot as plt

plt.plot([1, 2])
plt.savefig('image.jpg')

will save the figure into a JPEG file and will not generate a ValueError any more.

Contrary to @amillerrhodes answer, as of Matplotlib 3.1, JPEG files are still not supported. If I remove the Pillow package I still receive a ValueError about an unsupported file type.

Upvotes: 43

Yann
Yann

Reputation: 35533

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

Upvotes: 51

letmaik
letmaik

Reputation: 3500

Just install pillow with pip install pillow and it will work.

Upvotes: 10

Davidm
Davidm

Reputation: 29

Matplotlib can handle directly and transparently jpg if you have installed PIL. You don't need to call it, it will do it by itself. If Python cannot find PIL, it will raise an error.

Upvotes: 2

amillerrhodes
amillerrhodes

Reputation: 2702

I just updated matplotlib to 1.1.0 on my system and it now allows me to save to jpg with savefig.

To upgrade to matplotlib 1.1.0 with pip, use this command:

pip install -U 'http://sourceforge.net/projects/matplotlib/files/matplotlib/matplotlib-1.1.0/matplotlib-1.1.0.tar.gz/download'

EDIT (to respond to comment):

pylab is simply an aggregation of the matplotlib.pyplot and numpy namespaces (as well as a few others) jinto a single namespace.

On my system, pylab is just this:

from matplotlib.pylab import *
import matplotlib.pylab
__doc__ = matplotlib.pylab.__doc__

You can see that pylab is just another namespace in your matplotlib installation. Therefore, it doesn't matter whether or not you import it with pylab or with matplotlib.pyplot.

If you are still running into problem, then I'm guessing the macosx backend doesn't support saving plots to jpg. You could try using a different backend. See here for more information.

Upvotes: 4

Related Questions