ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: brunos on September 18, 2021, 06:52:00 AM

Title: Adding photo orientation tag to subject
Post by: brunos on September 18, 2021, 06:52:00 AM
All,

I'm dealing with about 100k photos of which about 85k have filename prefix "fco_". The photos reside in many subdirectories and have a wide variety of resolutions. To all the photos with fco_ prefix I need to add a tag to the subject field that describe the photo orientation in italian language, e.g. "foto:orizzontale", "foto:verticale", "foto:quadrata" e "panorama". All other tags in subject (unrealted to photo orientation) must be preserved. The colon in the mid of the tag is required. Only one of the listed tags is allowed. If such tag already exists, should not be added again.

In some basic-like language, I would write it like this:


val = imagewidth/imageheight
if val > 1.1 and <=1.8 and not contains tags foto:orizzontale, foto:verticale, foto:quadrata, foto:panorama then
  add tag foto:orizzontale
elseif contains tags foto:verticale, foto:quadrata, foto:panorama then
  remove tags foto:verticale, foto:quadrata, foto:panorama
  add tag foto:orizzontale
endif

if val > 1.8 and and not contains tags foto:orizzontale, foto:verticale, foto:quadrata, foto:panorama then
  add tag panorama
elseif contains tags foto:verticale, foto:quadrata, foto:orizzontale then
  remove tags foto:verticale, foto:quadrata, foto:orizzontale
  add tag panorama
endif

.... and two more times for the remaining 2 tags.


What would be the syntax for exiftool?

Thanks in advance!!

Kindest regards
Bruno

Title: Re: Adding photo orientation tag to subject
Post by: StarGeek on September 18, 2021, 10:55:21 AM
Since I don't know Italian and Google translate returns "Square" for the word "quadrata" and that isn't an orientation value, I'll use English in this example.

Also, this assumes that all the files actually have an Orientation tag and none of them used a "Mirrored" orientation.

So try this, replacing the foto: values with your own
exiftool -if "defined $Orientation and $Subject!~/foto:(Horizontal|rotate 180|rotate 90 CW|rotate 270 CW)/" "-Subject+<${Orientation#;my %orient=(1,'foto:Horizontal',3,'foto:rotate 180',6,'foto:rotate 90 CW',8,'foto:rotate 270 CW');$_=$orient{$_}}" /path/to/files
Title: Re: Adding photo orientation tag to subject
Post by: brunos on September 18, 2021, 04:00:43 PM
Thank you, Stargeek, to looking into it and giving a helping hand. I will try it asap and will let you know.

Kindest regards
Bruno
Title: Re: Adding photo orientation tag to subject
Post by: brunos on September 18, 2021, 04:20:41 PM
Hello Stargeek,

I checked right now on few photos and found out that some have "-" in place of -Orientation, such as:
C:\ARCHIVIO\folderstemp\0000>exiftool -T -filename -imagewidth -imageheight -Orientation .

fco_Bruno Stivicevic_1895.JPG   2592    3888    -
fco_Bruno Stivicevic_1896.JPG   3888    2592    Horizontal (normal)
fco_Bruno Stivicevic_1897.JPG   3831    1501    Horizontal (normal)

I have no idea why it is so. Since a plenty of photos are downloaded from various sources, it is likely that most of them won't have orientation tags, that's why I taught the safer way would be to do the math on imagewidth and imageheight?

Thanks again
Bruno
Title: Re: Adding photo orientation tag to subject
Post by: StarGeek on September 18, 2021, 04:45:49 PM
Quote from: brunos on September 18, 2021, 04:20:41 PM
I checked right now on few photos and found out that some have "-" in place of -Orientation,
...
I have no idea why it is so.

