0

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!

anubhava
  • 761,203
  • 64
  • 569
  • 643
Sam
  • 3
  • 2
  • Unfortunately not, the height, width and resolution values are different for every file. – Sam Jan 29 '15 at 19:17
  • The "dots per inch" conversion factor that you need is unfortunately device specific (camera, scanner, monitor, printer ...), so without reference to a specific device that captured the image or will display it, the conversion to inches is pretty meaningless... – twalberg Jan 29 '15 at 21:00
  • Yeah I agree and was initially confused by the request but the images in question are textile patterns that will be mapped in C4D and have already been sized to their real world repeat values. – Sam Jan 30 '15 at 18:11

1 Answers1

1
# 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.

Mike S
  • 1,235
  • 12
  • 19
  • Thanks so much! I'm going to give this a shot once I'm back at my office. And yes, I inserted the w, h, and r to indicate the width, height and resolution values, they can easily be removed. Whitespace is no issue either, fixing that is well within my limited abilities! – Sam Jan 29 '15 at 19:18
  • If OP is using `bash`, you can do division without resorting to `dc`... `w=600; ((x=w/120)); echo $x; 5` – Mark Setchell Jan 29 '15 at 22:40
  • Thanks! Mike's solution worked perfectly but I'll give this a shot as well. – Sam Jan 30 '15 at 18:12
  • You should mark this as an accepted answer, if you believe it answered your question. Thanks. – Mike S Feb 08 '15 at 19:45
  • Thanks @Mark Setchell. I note, too, that you can modify the value of a variable using Mark's technique, without having to use another variable. That is, `w=600; ((w=w/120)); echo $w` returns `5` as expected. Time to retire dc for integer arithmetic :-) . – Mike S May 18 '15 at 15:18