Command Line – Resizing Images GIMPless
Fairly often I’ll find a desktop that I like on the Internet and download it. Usually the process is that it’s not the correct size for my desktop and I’ll open up Gimp and resize it. This can be a bit of a time taker if there is only one image. Imagemagick can do the job from the command line possibly faster.
For example, last night, I snatched a wallpaper from IamFreeman. Its a good wallpaper with rough texture, nice and simple. Now to resize it to the desktop size I use imagemagick’s mogrify or convert program. Mogrify tranforms the image meaning making mistakes = bad, convert will write a new image. I use mogrify here, if using convert specify the new filename at the end of the line.
If I just wanted the top left part of the image, I could just crop it.
mogrify Clean_is_simple_by_iAmFreeman.jpg -crop 800x600+0+0
The 0+0 specifies the top-left portion of the image. But I don’t want to just crop the top left, I want the whole image scaled down.
Since the proportions aren’t equal between my screens aspect ratio 4:3 (800×600) and the desktop I choose is 5:4 (1280×1024), I’d need to crop part of the image before I resize. The image dimensions can be listed with the identify command:
identify Clean_is_simple_by_iAmFreeman.jpg Clean_is_simple_by_iAmFreeman.jpg JPEG 1280x1024 1280x1024+0+0 DirectClass 8-bit 729.758kb 0.700u 0:02
To discover if height or width needs to be cropped, find the difference of the aspects:
echo "scale=5;(1280/1024)/(800/600)" | bc .93750
This tells me proportionally the 5:4 picture is taller. If the value is less that one then the height needs to be cropped, greater than one and the width needs to be cropped. In this instance, the height needs to be cropped 93.75%. If the width needs to be cropped:
echo "scale=5;(1280/800)/(800/600)" | bc 1.20000 echo "scale=5;(100/1.2)" | bc 83.33333
To use mogrify to crop, tell the x and y in percentages:
mogrify Clean_is_simple_by_iAmFreeman.jpg -crop 100%x93.75%+0+0
Purists will say that, this isn’t’ good. That god will walk upon the earth and smite thee. Yes, and he’d be right to. Here:
mogrify Clean_is_simple_by_iAmFreeman.jpg -crop 100%x93.75%+0+32
I specified in the previous command 0+0 (the crop value from the top left) hence the bottom portion was cut off in the previous command. Since the image is being reduced to 960 pixels height and since – (1024 – 960)/2 will give us the proper offset. The image is now centered. (+) values will crop x and y from the top left, (-) values will crop x and y from the bottom right.
+ - ___________ + | | | | | | - |___________|
OMG, what a pain!
Anyways, its easy to resize now:
mogrify -resize 800 Clean_is_simple_by_iAmFreeman.jpg
I only specified the width here as specifying height would be redundant. If the image needs to be stretched or compressed use an ! after the value (800×500!).
All in One Command
Put the steps in order and this can be done in one command.
mogrify Clean_is_simple_by_iAmFreeman.jpg -crop 100%x93.75%+0+32 -resize 800
I’ll talk about converting SVGs tomorrow and how I take screenshots.