It's because you're using the -f (-ForcePrint) option (https://exiftool.org/exiftool_pod.html#f--forcePrint) which forces those tags that do not have an Orientation to have a value of -.  Assuming that those files are already orientated in the landscape/horizontal orientation, you can make this change
8,'foto:rotate 270 CW'
into
8,'foto:rotate 270 CW','-','foto:Horizontal'
Title: Re: Adding photo orientation tag to subject
Post by: brunos on September 18, 2021, 05:09:07 PM
Hi Stargeek,

Is the -f option a default one? As I can't see that I'm using it...?

Thanks!!!
Bruno
Title: Re: Adding photo orientation tag to subject
Post by: StarGeek on September 18, 2021, 05:25:30 PM
Maybe the -T (-table) option (https://exiftool.org/exiftool_pod.html#T--table)?

The -if option specifically excludes files that don't have an orientation tag.

Hmmm...  I'm not sure now.  Quick testing shows even with the -f or -T options, I can't get an output of a dash with that command, even making the change above to account for a dash.

Maybe your files actually have a dash in the Orientation.  If so, some program is incorrectly writing the EXIF.

Title: Re: Adding photo orientation tag to subject
Post by: brunos on September 19, 2021, 03:30:40 AM
It is possible that there are wrong EXIFs, but it is even more possible that there are plenty of photos without EXIF at all (downloaded from external sources). Which however leaves me in limbo regarding math with multiple ifs & elseifs. Writing some appropriate code in .config unfortunately exceeds my capabilities... Anyway, I cal always return to tag the photos manually (sigh).

Thanks again
Bruno
Title: Re: Adding photo orientation tag to subject
Post by: greybeard on September 19, 2021, 03:39:29 AM
I'd agree that it most likely that the reason you are getting the dash in the command "exiftool -T -filename -imagewidth -imageheight -Orientation ." is that the Orientation tag is missing - it would have to be some strange XMP tag or something for an actual dash to be possible.

The best test would be to run "exiftool -a -G1 -n  -filename -imagewidth -imageheight -Orientation ." against your C:\ARCHIVIO\folderstemp\0000 directory.

If the Orientation tag is missing for fco_Bruno Stivicevic_1895.JPG then nothing will be reported for Orientation - if there is a strange Orientation tag this command will show you where it is.

I notice that your example shows the problem with assigning horizontal based on missing Orientation as fco_Bruno Stivicevic_1895.JPG would appear to be portrait (or cropped)
Title: Re: Adding photo orientation tag to subject
Post by: brunos on September 19, 2021, 03:56:15 AM
Hi Greybeard, thank you for helping. The results are:
C:\ARCHIVIO\folderstemp\0000>exiftool -a -G1 -n  -filename -imagewidth -imageheight -Orientation .
======== ./198962411_2905502769764828_8590232671525652158_n.jpg
[System]        File Name                       : 198962411_2905502769764828_8590232671525652158_n.jpg
[File]          Image Width                     : 1080
[File]          Image Height                    : 1080
======== ./fco_Bruno Stivicevic_1895.JPG
[System]        File Name                       : fco_Bruno Stivicevic_1895.JPG
[File]          Image Width                     : 2592
[File]          Image Height                    : 3888
======== ./fco_Bruno Stivicevic_1896.JPG
[System]        File Name                       : fco_Bruno Stivicevic_1896.JPG
[File]          Image Width                     : 3888
[File]          Image Height                    : 2592
[IFD0]          Orientation                     : 1
======== ./fco_Bruno Stivicevic_1897.JPG
[System]        File Name                       : fco_Bruno Stivicevic_1897.JPG
[IFD0]          Image Width                     : 3831
[File]          Image Width                     : 3831
[IFD0]          Image Height                    : 1501
[File]          Image Height                    : 1501
[IFD0]          Orientation                     : 1
    1 directories scanned
    4 image files read

C:\ARCHIVIO\folderstemp\0000>


It looks like that the first two photos have no orientation tag (they are from different sources), while other two have it.
Title: Re: Adding photo orientation tag to subject
Post by: brunos on September 19, 2021, 04:02:06 AM
One thing: I can try to make 4 rounds instead of one: in the first round I would assign only the foto:horizontal tag to the photos which have imagewidth/imageheight > 1.1 and <=1.8; and in the second round I would assign foto:vertical and so on. That to reduce the problem to the single "if" instead to have to deal with four consecutive "ifs".

What would be the exiftool syntax for  imagewidth/imageheight > 1.1 and imagewidth/imageheight <=1.8?
Title: Re: Adding photo orientation tag to subject
Post by: brunos on September 19, 2021, 05:27:09 AM
I did some tests, to no avail. Following an example in Calculations with exiftool - "Not a floating point number"?, I created asprat.config file as:

    MyAspRat => {
        Require => {
            0 => 'ImageWidth',
            1 => 'ImageHeight',
        },
        ValueConv => '$val[0] / $val[1]',
    },


and I called it in the command line as:

C:\ARCHIVIO\folderstemp\0000>exiftool -config AspRat.config -a -G1 -n  -filename -imagewidth -imageheight -myAspRat .

I would expect that as the third value in a result line I would be getting the imagewidth divided by imageheight but it is not so:

C:\ARCHIVIO\folderstemp\0000>exiftool -config AspRat.config -a -G1 -n  -filename -imagewidth -imageheight -myAspRat .
======== ./198962411_2905502769764828_8590232671525652158_n.jpg
[System]        File Name                       : 198962411_2905502769764828_8590232671525652158_n.jpg
[File]          Image Width                     : 1080
[File]          Image Height                    : 1080
======== ./fco_Bruno Stivicevic_1895.JPG
[System]        File Name                       : fco_Bruno Stivicevic_1895.JPG
[File]          Image Width                     : 2592
[File]          Image Height                    : 3888
======== ./fco_Bruno Stivicevic_1896.JPG
[System]        File Name                       : fco_Bruno Stivicevic_1896.JPG
[File]          Image Width                     : 3888
[File]          Image Height                    : 2592
======== ./fco_Bruno Stivicevic_1897.JPG
[System]        File Name                       : fco_Bruno Stivicevic_1897.JPG
[IFD0]          Image Width                     : 3831
[File]          Image Width                     : 3831
[IFD0]          Image Height                    : 1501
[File]          Image Height                    : 1501
    1 directories scanned
    4 image files read


If I use a shorter command line such as:

C:\ARCHIVIO\folderstemp\0000>exiftool -config AspRat.config -filename -imagewidth -imageheight -myAspRat .

not getting the 3rd value at all.

What is wrong here?

Thanks
Bruno
Title: Re: Adding photo orientation tag to subject
Post by: brunos on September 19, 2021, 08:20:00 AM
After some more digging I've found out that the user-defined part was incomplete: it should be like this one:
%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
    MyAspRat => {
        Require => {
            0 => 'ImageWidth',
            1 => 'ImageHeight',
        },
        ValueConv => '$val[0] / $val[1]',
    },
    },
);
1;  #end


