Resizing Images with ImageMagick
Rather than use the
bc statements from the original script,
let's make ImageMagick do the work by having this workflow:
Convert image to resized image and save as temp file.
identifyto get new dimensions of temp file.
Create new filename based on geometry.
Rename temp file to new filename with geometry specified.
It turns out it's a lot less work, because mathematics are no longer required, which is a good thing!
The hardest part is to create the new filename, which involves more lines of code than the conversion itself. It involves figuring out the filename suffix, chopping the filename up and building a new one that inserts the new image geometry in the middle.
Here's the result (it's long):
#!/bin/sh convert=/usr/bin/convert identify=/usr/bin/identify resize=$1 source=$2 if [ -z "$resize" -o -z "$source" ] ; then echo "Usage: $0 resize sourcefile"; ;exit 1 fi if [ ! -r $source ] ; then echo "Error: can't read source file $source" ; exit 1 fi # let's grab the filename suffix filetype=$(echo $source | rev | cut -d. -f1 | rev) tempfile="resize.$filetype" # temp file name # create the newly sized temp version of the image $convert $source -resize $resize $tempfile # figure out geometry, the assemble new filename geometry=$($identify $tempfile | cut -d\ -f3 ) newfilebase=$(echo $source | sed "s/$filetype//") newfilename=$newfilebase$geometry.$filetype # rename temp file and we're done mv $tempfile $newfilename echo \*\* resized $source to new size $resize. result = $newfilename exit 0
That's it. It's not incredibly complicated if you go through it step by step. In fact, go back to the four-step algorithm I presented earlier. That's almost exactly duplicated in the comments within the script.
The only nuance is the sequence for
newfilename assembly, which just
strings together a series of variables to have their values tucked
Let's give it a whirl and see what happens:
sh resize.sh 50% pvp.jpg ** resized pvp.jpg to new size 50%. result = pvp.485x153.jpg
I'm skeptical, so let's test the new image file by using
identify to get its dimensions:
$ identify pvp.485x153.jpg pvp.485x153.jpg JPEG 485x153 485x153+0+0 8-bit DirectClass 44.7kb
Perfect. More important, look at how the image size has shrunk as a result of it being scaled down 50%:
$ ls -l pvp.jpg pvp.485x153.jpg -rw-rw-r-- 1 taylor taylor 45751 Oct 9 04:14 pvp.485x153.jpg -rw-r--r-- 1 taylor taylor 130347 Sep 5 08:20 pvp.jpg
A definite win and a pretty handy script to keep around.
Of course, better positional parameter checking and a quick check to ensure that the resize parameter isn't something crazy would be good coding, but it's not a bad script that serves a very useful purpose.
So that's it. In my next article, I plan to take a look at adding embossing—text that's superimposed over a graphic—as an easy way to watermark sets of photos from the command line. Until then, cheerio!
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
- Tech Tip: Really Simple HTTP Server with Python
- Non-Linux FOSS: Caffeine!
- Murat Yener and Onur Dundar's Expert Android Studio (Wrox)
- Rogue Wave Software's Zend Server
- Doing for User Space What We Did for Kernel Space
- SuperTuxKart 0.9.2 Released
- Parsing an RSS News Feed with a Bash Script
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