Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding a comment to a jpeg file using python

Tags:

python

jpeg

I want to write a python program that adds a comment to a jpeg file. I've read that a comment in a jpeg file is signaled by the marker 0xfffe. Thus, can I just open the file and append this marker with whatever comment I want following it? My code looks something like this:

file = open("someimage.jpg", "a+b")
file.write("\xff\xfeCOMMENT")
file.close()

Does it matter if my comment is after the end of file marker (0xffd9)? Thanks!

like image 826
osdev0 Avatar asked Sep 18 '25 15:09

osdev0


1 Answers

This will work (it will append text beyond the part needed to store the image).

A more sophisticated approach would read the JPG file format and add a comment in EXIF fields. See this StackOverflow discussion: Exif manipulation library for python

See pyexiv for python bindings to exiv2, a tool for reading and writing image metadata.

like image 187
Raymond Hettinger Avatar answered Sep 20 '25 04:09

Raymond Hettinger