Writing JSON - No Source File error (Django)

Started by rob8624, February 24, 2024, 05:17:10 PM

Previous topic - Next topic

rob8624

Hi folks, first, i hope I've formatted my code correctly if not apologies. I'm using Exiftool in a Python/Django app where the user can edit data from an uploaded image, the data is stored in a JSON database field, I then want to write the edited exif data back to the image.

I'm using Python's subprocess to interact with exiftool, I can successfully extract data and it's rendered correctly to an editable form from a JSON field in my model. I can also edit this data and write it back to the database, but when I try to write it to the file, which is being excessed via Django's Path attribute, I get this error. IT doesn't crash my server but no edited data is written, obviously.
No SourceFile 'C:/Users/foo.bar.jpg' in imported JSON database
My relevant code....

#get image obj
image = Uploaded_Image.objects.get(id=image_id)#get data from request and do some custom 'cleaning'
cleaned_data = clean_data_from_request(request)#writing to dict to model field (exif_data) as valid JSON
image.exif_data = json.dumps(cleaned_data)

I am then doing this to try and write the JSON
subprocess.check_output(["exiftool", f"-json={image.exif_data}", image.image.path ])This is rendering the error i'm unsure on how to debug it and find out where the problem lies.

Any input muchly appreciated.
NB..i tried to pass exiftool to subprocess like this as per docs... but it returns a syntax error.
subprocess.check_output(["exiftool", -json=image.exif_data, image.image.path ])

StarGeek

The No SourceFile ... in imported JSON database occurs when the filename you are passing to exiftool doesn't match any of the SourceFile entries in the json file.

For example, this json file has a SourceFile of "y:\!temp\Test4.jpg", but I'm passing a different file to the command.
C:\>type temp.txt
[{
  "SourceFile": "y:/!temp/Test4.jpg",
  "XMP-dc:Description": "Test"
}]

C:\>exiftool -P -overwrite_original -json=temp.txt y:\!temp\Test3.jpg
No SourceFile 'y:/!temp/Test3.jpg' in imported JSON database
(full path: 'y:/!temp/test3.jpg')
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

rob8624

Ok cheers StarGeek. So the sourcefile within the JSON has to match the file it's being written too? OK. I'll have a look tomorrow when I have some more time are report back.

StarGeek

Take time to read the docs on the -j (-json) option.  If there is a SourceFile entry, it must match the file. In this case the json file is a lookup table for any file that is being processed.

If you don't have a SourceFile entry, then
QuoteAn object with a missing SourceFile or a SourceFile of "*" defines default tags for all target files
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

rob8624

Cheers for the link, I've matched sourcefile and also removed the key and tried a Asterix as a value but I'm still getting the same error.

StarGeek

Make sure and test the command on the command line to make sure it works before including it in a script.

Can you share a sample json?
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

rob8624

Just thinking, the JSON is not being served via a file but from a JSON obj created via json.dumps. Does the command handle that? Doc say - "or import JSON file if JSONFILE is specified"

Here is a partial sample of what i'm trying to write.

{"SourceFile": "*", "ExifToolVersion": "12.77", "FileName": "RM_foobar.jpg", "Directory": "C:/Users/..../images", "FileSize": "2.9 MB", "FileModifyDate": "2024:02:25 09:55:14+00:00", "FileAccessDate": "2024:02:25 09:55:14+00:00", "FileCreateDate": "2024:02:25 09:55:14+00:00", "FilePermissions": "-rw-rw-rw-", "FileType": "JPEG", "FileTypeExtension": "jpg", "MIMEType": "image/jpeg", "ExifByteOrder": "Little-endian (Intel, II)", "PhotometricInterpretation": "RGB", "Make": "NIKON CORPORATION", "Model": "NIKON Z 6_2", .........}

StarGeek

It has to be a file or it can come from STDIN by using a hyphen -, e.g. -json=-.  It cannot directly accept json data.  Doing so should have gotten an error,
Error opening JSON file '<json data>'
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

rob8624

Yea that is exactly what I get, Error opening JSON file -- then the data. So, is it possible to write JSON in the way I intend?

StarGeek

* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

rob8624

Yea. OK, looks like Exiftool isn't capable of doing this, this way. Thanks for the help, and back to the drawing board :(

rob8624

#11
Ok. Got this working, partially as it's only writing some of the edited tags (it's not writing edited keywords). I just dumped the dict as JSON (as I was doing originally), wrote that to a temporary file then passed it to subprocess to run the exiftool command. All works.

Thanks for help with this, muchly appreciated, I'll work on getting it fully working but it's progress :) !