2

The command sips has a great option to read information from files. The following command loops through all images and shows information on the width or height:

for i in *.jpg; do sips -g pixelWidth $i;done

for i in *.jpg; do sips -g pixelHeight $i;done 

Now I would like to read this information and use it with mv to rename the images like so:

image-widthxheight.jpg

image-1600x900.jpg

The final thing I want accomplish is, to use sips to resize images and write the new information directly into the filename.

Has anybody an idea, how I can extract the information from width and height and use it together with mv?

Phlow
  • 688
  • 4
  • 14
  • You can save the results of those commands in variables and use those I assume, e.g., `width="$(sips -g pixelWidth "$1")"`, I assume. I am not familiar with `sips` nor how to get it to resize – Eric Renouf Jan 16 '17 at 15:11
  • Thankyou, I found my solution using… I post it here :) – Phlow Jan 16 '17 at 15:40

1 Answers1

3

I found it out myself. It's a nice bash script now. Maybe not so elegant, but it works – It's also available as a gist on GitHub.

NEW VERSION THANKS TO THE ADVICE – SEE COMMENTS

#!/bin/bash
#
#   1. This script copies all *.jpg-files to a new folder
#   2. Jumps into folder and resizes all files with sips
#   3. Renames all files and uses information from sips
#
folder="resized_and_renamed"

mkdir -p "$folder"

cp *.jpg "$folder"

cd "$folder"

# RESIZE ALL IMAGES TO MAXIMUM WIDTH/HEIGHT OF 360
sips -Z 360 *.jpg

# RENAME FILES WITH INFORMATION FROM SIPS
for i in *.jpg
  do
    pixelWidth=$(sips -g pixelWidth "$i" | awk '/pixelWidth:/{print $2}')
    pixelHeight=$(sips -g pixelHeight "$i" | awk '/pixelHeight:/{print $2}')
    # REMOVE EXTENSION
    filename=${i%.jpg}
    # NOW RENAME
    mv $i ${filename##*/}-${pixelWidth}x${pixelHeight}.jpg
  done
Phlow
  • 688
  • 4
  • 14
  • Your `cp` doesn't need to be in a loop, you're copying all of the files for each file in there, so if you have 10 files you'll copy them all 10 times (assuming you don't have so many the argument list gets too long). Then, be sure to quote your variables, if any of them have an `IFS` character in them you'd end up not running the command(s) you expect, or at least with the arguments you expect, and the last 2 loops can probably be a single loop really, just resize then move – Eric Renouf Jan 16 '17 at 16:08
  • There are quite a few sub-optimal aspects in your script... I'll address one aspect in each comment so it is legible. – Mark Setchell Jan 16 '17 at 16:19
  • 1. You use `resized_and_renamed` 3 times which creates a maintenance issue, you should consider using a single variable which means there is just thing to maintain. So `sub=resized_and_renamed`, then `mkdir "$sub"` and so on everywhere. – Mark Setchell Jan 16 '17 at 16:21
  • 2. Consider using `mkdir -p "$new"` so that it can run more than once without error. Your script will error on second and subsequent invocations. – Mark Setchell Jan 16 '17 at 16:22
  • 3. Lines 12-15 should be just `cp *.jpg "$new"` not a loop. – Mark Setchell Jan 16 '17 at 16:23
  • 4. Lines 21-24 can be replaced with `sips -Z 360 *.jpg` without a loop. – Mark Setchell Jan 16 '17 at 16:24
  • 5. Your final `for` loop. You should double quote all instances of `$i` so they look like this `"$i"` if you want the script to handle filenames with spaces in them properly. – Mark Setchell Jan 16 '17 at 16:26
  • Try pasting your script, and any you write in future, into http://www.shellcheck.net/# and check its recommendations. – Mark Setchell Jan 16 '17 at 16:28
  • Hello everybody – Thank you very much from your input. I already learned a lot, but I have to deepen my knowledge. When I am finished simplifying my script with your help. I will post or edit it here. :) – Phlow Jan 18 '17 at 10:44
  • You only double quote `"folder"` the first time you use it, whereas you should always double quote it. Well done for sticking with it and sharing your work. – Mark Setchell Jan 18 '17 at 13:30
  • Ah, OK. Doin' it now. – Phlow Jan 18 '17 at 20:22