#------------------------------------------------------------------------------ # File: aspect_ratio.config # # Description: Definition for a Composite "AspectRatio" tag to compute the # Storage Aspect Ratio of an image based on the "ImageWidth" and # "ImageHeight" tags. Also the Composite "ImageFormat" tag is # defined to identify whether the image is in square, portrait # or landscape format. # # Notes: See https://en.wikipedia.org/wiki/Aspect_ratio_(image) for a # definition of Aspect Ratio # # Examples: # > exiftool -config aspect_ratio.config -aspectratio# \ # -aspectratio -imageformat image.jpg # # Aspect Ratio : 1.3333 # Aspect Ratio : 4:3 # Image Format : Landscape # # # Revisions: 2024/02/20 - Christian W. Correa Created #------------------------------------------------------------------------------ sub gcd { my ($A, $B) = @_; return 0 == $B ? $A : gcd($B, $A % $B); } %Image::ExifTool::UserDefined = ( 'Image::ExifTool::Composite' => { AspectRatio => { Require => { 0 => 'ImageWidth', 1 => 'ImageHeight', }, ValueConv => 'sprintf("%.4f", $val[0]/$val[1])', PrintConv => q{ my $width = $val[0]; my $height = $val[1]; my $gcd = gcd($width, $height); my $numerator = $width / $gcd; my $denominator = $height / $gcd; return "$numerator:$denominator"; }, }, ImageFormat => { Require => { 0 => 'AspectRatio', }, PrintConv => q { # Get Aspect Ratio as a fraction my $ar = $prt[0]; my ($ar_width, $ar_height); # Get width and height from ratio $ar =~ m/([\.\d]+)[:]([\.\d]+)/i; $ar_width = $1; $ar_height = $2; return 'Square' if $ar_width eq $ar_height; return 'Portrait' if $ar_width lt $ar_height; return 'Landscape' if $ar_width gt $ar_height; return undef; }, }, }, ); 1; #end