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