[Image-SIG] PIL issue with desktop wallpaper changing--bug?

Chris Stromberger chris.stromberger at gmail.com
Thu Jan 27 15:57:09 CET 2005


Hello,

It was suggested I send this thread to this list (originally posted to
the python-win32 list).  Details below.  I traced the problem into a
PIL module and found what appears to be the issue.  Here's the fix I
applied, and it does correct the problem I was seeing:

>diff Image.py Image.py.fixed 
1125c1125,1130
<         self.load()
---
>         try:
>             self.load()
>         except:
>             if close:
>                 fp.close()
>             raise

The corrupt jpg raised an exception in the load function.  Here's the
relevant portion of the traceback:

  File "C:\Python23\lib\site-packages\PIL\Image.py", line 1126, in save
    self.load()
  File "C:\Python23\lib\site-packages\PIL\ImageFile.py", line 177, in load
    raise IOError("image file is truncated (%d bytes not processed)" % len(b))
IOError: image file is truncated (47 bytes not processed)

I have PIL 1.1.4 installed (as far as I can tell), Windows XP.

Thanks,
Chris

http://www.fetidcascade.com/



---------- Forwarded message ----------
From: Chris Stromberger <chris.stromberger at gmail.com>
Date: Wed, 26 Jan 2005 17:41:10 -0600
Subject: Re: PIL issue with desktop wallpaper changing
To: python-win32 at python.org


One more clue.  If I change the code to resize always, then the
corrupt jpg fails in the call to im.resize (before it attempts to call
im.save), and thereafter there are no problems--the subsequent normal
jpgs all work fine.  So it appears that by failing in im.save, the
'windows.bmp' file is left with an open handle or somesuch to it
somehow, in PIL, somewhere...


On Wed, 26 Jan 2005 17:33:40 -0600, Chris Stromberger
<chris.stromberger at gmail.com> wrote:
> I have a script to change the desktop wallpaper.  If it encounters a
> corrupt jpg, it seems to get off track and not recover.  Here's the
> code snippet.  This is called in a loop with several jpg images in a
> directory to switch the wallpaper.
>
> im = Image.open(filename)
> [call im.resize() here if necessary to reduce/enlarge image size to fill screen]
> im.save('wallpaper.bmp')
> cs = ctypes.c_buffer('wallpaper.bmp')
> ok = ctypes.windll.user32.SystemParametersInfoA(win32con.SPI_SETDESKWALLPAPER,
> 0, cs, 0)
>
> The above fails in the im.save call when passed a corrupt jpg--an
> exception is raised in im.save.  I catch it and loop to the next
> image.  The next time this block is called, and for every call
> thereafter, the SystemParametersInfoA call fails (returns 0), and if I
> call GetLastError() after that, it shows error 32, which is a file
> sharing violation.  I added an os.unlink('wallpaper.bmp') to the start
> of this block and it works until the corrupt jpg is processed.
> Thereafter, the file cannot be deleted ([Errno 13] Permission denied).
>  So it seems like something is grabbing onto the file, but I have no
> idea how to release that handle or what it is.
>
> One further clue: if I raise an exception (anything) after detecting
> that ok is 0, then the next image I try to load will work.  The
> exception raising clears everything out.
>
> So here are the two scenarios:
>
> 0. call with normal image(s)--works, wallpaper is changed each time
> 1. call with corrupt jpg--exception.
> 2. then call with normal images--SystemParametersInfoA returns 0 every
> time, wallpaper not changed again.
>
> if I add...
> 3. raise exception if SystemParametersInfoA returns 0--the next and
> subsequent images work.  Until we loop around to the corrupt jpg
> again.
>
> Any ideas what's going on?
>
> Thanks,
> Chris
>
> http://www.fetidcascade.com/
>


More information about the Image-SIG mailing list