2

using the command line exiftool I added a comment to my JPEG image and now when trying to get the data in python to display it I get an error.

The way I put the comment in the picture was with the following command:

exiftool -Comment=1234 j1.2.jpg

Below is the code of me trying to get the information out the JPEG including the comments but I am having trouble with the _getexif() line as it says there is no attribute _getexif().

from PIL import Image
from PIL.ExifTags import TAGS

image = 'j1.2.jpg'
exifdata = image._getexif()

for k,v in exifdata.items():
    print(str(TAGS.get(k)) + '\t' + str(v))

I am using python 3.9 for reference

papasamil
  • 81
  • 6
  • I'd assume that it still downloads the full file, but parses it somewhere in between to a jpg. Try finding a raw response or use a lower-level request to get the data as binary/file-type agnostic. – Lukas Schmid Apr 15 '22 at 13:33
  • @LukasSchmid I have downloaded the image normally and it does not appear then either so I am guessing when it is uploaded to the chat it automatically gets rid of everything after the FFD9 tag, would there be anyway to bypass this do you think? I'm using slack for reference. – papasamil Apr 15 '22 at 13:43
  • You didn't provide the URL... how can anyone help? – Mark Setchell Apr 15 '22 at 19:26
  • @MarkSetchell the URL is not the issue, the issue is finding a way around the metadata being stripped and someone said a way around this was by embedding a comment into the image and my code above is my attempt at this however it did not work so I was asking if anyone could help me out with embedding a comment into the image – papasamil Apr 16 '22 at 10:12
  • If you would like to insert a comment... https://stackoverflow.com/a/71729176/2836621 – Mark Setchell Apr 16 '22 at 10:55
  • @MarkSetchell is that not using exiftool? I am trying to do this in python as in adding a comment and then being able to take it out again – papasamil Apr 16 '22 at 11:14
  • I don't understand what you are really trying to do, nor what the problem is, but if you want to insert a comment into a JPEG without using `exiftool` you could base it on this https://stackoverflow.com/a/66103968/2836621 or https://stackoverflow.com/a/70438206/2836621 – Mark Setchell Apr 16 '22 at 11:49
  • @MarkSetchell Essentially, I am trying to use python to retrieve comments from a JPEG file, I can insert the comments fine using the actual exiftool but then when I try to retrieve information in python it does not work, I will update the question with the code that I tried to get the data but neither of the links you sent help me I don't think, sorry I am fairly new to using exiftool. – papasamil Apr 16 '22 at 12:25
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/243949/discussion-between-papasamil-and-mark-setchell). – papasamil Apr 16 '22 at 12:28
  • Your question appears to be about incomplete downloads and `ffd9` markers or maybe `ff9d` markers, rather than inserting or retrieving comments. Can you update your question please so it correctly reflects what you are trying do. Thank you. – Mark Setchell Apr 16 '22 at 13:29
  • Comments can be stored in JPEGs in several different ways/places. Please show exactly what command you used with `exiftool`. Thank you. – Mark Setchell Apr 16 '22 at 17:52
  • @MarkSetchell I have updated it to say how I put the comment in the first place as you asked, hope you might be able to help! – papasamil Apr 17 '22 at 11:15

2 Answers2

3

Create a file with ImageMagick and insert a comment with exiftool:

magick -size 640x480 gradient:red-blue image.jpg
exiftool -Comment="Freddy Frog" image.jpg

Extract comment with PIL:

#!/usr/bin/env python3

from PIL import Image

im = Image.open('image.jpg')
comment = im.info['comment']
print(comment)

Sample Run

./PILJPEGComment.py
b'Freddy Frog'
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
0

Looking at the jpg-block-structure, there is a tag to embed a comment in the file. Use FFFE to start the comment block and then FF9D to end the file. This should be retained through all media.

Lukas Schmid
  • 1,895
  • 1
  • 6
  • 18