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!
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.