Resizing Images with ImageMagick
Sure, you can open up a graphics program like GIMP and resize an image, but
what if you want to resize 10, 50 or 200 images?
convert program is just what you need.
I a previous article, I started a series on working with ImageMagick on the command line, but then I had to stop and deal with the massive migration project of moving my AskDaveTaylor.com site from one server to another while simultaneously dropping it into a completely different back-end software system—madness. I'm still fixing things and cleaning up the insane sprawl of it all.
So, my last article detoured into a discussion of scripts that helped with the migration process. I'm still working on these fast, short scripts, including one I wrote this morning:
for entry in blog/* do new=$(echo $entry | sed 's/blog\///') echo "Redirect 301 $entry $new" done
Can you track what this loop does? The only tricky part is the
new=statement that removes
blog/ from the filename
matched in the
for statement; otherwise, it's quite
Seriously though, let's return to ImageMagick. There are a ton of things you can do with the command-line utilities. But first, let me look at where I left off.
I'd just shown a simple example of ImageMagick command-line tools to identify the dimensions of an image and use that as the basis of coming up with a scaled HTML img tag. Here's the script:
#!/bin/sh identify=/usr/bin/identify scale=$1 image=$2 # needs input validation code height=$($identify $image | cut -d\ -f3 | cut -dx -f1) width=$($identify $image | cut -d\ -f3 | cut -dx -f2) newwidth="$(echo $width \* $scale | bc | cut -d. -f1)" newheight="$(echo $height \* $scale | bc | cut -d. -f1)" echo "<img src=$image height=$newheight width=$newwidth>" exit 0
(Actually, I couldn't resist tweaking it slightly if you are keeping track, but I'm still being lazy and not validating the input as of yet. You can add that code easily enough.)
$ scaledown.sh 0.5 pvp.jpg <img src=pvp.jpg height=152 width=485>
Okay, that's one way to make the display of the image be reduced on a Web page, but anyone who has done any work trying to speed up a Web site knows the huge problem here: reducing the container that displays an image doesn't reduce the image. The Web site visitor still has to download the original image, which is a huge waste of bandwidth and a performance hit.
So let's update the script to create a new, smaller version of the image as part of its output.
identify command is a great way to learn specific information
about a graphical image file, but to manipulate it, you need
to switch to
There are a million command-line options to
convert, but the one
I use here is
-resize, like this:
$ convert pvp-big.jpg -resize 0.5 pvp-0.5.jpg $ identify pvp-big.jpg pvp-0.5.jpg pvp-big.jpg JPEG 970x305 970x305+0+0 8-bit DirectClass 127kb pvp-0.5.jpg JPEG 1x1 1x1+0+0 8-bit DirectClass 1.1kb
Hmmm...you can see what's happened, right? The image went from 970x305 to 1x1. Yikes.
How did that happen? The problem is that I'm handing the wrong kind
of parameter to the
-resize option. In fact, it wants a
percentage (weirdly enough), so
-resize 50% or
50 both work:
$ convert pvp-big.jpg -resize 50 pvp-50.jpg $ convert pvp-big.jpg -resize 50% pvp-50%.jpg $ identify pvp* pvp-50.jpg JPEG 50x16 50x16+0+0 8-bit DirectClass 2.01kb pvp-50%.jpg JPEG 485x153 485x153+0+0 8-bit DirectClass 44.7kb pvp-big.jpg JPEG 970x305 970x305+0+0 8-bit DirectClass 127kb
A bit of mathematics reveals that
-resize 50 meant that the width
was scaled to 50 pixels, with the height proportionally scaled down to a
tiny 16 pixels.
-resize 50%, on the other hand,
accomplished the goal, scaling the image down to a reasonable 485x153.
So the script will need users to enter a proper percentage amount or otherwise compensate. To make it more interesting, let's make the output filename gain a suffix that denotes the new geometry (as ImageMagick likes to refer to the height x width values). In this instance, the goal is to have pvp-big.jpg shrink 50% and be copied as pvp-big.285x153.jpg.
Dave Taylor has been hacking shell scripts for over thirty years. Really. He's the author of the popular "Wicked Cool Shell Scripts" and can be found on Twitter as @DaveTaylor and more generally at www.DaveTaylorOnline.com.
Practical Task Scheduling Deployment
July 20, 2016 12:00 pm CDT
One of the best things about the UNIX environment (aside from being stable and efficient) is the vast array of software tools available to help you do your job. Traditionally, a UNIX tool does only one thing, but does that one thing very well. For example, grep is very easy to use and can search vast amounts of data quickly. The find tool can find a particular file or files based on all kinds of criteria. It's pretty easy to string these tools together to build even more powerful tools, such as a tool that finds all of the .log files in the /home directory and searches each one for a particular entry. This erector-set mentality allows UNIX system administrators to seem to always have the right tool for the job.
Cron traditionally has been considered another such a tool for job scheduling, but is it enough? This webinar considers that very question. The first part builds on a previous Geek Guide, Beyond Cron, and briefly describes how to know when it might be time to consider upgrading your job scheduling infrastructure. The second part presents an actual planning and implementation framework.
Join Linux Journal's Mike Diehl and Pat Cameron of Help Systems.
Free to Linux Journal readers.Register Now!
- SUSE LLC's SUSE Manager
- My +1 Sword of Productivity
- Managing Linux Using Puppet
- Non-Linux FOSS: Caffeine!
- Murat Yener and Onur Dundar's Expert Android Studio (Wrox)
- SuperTuxKart 0.9.2 Released
- Doing for User Space What We Did for Kernel Space
- Google's SwiftShader Released
- Parsing an RSS News Feed with a Bash Script
- SourceClear Open
With all the industry talk about the benefits of Linux on Power and all the performance advantages offered by its open architecture, you may be considering a move in that direction. If you are thinking about analytics, big data and cloud computing, you would be right to evaluate Power. The idea of using commodity x86 hardware and replacing it every three years is an outdated cost model. It doesn’t consider the total cost of ownership, and it doesn’t consider the advantage of real processing power, high-availability and multithreading like a demon.
This ebook takes a look at some of the practical applications of the Linux on Power platform and ways you might bring all the performance power of this open architecture to bear for your organization. There are no smoke and mirrors here—just hard, cold, empirical evidence provided by independent sources. I also consider some innovative ways Linux on Power will be used in the future.Get the Guide