Selecting images with a particular Aspect Ratio

Discuss and share scripts and script files...
Post Reply
klownboy
Posts: 4403
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.7171 at 100% 2560x1440

Selecting images with a particular Aspect Ratio

Post by klownboy »

Hi y'all (I couldn't help myself, now living in Virginia transplanted from New England),

Over in the Tips & Tricks area I had recently posted a short script to select image files in a folder that were a specific aspect ratio. That script applied to only one specific aspect ratio, 16x9. I tweaked the script to tighten up the code a bit and provided a menu to select different aspect ratio JPGs. You could assign it to a CTB or right click of an existing CTB. See screen shot.
aspect_ratio.JPG
aspect_ratio.JPG (14.75 KiB) Viewed 4785 times
It can be helpful if you're like me and at times need to select files that are 16x9, 8x5, 4x3, or 3x2 aspect ratio from various camera photo or desktop wallpaper folders to move or copy JPG files to other computers with different screen aspect ratios. There are no existing filters for aspect ratio and there is no direct meta property for it. To obtain the actual aspect ratio you have to divide the image horizontal dimension (x) by it's vertical dimension (y). I know that aspect ratios of cameras and computer screens vary here and across the pond so if anyone would like to me to tweak the script for other aspect ratios, I'd be glad to do it.

Code: Select all

"Image Aspect Ratio to Select|:preview";
"    16x9 or 1.78";
    sub _aspect;
"    8x5 or 1.60";
    sub _aspect;
"    3x2 or 1.50";
    sub _aspect;
"    4x3 or 1.33";
    sub _aspect;

"_aspect";
   $aspect_ratio = (gettoken(caller("caption"), -1, "or ", "t"));
   selfilter "*.jpg", f;                            // select jpg files
   End GetInfo("CountSelected") < 1, "There are no JPG files in this folder.";
    $images_selected = get("selecteditemsnames", "|"); 
    #251;                                           // unselect all files
    $orient = get("propertyindex", "Orientation");   //requires v12.60.0015, Orientation name varies across systems in German "Ausrichtung"
    foreach($file, $images_selected, "|") {
          $rotate = property("#$orient", $file);        // image orientation
       if (($rotate == "Rotate 90 degrees") || ($rotate == "Rotate 270 degrees")) {
          $ratio = round((property("ImageY", $file)) / (property("ImageX", $file)), 2); }
       else {
          $ratio = round((property("ImageX", $file)) / (property("ImageY", $file)), 2); }
       if ($ratio == $aspect_ratio) {
          $selected = $file;
          selectitems $selected, , ,"a"; }
    }
Don, maybe at some point, selection of image files based on aspect ratio could be another potential "power filter".

Note: the big black dot or bell beside the aspect ratios on the menu may not show as it does on the screen shot. The script is saved as an ANSI file and you can duplicate the bell by holding down the alt key and hitting 007 on the numpad. I loaded a folder full of images with all four of the aspect ratios to test and it appears to be working fine.

Edit on July 23, 2013: update to use new get("propertyindex", propertyname) in v12.60.0015 for more compatibility across systems, but you still may have to change the actual property e.g., "Rotate 90/270 degrees" line depending on language/translation.

For expanded compatibility on other systems/languages please see highend's version post #7.

Thanks,
Ken
Last edited by klownboy on 23 Jul 2013 18:58, edited 4 times in total.

highend
Posts: 14606
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Selecting images with a particular Aspect Ratio

Post by highend »

Hi Ken,

it doesn't work on my system.

#245 returns "Blitz, automatisch" instead of an angle...

I guess it's because of the model of the camera, a Canon DIGITAL IXUS 400.
Maybe it doesn't write that meta data into the picture (at least I can't find
anything in the picture's properties that looks like an angle)...

Regards,
Highend
One of my scripts helped you out? Please donate via Paypal

klownboy
Posts: 4403
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.7171 at 100% 2560x1440

Re: Selecting images with a particular Aspect Ratio

Post by klownboy »

Hi highend, thanks for testing the script.

