Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (45.3k points)

I'm trying to save with the following call and it raises an 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

1 Answer

0 votes
by (16.8k points)

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)

Related questions

0 votes
4 answers
0 votes
2 answers
0 votes
1 answer
asked Dec 2, 2020 in Python by ashely (50.2k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...