Adding a comment to an image using PIL

Fredrik Lundh fredrik at pythonware.com
Tue Oct 31 01:53:02 EST 2006


Jeffrey Barish wrote:

> I am trying to use PIL to add a comment to an image.  I have never used PIL
> before, but I discovered that it is possible to print an existing comment
> with the following:
> 
> im = PIL.Image.open('filename.jpg')
> print im.app['COM']
> 
> I figured that I could write a comment by reversing this procedure:
> 
> im.app['COM'] = 'New comment'
> im.save('newfilename.jpg')
> 
> However, when I open newfilename.jpg, I find that key 'COM' does not
> exist -- the comment is not being written.  Presumably, I am doing
> something wrong.

PIL's support for JPEG markers is mostly read-only.  Adding comments
by decoding/encoding isn't a very good idea anyway; to do that right, 
you need to work on the JPEG data stream, not on a decoded image.

it would probably not be too hard to write a JPEG stream editor based
on PIL's JPEG stream parser, but for the time being, I recommend using 
IJG's "wrjpgcom" utility for things like this.

</F>




More information about the Python-list mailing list