Sorting PNG files from a keyword in the description

Started by KevinVideo, July 22, 2024, 01:25:05 PM

Previous topic - Next topic

KevinVideo

I am sure that there is a way to do this, but I have so far been unable to figure it out.
I want to sort a large batch of PNG files that were created with an AI image generator and included wildcards.
I want to sort these files into separate folders based on specific keywords that are included in the Description tag of the image.
Can you show me how I would use exiftool to sort images based on, for example, 4 different variables that would have been used in the image batch generation?

Phil Harvey

What do you mean?  Sort images into 4 different directories based on the Description tag containing one of the 4 different variables?  If so, something like this could do it:

exiftool "-directory<${description;s/.*\b(VAR1|VAR2|VAR3|VAR4)\b.*/$1/i or $_=undef}" DIR

Files with none of the variables in Description won't be moved.  I've put the "\b" around the variable names to make sure they start/end at a word boundary (eg. aren't found in the middle of another word).  I've also made the match case insensitive (that's what the "i" does).

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

KevinVideo

Sort images into 4 different directories, yes.
I was incorrect on the field. I thought it was Description, but it is Parameters.
The Parameters field has more entries, at least potentially, than most of the others in the image.
I made the substitution in the script and it does scan the images, but it does not move them or create subdirectories.
I tried it with and without wildcards (*), just to see what happened but nothing different.
Here is the syntax that I used:

C:\exiftool "-directory<${parameters;s/.*\b(VAR1|VAR2|VAR3|VAR4)\b.*/$1/i or $_=undef}" .\.

Any further suggestions that may help to resolve my conundrum?

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).

KevinVideo

I did use cmd. From the address bar of the directory that I was doing this in.

Phil Harvey

A few questions/comments:

1. Did you substitute VAR1, VAR2, etc with the variables you want to use?

2. Did you confirm that these variables exist in the Parameters of these files?: Run this command to confirm:

exiftool -parameters .\.

(I'm not sure what ".\." does, but I'm using it in this command because that is what you did.)

3. If you can't get it to work, provide the output from the above command and the exact exiftool command you used to do the sorting and we'll be able to help more.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

KevinVideo

Yes, I substituted VAR for the actual search terms.
Yes, I verified that each variable exists in the Parameters of the PNG image.

exiftool "-directory<${parameters;s/.*\b(VAR1|VAR2|VAR3|VAR4)\b.*/$1/i or $_=undef}" DIR

DIR at the end produced an error. I changed it to the path where the images are located, and it still gave an error. I changed it to .\. to indicate the current directory and the script ran. It did show errors that said the directory names could not be created due to reserved names (or something along those lines, but didn't make logical sense to me as the names have no special characters).


Phil Harvey

Knowing the exact error would help, but you can debug this yourself with the -p option to see what directory it is trying to set:

exiftool -p "'${parameters;s/.*\b(VAR1|VAR2|VAR3|VAR4)\b.*/$1/i or $_=undef}'" DIR

Here I have put a set of single quotes around each output line so you can see any trailing spaces.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

StarGeek

#8
I think I have a guess as to the problem. I think that the Parameters tag includes some new lines. I tested the command on the SD image in this post and recieved the
Warning: New file name not allowed in Windows (contains reserved characters)warning.  That's because the regex was only working on the line with the keyword in it. And new lines would be considered reserved characters

Try adding an s to the end of the regex, i.e. change
/$1/i
into
/$1/is

That tells the regex to allow dots to match newlines.

The whole AI Parameters tag is a rapidly changing target, as the values I've previous seen were all a single line.
* 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).

KevinVideo

#9
These are the commands and the outputs, without creating a subdirectory. I am generating images based on nationalities, and would like to separate them into nationality directories (I do about 50 at a time, so most likely I will be creating a batch file from the working command).


C:\Automatic1111\webui\outputs\txt2img-images\2024-07-23\exiftool test>C:\exiftool -p "'${parameters;s/.*\b(arab|Afro-Caribbean|mexican|bihari|french)\b.*/$1/is or $_=undef}'" .\.
Warning: [Minor] Advanced formatting expression returned undef for 'parameters' - ././00000-1235604802.png
Warning: [Minor] Advanced formatting expression returned undef for 'parameters' - ././00000-1235604809.png

...

'Arab'
Warning: [Minor] Advanced formatting expression returned undef for 'parameters' - ././02242-1235604804.png
Warning: [Minor] Advanced formatting expression returned undef for 'parameters' - ././02243-1235604805.png
Warning: [Minor] Advanced formatting expression returned undef for 'parameters' - ././02244-1235604806.png
Warning: [Minor] Advanced formatting expression returned undef for 'parameters' - ././02245-1235604807.png
Warning: [Minor] Advanced formatting expression returned undef for 'parameters' - ././02246-1235604808.png
    1 directories scanned
2996 image files read

C:\Automatic1111\webui\outputs\txt2img-images\2024-07-23\exiftool test>


=======================================================================================================


C:\Automatic1111\webui\outputs\txt2img-images\2024-07-23\exiftool test>C:\exiftool "'${parameters;s/.*\b(arab|Afro-Caribbean|mexican|bihari|french)\b.*/$1/is or $_=undef}'" .\.
Wildcards don't work in the directory specification
======== ././00002-560484948.png
ExifTool Version Number        : 12.85
File Name                      : 00002-560484948.png
Directory                      : ./.
File Size                      : 1387 kB
File Modification Date/Time    : 2024:07:23 21:32:34-04:00
File Access Date/Time          : 2024:07:23 21:37:09-04:00
File Creation Date/Time        : 2024:07:23 21:32:34-04:00
File Permissions                : -rw-rw-rw-
File Type                      : PNG
File Type Extension            : png
MIME Type                      : image/png
Image Width                    : 768
Image Height                    : 1152
Bit Depth                      : 8
Color Type                      : RGB
Compression                    : Deflate/Inflate
Filter                          : Adaptive
Interlace                      : Noninterlaced
Parameters                      : Ultra-HD-photo-same-realistic-quality-details, 8k, next to a stream in an isolated meadow, .1girl, 21yo-young-woman, nude, submissive-girl-pose, first-person-pov, overexcited-expectation, .arab ,.Negative prompt: (disfigured), (bad art), (deformed), (poorly drawn), (extra limbs), blurry, boring, sketch, lacklustre, repetitive, cropped, hands, (disfigured), (bad art), (deformed), (poorly drawn), (extra limbs), blurry, boring, sketch, lacklustre, repetitive, cropped, hands.Steps: 10, Sampler: DPM++ 2S a, Schedule type: Karras, CFG scale: 7, Seed: 560484948, Size: 512x768, Model hash: a72645249a, Model: oxytocin_v10, Denoising strength: 0.7, Style Selector Enabled: True, Style Selector Randomize: False, Style Selector Style: base, ADetailer model: face_yolov8n.pt, ADetailer confidence: 0.3, ADetailer dilate erode: 4, ADetailer mask blur: 4, ADetailer denoising strength: 0.4, ADetailer inpaint only masked: True, ADetailer inpaint padding: 32, ADetailer model 2nd: hand_yolov8n.pt, ADetailer confidence 2nd: 0.3, ADetailer dilate erode 2nd: 4, ADetailer mask blur 2nd: 4, ADetailer denoising strength 2nd: 0.4, ADetailer inpaint only masked 2nd: True, ADetailer inpaint padding 2nd: 32, ADetailer model 3rd: genitalia.pt, ADetailer confidence 3rd: 0.3, ADetailer dilate erode 3rd: 4, ADetailer mask blur 3rd: 4, ADetailer denoising strength 3rd: 0.4, ADetailer inpaint only masked 3rd: True, ADetailer inpaint padding 3rd: 32, ADetailer model 4th: female_breast-v4.2.pt, ADetailer confidence 4th: 0.3, ADetailer dilate erode 4th: 4, ADetailer mask blur 4th: 4, ADetailer denoising strength 4th: 0.4, ADetailer inpaint only masked 4th: True, ADetailer inpaint padding 4th: 32, ADetailer version: 24.6.0, Hires upscale: 1.5, Hires upscaler: Latent, Version: v1.9.4
Image Size                      : 768x1152
Megapixels                      : 0.885
======== ././00003-560484949.png

...

    1 directories scanned
    5 image files read

C:\Automatic1111\webui\outputs\txt2img-images\2024-07-23\exiftool test>

StarGeek

The command with -p isn't meant to move any files, it's just to look at the data. The
Warning: [Minor] Advanced formatting expression returned undef for 'parameters'
is to be expected for any file that doesn't have any matching values. The one line with 'Arab' would indicate a correct match for the command.

Using the PNG image from the post I linked above, here's what I get

First, a dir to show directory contents.  The PNG file is the only file and there are no subdirectories. The PNG file contains the word "astronaut"
Y:\!temp\x\y>dir .
 Volume in drive Y is DrivePool
 Volume Serial Number is 3881-4F27

 Directory of Y:\!temp\x\y

2024-07-23  07:32 PM    <DIR>          .
2024-07-23  07:32 PM    <DIR>          ..
2024-07-18  01:09 PM           402,325 2024-07-18_20-30-09-0-1512552530.png
               1 File(s)        402,325 bytes
               2 Dir(s)  9,436,106,989,568 bytes free

Using the -p (-printFormat) option, it shows that the command gets a match with "astronaut"
Y:\!temp\x\y>exiftool -p "'${parameters;s/.*\b(astronaut|VAR2|VAR3|VAR4)\b.*/$1/is or $_=undef}'" Y:\!temp\x\y
'astronaut'
    1 directories scanned
    1 image files read

Finally, using the command with Directory to move the file.  The tree command shows the file has been moved into the newly created "astronaut" directory
Y:\!temp\x\y>exiftool "-directory<${parameters;s/.*\b(astronaut|VAR2|VAR3|VAR4)\b.*/$1/si or $_=undef}" .
    1 directories scanned
    1 directories created
    1 image files updated

Y:\!temp\x\y>tree . /f
Folder PATH listing for volume DrivePool
Volume serial number is 3881-4F27
Y:\!TEMP\X\Y
└───astronaut
        2024-07-18_20-30-09-0-1512552530.png
* 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).

KevinVideo

That did it!
Thank you all so very much!

exiftool "-directory<${parameters;s/.*\b(VAR1|VAR2|VAR3|VAR4)\b.*/$1/si or $_=undef}" .

This will save me TONS of time as I do my initial sort through tens of thousands of images!

StarGeek

If you wish to suppress the Advanced formatting expression messages, you can either use the -m (-ignoreMinorErrors) option, which will suppress all minor warnings, or you can use the -api NoWarning option to suppress just this warning
-api NoWarning="Advanced formatting expression"
* 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).


KevinVideo

Quote from: Phil Harvey on July 22, 2024, 07:10:50 PMWhat do you mean?  Sort images into 4 different directories based on the Description tag containing one of the 4 different variables?  If so, something like this could do it:

exiftool "-directory<${description;s/.*\b(VAR1|VAR2|VAR3|VAR4)\b.*/$1/i or $_=undef}" DIR

Files with none of the variables in Description won't be moved.  I've put the "\b" around the variable names to make sure they start/end at a word boundary (eg. aren't found in the middle of another word).  I've also made the match case insensitive (that's what the "i" does).

- Phil

If I had just made that DIR a . instead of a .\.
Thank you very much for a wonderful, versatile, useful, confusing tool for so many of my graphics-related needs!