ExifTool Forum

ExifTool => Bug Reports / Feature Requests => Topic started by: Faramy on March 07, 2023, 01:19:38 AM

Title: Python doesn't work properly with exiftool
Post by: Faramy on March 07, 2023, 01:19:38 AM
Hello.

The script has this line:
subprocess.run(f'exiftool.exe -Description="{result}" -overwrite_original "{image}"')
Some sites display Description normally.
And some sites (as I calculated by the number of characters) remove spaces, and show spaces instead of characters (by the number of characters.
That is, instead of
Curvy road with a sign in the middle of it.shows
                               
If you directly write the Description to the file, then everything is fine.

Is there a solution?
Title: Re: Python doesn't work properly with exiftool
Post by: Faramy on March 07, 2023, 02:42:21 AM
Maybe there is some way to fix this?

I tried to resave, but to no avail.
exiftool.exe -P -overwrite_original "-Description<${Description}" "D:\."
How else can you overwrite the Description so that it is correctly saved?
Title: Re: Python doesn't work properly with exiftool
Post by: Malus on March 07, 2023, 05:46:49 AM
This code is running fine on my site:

import subprocess
result = 'myresult'
image = r'd:\test\file.jpg'
_r = subprocess.run(f'exiftool.exe -Description="{result}" -overwrite_original "{image}"')
_r = subprocess.run(f'exiftool.exe -Description "{image}"', capture_output=True, text=True)
print(_r.stdout)
print(_r.stderr)

What's the value of your string "result".

Last, let me encourage you to try pyExiftool. It's using stay_open (https://exiftool.org/exiftool_pod.html) and much faster then running "subprocess.run(f'exiftool.exe . . .". several times in a loop.



Thanks for reading, malus


Title: Re: Python doesn't work properly with exiftool
Post by: Faramy on March 07, 2023, 08:36:51 AM
Malus, thanks for the tip about pyExiftool. I will try it.
As for result, everything is recorded normally.
And Description is visible in exif. But here are some sites that do not see the Description, which was written in this way.
Title: Re: Python doesn't work properly with exiftool
Post by: StarGeek on March 07, 2023, 10:11:32 AM
Quote from: Faramy on March 07, 2023, 08:36:51 AMAnd Description is visible in exif. But here are some sites that do not see the Description, which was written in this way.

Description is not an EXIF tag, it is an XMP (https://exiftool.org/TagNames/XMP.html) tag, specifically a Dublin Core XMP-dc tag.  All EXIF data is metadata, but not all metadata is EXIF data.

Otherwise, this is FAQ #3 (https://exiftool.org/faq.html#Q3). Exiftool has no control over what tags some website reads. Find a file that reads the data you want correctly and use the command in FAQ #3 to figure out what the actual tag you want to write is.
Title: Re: Python doesn't work properly with exiftool
Post by: Faramy on March 08, 2023, 03:18:30 AM
Is it possible to simply resave the meta-information in the file? This should fix the bug.

It would be great if there was such a command.
Title: Re: Python doesn't work properly with exiftool
Post by: Faramy on March 08, 2023, 03:42:34 AM
Now I looked more closely.
The uncorrected file has only "Description"
And in the corrected one, this name is still in the "Image Description"
I'll try the command:
exiftool.exe -P -overwrite_original "-Image Description<${Description}" "D:\."Warning: Invalid tag name 'image description' :(
Title: Re: Python doesn't work properly with exiftool
Post by: Faramy on March 08, 2023, 05:25:17 AM
Tried -P -overwrite_original "-Description<${Title}" Output:
QuoteIPTCDigest is not current. XMP may be out of sync
Title: Re: Python doesn't work properly with exiftool
Post by: Faramy on March 08, 2023, 05:46:42 AM
If I apply this JS, then the whole file is corrected:
        btnSave.onClick = function()
        {
            if ( app.document.selections.length == 1 )
            {
                editTitle = wrapper.editTitleRefs[0];
                new_title = editTitle.text;
                editDescr = wrapper.editDescrRefs[0];
                new_descr = editDescr.text;
                DescrToTitle = wrapper.chkCloneDescrBox[0];
                TitleToDescr = wrapper.chkCloneTitleBox[0];
                if ( DescrToTitle.value == true )
                    new_title = new_desct;
                if ( TitleToDescr.value == true )               
                    new_title = new_descr;
                editKeywords =  wrapper.editKeywordsRefs[0];
                new_subject = editKeywords.text.split(",");
                for (var k  =0 ; k < new_subject.length; k++)
                {
                        new_subject[k] = new_subject[k].trim();
                }
                saveMetadata(app.document.selections[0],
                                    new_title,
                                    new_descr,
                                    new_subject,
                                    { sort:wrapper.chkSortBox[0].value,
                                    append:false } );
                reselectFiles();
            }
            else
            {
                alert("Metadata save only for one file", "Error", errorIcon)
            }           
        }   

But I can only apply to one file.
I have to come up with something to apply to the whole folder.
Title: Re: Python doesn't work properly with exiftool
Post by: Phil Harvey on March 08, 2023, 08:02:08 AM
Quote from: Faramy on March 08, 2023, 03:42:34 AMexiftool.exe -P -overwrite_original "-Image Description<${Description}" "D:\."Warning: Invalid tag name 'image description' :(

See FAQ 2 (https://exiftool.org/faq.html#Q2).

- Phil
Title: Re: Python doesn't work properly with exiftool
Post by: Faramy on March 08, 2023, 09:15:38 AM
First of all, this is the error:
QuoteWarning: IPTCDigest is not current. XMP may be out of sync
Most likely, python somehow crookedly writes:
subprocess.run(f'exiftool.exe -Description="{result}" -overwrite_original "{image}"')
Title: Re: Python doesn't work properly with exiftool
Post by: Faramy on March 08, 2023, 09:21:56 AM
In general, I found a way out. What is called - with crutches.
Suddenly someone will need it too. I leave it here:
1. In the same script that sends files to the server, I form a csv file from jpg files:
exiftool.exe -T -csv -Filename -Description -Subject ./*.jpg > CSV.csv2. I send this file along with pictures.
3. On the server, the csv file is processed and the Description is now normally visible!
Title: Re: Python doesn't work properly with exiftool
Post by: Phil Harvey on March 08, 2023, 01:51:56 PM
The -T option has no effect when combined with -csv

- Phil
Title: Re: Python doesn't work properly with exiftool
Post by: StarGeek on March 08, 2023, 02:01:46 PM
Quote from: Faramy on March 08, 2023, 03:18:30 AMThis should fix the bug.

Failing to see where you have shown there is a bug.

It appears more that you don't know what is the correct tag you want to write. Which is understandable given the complete mess of competing standards that is image metadata.

Obligatory xkcd
Title: Re: Python doesn't work properly with exiftool
Post by: Faramy on March 09, 2023, 11:32:51 AM
Thanks everyone for the help, but choosing with an intermediate csv works fine.

Now there is another problem:

    subprocess.run(f'C:/exiftool.exe -P -overwrite_original -Title="{result}" "{image}"')
    subprocess.run(f'C:/exiftool.exe -P -overwrite_original -Description="{result}" "{image}"')

If the file is .jpg, then it normally writes both in the Title and in the Description.

If the file is .mov - writes only in the Title. And the Description in the file remains empty. :(

And if I change places - it is written only in Description
    subprocess.run(f'C:/exiftool.exe -P -overwrite_original -Description="{result}" "{image}"')
    subprocess.run(f'C:/exiftool.exe -P -overwrite_original -Title="{result}" "{image}"')

The problem, as I understand it, is the inability to create a second temporary file, since it already exists:
Error renaming temporary file

Only I don't understand why it creates a temporary file. Isn't it possible to write directly to a file?
Title: Re: Python doesn't work properly with exiftool
Post by: StarGeek on March 09, 2023, 12:19:51 PM
Quote from: Faramy on March 09, 2023, 11:32:51 AMThe problem, as I understand it, is the inability to create a second temporary file, since it already exists:
Error renaming temporary file

Only I don't understand why it creates a temporary file. Isn't it possible to write directly to a file?

Exiftool always creates a new file and renames the old one once it the write returns that the operation has been successfully completed.  See FAQ #31 (https://exiftool.org/faq.html#Q31).
Title: Re: Python doesn't work properly with exiftool
Post by: Faramy on March 09, 2023, 12:45:28 PM
Like another crutch - put between them
if os.path.exists(mov_file):
    time.sleep(10)

It works now.
Title: Re: Python doesn't work properly with exiftool
Post by: StarGeek on March 09, 2023, 12:56:20 PM
You should probably look into PyExifTool (https://pypi.org/project/PyExifTool/).  It is a wrapper around exiftool that keeps exiftool running in the background.  This can speed up the process, especially where there are a lot of files.  You should also try condensing the writes.  For example, rather than write Title and Description separately, write them both at the same time.

See the second paragraph in this post (https://exiftool.org/forum/index.php?topic=14581.msg78560#msg78560).  tl;dr, it took an hour for a person running exiftool once per file on 5k files, while my single command took only 6 minutes.
Title: Re: Python doesn't work properly with exiftool
Post by: Faramy on March 09, 2023, 01:50:33 PM
Thank you for your advice.
Unfortunately, I did not see an example there, how you managed to run exiftool before the start of the loop, and just execute scripts in the loop.
Title: Re: Python doesn't work properly with exiftool
Post by: Faramy on March 09, 2023, 02:29:21 PM
It turns out that everything is very simple:

with ExifToolHelper() as et:
    et.set_tags(image,
        tags={"XMP:Title": result, "XMP:Description": result},
        params=["-P", "-overwrite_original"]
    )

Thank you again. Now I will try it in practice. :)