Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save progressive jpeg using Python PIL 1.1.7?

Tags:

I'm trying to save with the following call and it raises error, but if i remove progressive and optimize options, it saves.

Here is my test.py that doesn't work:

import Image img = Image.open("in.jpg") img.save("out.jpg", "JPEG", quality=80, optimize=True, progressive=True) 

It raises this error:

Suspension not allowed here Traceback (most recent call last):   File "test.py", line 3, in <module>     img.save("out.jpg", "JPEG", quality=80, optimize=True, progressive=True)   File "/Library/Python/2.6/site-packages/PIL/Image.py", line 1439, in save     save_handler(self, fp, filename)   File "/Library/Python/2.6/site-packages/PIL/JpegImagePlugin.py", line 471, in _save     ImageFile._save(im, fp, [("jpeg", (0,0)+im.size, 0, rawmode)])   File "/Library/Python/2.6/site-packages/PIL/ImageFile.py", line 501, in _save     raise IOError("encoder error %d when writing image file" % s) IOError: encoder error -2 when writing image file 

Link to image: http://static.cafe.nov.ru/in.jpg (4.3 mb)

like image 859
Andrey Kuzmin Avatar asked Jul 22 '11 09:07

Andrey Kuzmin


People also ask

Does PIL support JPEG?

PIL is a free library that adds image processing capabilities to your Python interpreter, supporting a range of image file formats such as PPM, PNG, JPEG, GIF, TIFF and BMP.

How do I save a JPEG in Python?

The PIL module is used for storing, processing, and displaying images in Python. To save images, we can use the PIL. save() function. This function is used to export an image to an external file.

What is PIL image format?

Python Imaging Library is a free and open-source additional library for the Python programming language that adds support for opening, manipulating, and saving many different image file formats. It is available for Windows, Mac OS X and Linux. The latest version of PIL is 1.1.


2 Answers

import PIL from exceptions import IOError  img = PIL.Image.open("c:\\users\\adam\\pictures\\in.jpg") destination = "c:\\users\\adam\\pictures\\test.jpeg" try:     img.save(destination, "JPEG", quality=80, optimize=True, progressive=True) except IOError:     PIL.ImageFile.MAXBLOCK = img.size[0] * img.size[1]     img.save(destination, "JPEG", quality=80, optimize=True, progressive=True) 

PIL encodes part of the image at a time. This is incompatible with the 'optimize' and 'progressive' options.

Edit: You need to import PIL.Image, PIL.ImageFile for newer versions of PIL / Pillow.

like image 54
agf Avatar answered Sep 20 '22 08:09

agf


Here's a hack that might work, but you may need to make the buffer even larger:

from PIL import Image, ImageFile  ImageFile.MAXBLOCK = 2**20  img = Image.open("in.jpg") img.save("out.jpg", "JPEG", quality=80, optimize=True, progressive=True) 
like image 40
Eryk Sun Avatar answered Sep 19 '22 08:09

Eryk Sun