Python stores JPG images as progressive error

Source: Internet
Author: User

Recently used PIL to do simple site image processing, but there are always some problems, fortunately stackoverflow on the cattle too much, search also has the answer. 1.jpg Picture stored as progressive error

The web often has to turn pictures into progressively displayed formats, while transferring pictures while at the same time making the web more ambiguous to show. Here is the code that uses PIL to do JPG conversion:

Import Image
img = Image.open ("in.jpg")
img.save ("Out.jpg", "JPEG", quality=80, Optimize=true, progressive= True)
Error:
Suspension not allowed
to Traceback (most recent call last):
  File "test.py", line 3, in <module>
    img.s Ave ("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
Answer:

This can actually transform the small picture, but the big picture will be wrong. The solution is to increase bufferr.

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)
In short, you want to increase the value of PIL.ImageFile.MAXBLOCK.

See also: Http://stackoverflow.com/questions/6788398/how-to-save-progressive-jpeg-using-python-pil-1-1-7?answertab=votes#tab-top


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.