Write MaxApertureValue based on FocalLength (help with Perl snippet needed)

Started by abs_beginner, August 30, 2024, 03:56:58 PM

Previous topic - Next topic

abs_beginner

I'm using a zoom lens with a variable aperture, but the camera isn't writing the -Exif:MaxApertureValue tag. While it's probably not the most important tag, I'd like to add the tag nonetheless by creating a composite tag that returns the MaxApertueValue based on the value of exif:focallength. I know the focal lengths, at which the aperture changes. Lens is a 18-55mm 1:3.5-5.6.

I tried to come up with some code that I can put in .ExifTool_config, but I can't seem to get it to work (as I don't really know perl):


%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
        SamsungMaxAperture => {
            Require => 'FocalLength',
            ValueConv => '($FocalLength > 1 && $FocalLength < 22) ? "3.5" :
                          ($FocalLength > 21 && $FocalLength < 30) ? "4.0" :
                          ($FocalLength > 29 && $FocalLength < 40) ? "4.5" :
                          ($FocalLength > 39 && $FocalLength < 48) ? "5.0" :
                          ($FocalLength > 47 && $FocalLength < 55) ? "5.6" : undef',
        },
    },
);

Any help is appreciated.

StarGeek

Maybe try this, I haven't tested it
%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
        SamsungMaxAperture => {
            Require => 'FocalLength',
            ValueConv => q{
                return "3.5" if ($FocalLength > 1 && $FocalLength < 22);
                return "4.0" if ($FocalLength > 21 && $FocalLength < 30);
                return "4.5" if ($FocalLength > 29 && $FocalLength < 40);
                return "5.0" if ($FocalLength > 39 && $FocalLength < 48);
                return "5.6" if ($FocalLength > 47 && $FocalLength < 55);
                return undef;
            },
        },
);
1;

Edit:
Or ChatGPT's version of this (also haven't tested it)
%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
        SamsungMaxAperture => {
            Require => 'FocalLength',
            ValueConv => q{
                my %aperture_map = (
                    22 => 3.5,
                    30 => 4.0,
                    40 => 4.5,
                    48 => 5.0,
                    55 => 5.6,
                );

                for my $max_focal_length (sort { $a <=> $b } keys %aperture_map) {
                    return $aperture_map{$max_focal_length} if $FocalLength < max_focal_length;
                }

                return undef;
            },
        },
    },
);
1;

With this version, if it works correctly, any time you want to add a new entry, you would just need to add it to the aperture_map section.
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

abs_beginner

Thanks for your tips and input. Unfortunately, both scripts don't work as well.

However, after reading the documentation again, I found the following info:

QuoteIn an expression, $self is a reference to the current ExifTool object, $val is the Raw value, and $tag is the tag key.


I replaced $Focallength with $val in my original code and now it works as planned  ;D

%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
        SamsungMaxAperture => {
            Require => 'FocalLength',
            ValueConv => '($val gt 1 && $val lt 22) ? "3.5" :
                          ($val gt 21 && $val lt 30) ? "4.0" :
                          ($val gt 29 && $val lt 40) ? "4.5" :
                          ($val gt 39 && $val lt 48) ? "5.0" :
                          ($val gt 47 && $val lt 56) ? "5.6" : undef',
        },
    },
);

StarGeek

Quote from: abs_beginner on September 08, 2024, 12:30:06 PMI replaced $Focallength with $val in my original code and now it works as planned  ;D

Ah, yes, sorry, I should have caught that. The tag name can't be used as a variable because it's possible to have duplicate tag names. For example, you could be checking both XMP:Headline and IPTC:Headline, so you couldn't just use $Headline.
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype