PIL>>> Image.open('f.GIF').save('f.jpg', 'JPEG') # ?

greg Landrum greglandrum at earthlink.net
Tue May 23 16:42:36 CEST 2000


"Dinu C. Gherman" wrote:
>
> after studying the PIL tutorial I don't think I found the
> proper place explaining why I get an error like this when
> I try to save a GIF file in JPEG format:
> 
> >>> im = Image.open('sample.gif')
> >>> im.save('sample.jpg', 'jpeg')
> Traceback (innermost last):
>   File "<interactive input>", line 0, in ?
>   File "Image.py", line 665, in save
>     SAVE[string.upper(format)](self, fp, filename)
>   File "JpegImagePlugin.py", line 298, in _save
>     raise IOError, "cannot write mode %s as JPEG" % im.mode
> IOError: cannot write mode P as JPEG
> 
> Is it because palette mode images cannot be converted to
> JPEG? If so, can it be done with the pilconvert utility?
> I haven't tested this, but if pilconvert can do it, why
> isn't it possible without?
> 

Use the convert method of the Image:
>>> from PIL import Image 
>>> Image.open('prev.gif').convert('RGB').save('prev.jpg')

-greg


-- 

greg Landrum (greglandrum at earthlink.net)
Software Carpenter/Computational Chemist



More information about the Python-list mailing list