Unix hacker stuff

Here’s a one-liner to print the aspect ratios of all the JPEG images in a directory.  I’ve broken it up onto multiple lines here, just for readability:


for f in *.jpg; do {
echo -n "$f  "; identify $f
|grep -oE "[0-9]+x[0-9]+"
|sed ’s!\(.*\)!scale=3;\1!’
|sed ’s!x!/!’|bc;
} done

(You can also substitute [[:digit:]] for [0-9].)

What it does:

• the identify command (from the ImageMagick package) prints info about the JPEG, including its dimensions as XxY, which we grep out.

• we use sed to prepend "scale=3;" so that bc prints 3 decimal places.

• sed again to change XxY to X/Y.

• pass "scale=3; X/Y" to bc, the command-line calculator, to perform the division.

Requires just ImageMagick, sed, and of course bash, all of which are present on most modern Unixes and Linuxes.

Posted by Anthony on reply

Reply to this message here:

Your name
Email
Website (optional)
Subject

HomeCreate PostArchivesLoginCMS by Encodable ]