[Image-SIG] PIL 1.0 (beta?) - Problem saving JPEG

Kingsley Turner krt@krt.com.au
Wed, 15 Dec 1999 21:49:55 +1100


I've got this occasional problem.

I have an image database,
we use http upload to get an image,
it's stored as a local file.

All this works fine, I've binary compared the upload
with what we get, and it's a 100% match.

So now _some_image_ is sitting in a known filename.
We use PIL to load the image, resize it to
some acceptable limit, and then save it as the master copy.

### Get some stats from the uploaded image
im = Image.open(pict_file_path)
pict_hires_x = im.size[0]
pict_hires_y = im.size[1]

### If the image is too big, resize it to 600x450ish
if pict_hires_x > 640 or pict_hires_y > 480:
    newx = 600
    newy = 450
    if pict_hires_x > pict_hires_y:
        newy = pict_hires_y * newx / pict_hires_x
        newx = pict_hires_x * newy / pict_hires_y
        pict_hires_x = newx
        pict_hires_y = newy
        im = im.resize((newx,newy))
### Save a reference copy

Now the problem...

In the last line of the code, *sometimes* if
quality=80, the save fails with the error - 
(depends entirely on the image uploaded)

Traceback (innermost last):
  File "/home/httpd/cgi-bin/picture_add.py", line 104, in ?
  File "/usr/local/lib/python1.5/PIL/Image.py", line 665, in save
    SAVE[string.upper(format)](self, fp, filename)
  File "/usr/local/lib/python1.5/PIL/JpegImagePlugin.py", line 307, in
    ImageFile._save(im, fp, [("jpeg", (0,0)+im.size, 0, rawmode)])
  File "/usr/local/lib/python1.5/PIL/ImageFile.py", line 223, in _save
    raise IOError, "encoder error %d when writing image file" % s
IOError: encoder error -2 when writing image file

At first I figured it must be some sort of image file format
problem, so I opened it in GIMP, resized it, saved it,
tried again - exactly the same.

When I changed the quality=70 the problem (at least for the
test image in question) disappeared.
Test image: http://MadDogsBreakfast.com/crocus.jpg

Does anyone have a clue as to what might be going on here ?


Kingsley Turner, mailto:kingsley@krt.com.au
Network R&D, KRT P/L
Mobile: +61 2419621568