That meta value #245 is simply "Orientation". I substituted "Orientation" (in quotes) for #245 and it still works for me. I also tried the script again with Canon SLR photos I had downloaded from Flicker and it worked fine for those too (I have a Panasonic). The orientation value should be present in the "meta", but if you're using a different language, the property name for "Orientation" may also be different.

EDIT: Sorry I was wrong, substituting Orientation for #245 does not work. In XYplorer's Metadata it's called "Orientation" but in other programs like ImageMagic it's different. I have to do more checking to see if there is a proper name substitute for #245 that works generically. Could you try using TheQwerty's and serendipity's Properties-v3.xys script http://www.xyplorer.com/xyfc/viewtopic. ... 3&start=15 to see what you have as a substitute for #245? By the way, Don calls #245 "Orientation" also under Tools-Configuration-File Info Tips. Make sure it's checked and what's it called in your case?

Run "Meta", the CTB on an example image file and see what "orientation" is called. Orientation is usually "Normal" for landscape photos. Typically for portrait mode the orientation is Rotated, so the orientation instead of being called "Normal", it's called "Rotate 90 degrees" or "Rotate 270 degrees". The "Orientation" may be different in another language so the script line which has "Rotate 90 degrees" and "Rotate 270 degrees" would also have to be changed by the language equivalent if different. I had to do a check for orientation in the script because the aspect ratio would be wrong if not - the "X" and "Y" values are essentially reversed.

Thanks again,
Ken

highend
Posts: 14606
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Selecting images with a particular Aspect Ratio

Post by highend »

I was already using the properties script to see, which properties are available for this file.

Now I know that a not rotated image provides "Normal" as the description, it seems that
#254 should be the correct one on my system.

Can you link me a picture (to download it) that is identified as
"Rotate 90 degrees" or
"Rotate 270 degrees"

on your system to let me check what #254 provides when I run the properties script on it?
One of my scripts helped you out? Please donate via Paypal

klownboy
Posts: 4403
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.7171 at 100% 2560x1440

Re: Selecting images with a particular Aspect Ratio

Post by klownboy »

Hi highend,

I sent you 2 photos via email, one with a orientation of "Rotate 90 degrees" and the other with a orientation "Rotate 270 degrees" from 2 different cameras, one a Panasonic and the other my girlfriend's (actually her library's) Canon. It seems odd that the number for orientation is different. I suppose it's either a language/system difference.

Edit: I'm curious to know though, what does XY's Metadata (under the File menu) call it on your system? On my system, it's called: Orientation: Normal, Orientation: Rotate 90 degrees, or Orientation: Rotate 270 degrees. It would be nice if Don could have a common name (i.e., Orientation) that worked across these different systems like XP, Vista and Windows 7 & 8 since we know the number value is different. This would allow the property name to be used in both "report" and "property" commands across the different systems. He mentions in the help though that Microsoft is always changing the property values and names. His statements lead you to believe that the property number index is XYplorer specific.

The help states that using

Code: Select all

::msg property("#245 n");
will provide the property name and the property and it does. The resultant message is: "Orientation: Rotate 270 degrees" so, "Orientation" is indeed the name of the property, yet it doesn't appear that we can use that "name" with property or report. It's strange that I can use property("ImageY") in the AB but not property("Orientation") unless our syntax is all wrong.

Thanks,
Ken
Last edited by klownboy on 21 Jul 2013 20:39, edited 1 time in total.

highend
Posts: 14606
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Selecting images with a particular Aspect Ratio

Post by highend »

Thanks Ken, got them.

Metadata:
Ausrichtung: Normal
Ausrichtung: Um 90° drehen
Ausrichtung: Um 270° drehen

#254 is the correct id.

Windows Server 2012 Datacenter German
One of my scripts helped you out? Please donate via Paypal

highend
Posts: 14606
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Selecting images with a particular Aspect Ratio

Post by highend »

Hi Ken,

I've modified your script a bit since we got the new "get(propertyindex,..." command.
If it doesn't work on a different language than english / german only the variable
$prop_orientation has to be extended with the correct keyword (removes the necessity
to add a .ini file that stores names with their corresponding property for each language).

