[python-win32] Re: PIL issue with desktop wallpaper changing

Chris Stromberger chris.stromberger at gmail.com
Thu Jan 27 00:41:10 CET 2005


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 Python-win32 mailing list