And then, it returns the aspect ratio (which is a bit more than I had before):
C:\ARCHIVIO\folderstemp\0000>exiftool -config AspRat.config -filename -imagewidth -imageheight -MyAspRat .
======== ./198962411_2905502769764828_8590232671525652158_n.jpg
File Name                       : 198962411_2905502769764828_8590232671525652158_n.jpg
Image Width                     : 1080
Image Height                    : 1080
My Asp Rat                      : 1
======== ./fco_Bruno Stivicevic_1895.JPG
File Name                       : fco_Bruno Stivicevic_1895.JPG
Image Width                     : 2592
Image Height                    : 3888
My Asp Rat                      : 0.666666666666667
======== ./fco_Bruno Stivicevic_1896.JPG
File Name                       : fco_Bruno Stivicevic_1896.JPG
Image Width                     : 3888
Image Height                    : 2592
My Asp Rat                      : 1.5
======== ./fco_Bruno Stivicevic_1897.JPG
File Name                       : fco_Bruno Stivicevic_1897.JPG
Image Width                     : 3831
Image Height                    : 1501
My Asp Rat                      : 2.55229846768821
    1 directories scanned
    4 image files read

C:\ARCHIVIO\folderstemp\0000>


Now begins the struggle for the part that will test a range of MyAspRat values, such as "if myasprat >1.1 and myasprat<= 1 ...
Title: Re: Adding photo orientation tag to subject
Post by: brunos on September 19, 2021, 08:51:41 AM
Just for the record:

Add foto:orizzontale (if no other "foto:xxx" tag exists already) to all photos in the current folder
exiftool -config AspRat.config -if "$MyAspRat > 1.1 and $MyAspRat <= 1.8 and $Subject!~/foto:(orizzontale|verticale|quadrata|panorama)/" "-Subject+=foto:orizzontale" .

Test what has been added
exiftool -config AspRat.config -filename -imagewidth -imageheight -MyAspRat -subject .

So far so good. Thanks to all who helped!!!