When we talk about batch resizing images we're usually talking about taking a collection
of images and resizing them all to be the same size or scale them down the same amount.
But sometimes you're doing, kind of, the opposite.
You're taking a single image and you want to produce multiple versions of it.
App developers have to do this all the time to produce the icons in their apps.
Sometimes you need to do it for other purposes as well.
For screen shots and different things in projects.
So let's look at an example for a way to do it.
But instead of using Automator or some batch processing tool inside of an image editing
app like Photoshop or something we'll use the Terminal and create a shell script.
A shell script is something that runs in the Terminal that's more than one line and it's
a little program.
So here I've got in a folder a test image and I've got my script which is in a text
file that I can open up in TextEdit.
Here it is.
It's pretty simple.
When you write a shell script in TextEdit you it start off with the line that's the
pound symbol or hash symbol, exclamation point, and then slash bin slash bash.
This says that when this file is run as an executable it should be run using the bash
shell, which is usually what you type commands in using the terminal.
Then I set a variable.
Just two letters, FN, I use for the variable and I set it to TestImage which is the name
of the file.
In this case it's TestImage dot jpg.
Now here's the tricky part.
This is the part that you can't just do as a regular terminal command.
I'm going to loop and go through a series of sizes.
So I'm going to use for and then another variable calls SIZE in and then a whole series of numbers.
29, 40, 48, 50 all the way up to 1024.
These are going to be the sizes for the images I'm going to generate.
They happen to be the sizes you need when you create an app icon if you're an app developer.
Then after for you need to follow it up with do and done.
Between them you have the command.
The command is going to run the sips command which is something built in the terminal that
you can use to resize images.
You can just do it as a single line using a similar format just to process an image
from one size to another.
Dash Z means we want to resize it.
Then the SIZE is going to take the value of the variable size.
Put the dollar sign in front of it to indicate that.
So the first time it runs this will be a 29.
The second time it will be 40, etc.
Then with quotes around it we're going to use the value of the variable FN, so the test
image, and we put these little brackets around it to make it clear what the variable is.
We didn't need to do that here because we've got spaces around SIZE.
Now we do dash dash out.
So we're going to output it, not as the same file name not replacing the file, but we're
going to use a different file name which is going to be the thing in-between the quotes
here, the same name so TestImage, but also tag on the variable SIZE.
So in other words instead of TestImage.jpg we'll end up with TestImage 29.jpg.
Then it's going to loop.
It will do the same thing but the size is going to be 40.
So you end up taking the image, resizing it to a 40 by 40 and it's a square image in this
case so it makes it easy.
Then you end up with a TestImage of 40.jpg.
Then it will do 48 all the way to 1024.
So we'll close this and how do we run it?
Well, if we're going to run it by launching terminal.
The first thing we need to do is we need to get into this directory here.
So we'll do cd, change directory, and I'm going to drag the folder here just as a quick
way to type that name.
So now we should be in there.
I can check by using List Directory, ls, and we can see there is ProcessImage.txt and TestImage.jpg.
So we are in the right place.
Now to run this I'm going to do./ which is just a good way to force it to, instead of
accessing the file to actually run the file, and we'll do ProcessImage.txt.
A lot of people like to rename the file .sh instead of .txt but it really doesn't matter.
It's going to run it no matter what the extension is.
I'll hit Return and you can see it does a whole bunch of processing.
Outputs for each time through the loop exactly what it's doing.
I look now at the directory and I can see that it is now filled up with files that are
that size.
This was a 1024 by 1024.
Actually this was a larger image.
4000 by 3000.
So this is now 1024 by 768.
So the maximum width was 1024.
Then the other dimension, the height, follows suit.
You have that all the way down to this one, the 29 by 21.
So in the case of doing app icons, of course these are square images like I mentioned before,
you would start off with the 1024 by 1024 and you would get a repeat of that and then
one 512 by 512, 180 by 180, all the way down to 29 by 29.
But since this is a non-square image, 4000 by 3000, you will just get the maximum dimension
being that.
So this is an easy way to do it and the great thing about it is, for doing things like app
icons and things, it's very common to change that image and then want to regenerate all
of these.
So here you can just delete them, or I believe it will just override the image if I would
run it again.
I could have it very easily, in terminal here, go ahead and recreate all those little images
from this one big one.