#------------------------------------------------------------------------------ # 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 -imagesize -aspectratio# \ # -aspectratio -imageformat image_std.jpg # # Image Size : 1600x1200 # Aspect Ratio : 1.3333 # Aspect Ratio : 4:3 # Image Format : Std. Landscape # # > exiftool -config aspect_ratio.config -imagesize -aspectratio# \ # -aspectratio -imageformat image.jpg # # Image Size : 2500x1656 # Aspect Ratio : 1.5097 # Aspect Ratio : 625:414 # Image Format : Landscape # # Revisions: 2024/02/20 - Christian W. Correa Created # 2024/02/21 - Added a list of Aspect Ratio standard values to # the ImageFormat tag #------------------------------------------------------------------------------ # Compute the Greatest Common Divisor of two numbers 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 AR as a fraction my $ar = $prt[0]; my ($ar_width, $ar_height); # Define standard AR values my @std_square = qw/1:1/; my @std_portait = qw/2:3 3:4 3:5 4:5 3:7 5:7 3:8 5:9 8:11 9:13 9:14 9:16 9:17 9:19 10:19 18:35/; my @std_landscape = qw/3:2 4:3 5:3 5:4 7:3 7:5 8:3 9:5 11:8 13:9 14:9 16:9 17:9 19:9 19:10 35:18/; # Get width and height from ratio $ar =~ m/([\.\d]+)[:]([\.\d]+)/i; $ar_width = $1; $ar_height = $2; # Check if AR matches one of the standard values # and return Square, Portrait or Landscape return 'Square' if /$ar/i ~~ @std_square; return 'Std. Portrait' if /$ar/i ~~ @std_portait; return 'Std. Landscape' if /$ar/i ~~ @std_landscape; # If AR is not a standard value, return Portrait or Landscape return 'Portrait' if $ar_width lt $ar_height; return 'Landscape' if $ar_width gt $ar_height; return undef; }, }, }, ); 1; #end