ExifTool Forum

ExifTool => Developers => Topic started by: rob8624 on February 24, 2024, 05:17:10 PM

Title: Writing JSON - No Source File error (Django)
Post by: rob8624 on February 24, 2024, 05:17:10 PM
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 ])
Title: Re: Writing JSON - No Source File error (Django)
Post by: StarGeek on February 24, 2024, 08:14:04 PM
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')
Title: Re: Writing JSON - No Source File error (Django)
Post by: rob8624 on February 24, 2024, 08:36:26 PM
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.
Title: Re: Writing JSON - No Source File error (Django)
Post by: StarGeek on February 24, 2024, 11:00:15 PM
Take time to read the docs on the -j (-json) option (https://exiftool.org/exiftool_pod.html#j-JSONFILE--json).  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
Title: Re: Writing JSON - No Source File error (Django)
Post by: rob8624 on February 25, 2024, 04:32:05 AM
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.
Title: Re: Writing JSON - No Source File error (Django)
Post by: StarGeek on February 25, 2024, 10:11:42 AM
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?
Title: Re: Writing JSON - No Source File error (Django)
Post by: rob8624 on February 25, 2024, 02:24:47 PM
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", .........}
Title: Re: Writing JSON - No Source File error (Django)
Post by: StarGeek on February 25, 2024, 02:41:18 PM
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>'
Title: Re: Writing JSON - No Source File error (Django)
Post by: rob8624 on February 25, 2024, 03:42:40 PM
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?
Title: Re: Writing JSON - No Source File error (Django)
Post by: StarGeek on February 25, 2024, 06:17:54 PM
Quote from: StarGeek on February 25, 2024, 02:41:18 PMIt cannot directly accept json data.
Title: Re: Writing JSON - No Source File error (Django)
Post by: rob8624 on February 25, 2024, 07:14:35 PM
Yea. OK, looks like Exiftool isn't capable of doing this, this way. Thanks for the help, and back to the drawing board :(
Title: Re: Writing JSON - No Source File error (Django)
Post by: rob8624 on February 25, 2024, 07:39:45 PM
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 :) !