Sam
Sam

Reputation: 3

File name contains pixel dimensions that need to be converted to inches

I am about to receive 1000+ files that will need to be renamed so the names contain their print dimensions. I've figured out how to extract the pixel dimensions and rename the files to include that with exiftool but I'm not sure what the best approach is for automating the pixels to inches calculation and subsequent renaming.

This is what exiftool is doing:

exiftool '-filename<%f_h${ImageHeight}w${ImageWidth}r${XResolution}.${FileType}'

So the original file name of 466001004_cc.jpg gets renamed to 466001004_cc_h5280w4080r120.jpg and now I need to divide 5280 and 4080 by 120 to get the inch values.

Ultimately the file name should look like this: 466001004_cc_044x034.jpg

Any help would be greatly appreciated, thanks!

Upvotes: 0

Views: 335

Answers (1)

Mike S
Mike S

Reputation: 1361

# file looks like: 466001004_cc_h5280w4080r120.jpg
for file in *; do
    read basename w h <<< $(echo $file | awk -F_h '{print $1 " " $2}' | awk -Fr '{print $1}' | awk -Fw '{print $1 " " $2}')
    w=$(echo "$w 120 / p" | dc)
    h=$(echo "$h 120 / p" | dc)
    mv $file ${basename}_${w}x${h}.jpg
done

I assume that the only _h in the filename was inserted by you. If that's part of the base filename, my script will break.

I also assume there is no whitespace in the filename (would break the "for" command), and that there aren't so many files that the line length is longer than Bash can handle.

Easy test:

for file in *; do echo $file; done

...if it dumps all your filenames correctly, you're good. With a ton of files, you would need to replace the for with something like

find . -name "*.jpg" | while read file; do
    file=$(basename $file)

...actually, that's a pretty simple fix. The rest of the script would remain unchanged.

Upvotes: 1

Related Questions