I am trying to write metadata to an image file in Python. I get the image URL from an API and one of the object field has comments which I want to add to my image.
response = requests.get(updated_image_url)
download_path = DOWNLOAD_PATH '/images_test/' file_name
with open(download_path, 'wb') as f:
f.write(response.content)
f['Comments'] = 'Comments'
The above code fails when I try to add comments. However the file is downloaded as expected and saved.
Error for comments:
TypeError: '_io.BufferedWriter' object does not support item assignment
I was able to use another snippet to add metdata but that doesn't show the metadata in the comments in Finder. Here is the code:
meta = {'Comments': 'my test'}
f.write(json.dumps(meta).encode('utf-8'))
Can anyone suggest what I may be doing wrong here and how to save the metadata for the image (jpg) file when I download it? I am using MACOS to run this script.
CodePudding user response:
You can try using this library:
Note: You might want to strip any characters that might upset osascript - I don't know, I haven't tested, but maybe double quotes, single quotes, slashes?