To avoid problems with the translation for the "Rotate 90 degrees" I've used a regex to
get only the number from it.

Btw, different languages use different characters (at least "," or ".") for delimetering floats.
E.g. in germany it's a "," instead of a "." so the old script returned always a false
for the $ratio == $aspect_ratio test :)

Does this one work on your system?

Code: Select all

	"Image Aspect Ratio to Select|:preview";
	"    16x9 or 1.78";
	sub _aspect;
	"    8x5 or 1.60";
	sub _aspect;
	"    3x2 or 1.50";
	sub _aspect;
	"    4x3 or 1.33";
	sub _aspect;

	"_aspect";
	$aspect_ratio = (gettoken(caller("caption"), -1, "or ", "t"));
	$images = formatlist(folderreport("filesrel", "r", , , , "|"), "f", "|", "*.jpg"); // select jpg files
	$prop_orientation = "orientation|ausrichtung"; // Translations for the "orientation" property
	end gettoken($images, "count", "|") < 1, "There are no JPG files in this folder.";

	// Get the correct propertyindex for the image orientation
	foreach ($property,	$prop_orientation, "|") {
		$property_index = get("propertyindex", $property);
		if ($property_index != -1) {
			$orientation = "#$property_index";
			break;
		}
	}

	// Process all files
	$selected = "";
	foreach($file, $images, "|") {
		$rotate = regexreplace(property($orientation, $file), ".*?(\d+).*", "$1"); // image orientation in degrees

		if (($rotate == 90) || ($rotate == 270)) {
		$ratio = replace(round((property("ImageY", $file)) / (property("ImageX", $file)), 2), ",", ".");
		} else {
			$ratio = replace(round((property("ImageX", $file)) / (property("ImageY", $file)), 2), ",", ".");
			}
			if ($ratio == $aspect_ratio) {
				$selected = $selected . $file . "|";
			}
		}
		selectitems $selected;
Last edited by highend on 23 Jul 2013 18:53, edited 1 time in total.
One of my scripts helped you out? Please donate via Paypal

klownboy
Posts: 4403
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.7171 at 100% 2560x1440

Re: Selecting images with a particular Aspect Ratio

Post by klownboy »

Hi highend,

I tried your script on a temporary folder I filled the other day full of JPGs of the 4 different aspect ratios as well as others and it worked great. Thank you. I did wonder why it was stepping through the script at first since I had stepping off in the Menu/Scripting until I realized you had inadvertently left a step command in the script. I wanted to step through it in any case to see how your new changes were working and how they worked (I'm still relatively new at this). Your version may or may not be any faster using "folderreport", but regardless, it does eliminate the flicker of selecting/highlighting the files and then unselecting them so that's a very good thing and it probably does make it faster as well.

Concerning your statement concerning "delimiting floats", does that mean in Germany the aspect ratio would be seen as "1,78" and for that reason you had to replace the "," with "." for consistency? So, for me using the English version, I wouldn't necessarily need to have the replace command since $ratio == $aspect_ratio does equate just fine.

More importantly your version provides even more compatibility than the tweaked version I posted above which incorporated the new "get(propertyindex,...".

I will note on my first post to see your version for increased compatibility.

Thanks again,
Ken

highend
Posts: 14606
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Selecting images with a particular Aspect Ratio

Post by highend »

until I realized you had inadvertently left a step command in the script
Emh... Fixed^^
Your version may or may not be any faster using "folderreport", but regardless, it does eliminate the flicker of selecting/highlighting the files and then unselecting them
I'd say that folderreport() is probably faster with a lot of files instead of using select but it's rather unimportant because what takes more time is getting the properties of each file. But I just wanted to get rid of the selection mechanism because of the flickering. It just looked weird :)

Code: Select all

does that mean in Germany the aspect ratio would be seen as "1,78"
Exactly.

I've updated my last posting and fixed an indentation issue (mixed tabs & spaces) as well.
One of my scripts helped you out? Please donate via Paypal

Post Reply