Democratic Underground Latest Greatest Lobby Journals Search Options Help Login
Google

Command Line Goodness - Part Two - Image Conversion

Printer-friendly format Printer-friendly format
Printer-friendly format Email this thread to a friend
Printer-friendly format Bookmark this thread
Home » Discuss » DU Groups » Computers & Internet » Open Source and Free Software Group Donate to DU
 
RoyGBiv Donating Member (1000+ posts) Send PM | Profile | Ignore Mon Jun-14-10 10:07 PM
Original message
Command Line Goodness - Part Two - Image Conversion
ImageMagick is one of the most powerful and versatile tools in the Open Source arsenal. It forms the backbone of all graphic manipulation programs under Linux and comes standard with most major distributions. (It is also available for Windows.) One could spend a month playing with its various features and still not have covered half of what it can do. GUI based applications are in fact essential for using all these tools to their fullest extent, but what often gets lost is the fact that the command line can be just as handy in many situations.

ImageMagick is not a program but a collection of programs, one of which is called "convert." I use this command frequently for simple tasks and a part of scripts to perform tasks for which I don't need a full-scale image editor and which can simply be done faster by typing a few keystrokes. Google "convert jpg to png" and just look at that massive list of links pointing to tutorials and software. You don't need it. You just need "convert."

I have an image in .jpg format, and I want to convert it to .png. How do I do so?

convert image.jpg image.png

There's not even anything fancy here. 'Convert' knows by the file extension what format is being inputted and which is to be used for output.



Let's say I want to do something a little more complicated and resize an image for uploading. I've got this huge image I took from my digital camera that's 5MB and almost 5000 pixels wide, and I just want something appropriate for posting to DU, say, 800 pixels wide, maintaining the aspect ratio so my image doesn't look all squeezed or stretched. No problem.

convert -scale 800 image2.jpg image2_sm.jpg



I just took that 5.3MB image and reduced it to a much more appropriately proportioned 800 pixels wide with the same aspect ratio and 153KB in size.

Note that in both examples I kept my original file. When constructing the syntax for the command, the first file name is the file I want to take some action on, and the second file name is what I want the new filed named.

I use these two commands all the time, but what is often the case is I have downloaded a big ol' heap of images from my camera at once, all of which are huge. I want that so I can work on them with my GUI graphics app and do lots of fancy stuff without losing detail. But I don't want that if, for example, my friend across the country wants all those pictures I took on my recent trip to my hometown. I'm not e-mailing 1.3GB worth of photos, and I have no particular desire to individually convert these one at a time.

So now we're going to get a little more advanced and write a very small script that does all this for us. Here's what it looks like:

#!/bin/bash
##
## A Script to resize all the .jpg images in a folder to a standard
## width of 800 pixels
#
## Make a new directory underneath the current directory for the resized images
#
mkdir resize
# Loop to check for all the files ending in .jpg and do the conversion
#
for img in `ls *.jpg`
do
convert -scale 800 $img ./resize/$img
done


Creating small scripts like this is not hard at all. It's more difficult to explain than to do. The first line '#!/bin/bash' simply tells the system what script interpreter we're going to be using, which in my case is always "bash." Every line with a '#' before it other than the first is of course an optional comment. I make a directory to save my new files so they're already sorted when the script is done. Then I just use a simple for/do loop, which means in plainer English:

Take a directory LiSting and store the filename in a variable called "img" then FOR every file ending with the characters ".jpg" DO the convert command on that file, placing the new image file in the directory I just created for them.

I could actually just use the final four lines from the command line itself without a script, but since I use this function a lot, it's a lot more convenient to put all these commands in a script, name is something easy to remember and put the file in my PATH (in my case, ~/bin), then chmod +x to make it executable.

Now, going into that 1.3GB directory of images I just enter this "command":

rsjpgs.sh

Converting that many images of that size will take awhile, but not nearly as long as it would have taken individually. Of course other graphics manipulation programs have batch processes that do this also, but I personally find this method more convenient and even quicker.

The whole directory is now about 40MB, which is still quite large, but I can ZIP those images up and upload it to a file sharing service rather quickly.

I hope you have enjoyed this episode of I Have Too Much Time on My Hands.

Refresh | 0 Recommendations Printer Friendly | Permalink | Reply | Top
Syrinx Donating Member (1000+ posts) Send PM | Profile | Ignore Tue Jun-15-10 03:30 AM
Response to Original message
1. another great episode!
Remember me talking about doing a re-dub of the BCS National Championship game?

Well, I still haven't got that all finished. I had it pretty much wrapped up months ago, but then I realized that ffmpeg was reducing the resolution of my video, without warning. This was after I had got the audio and video pretty much synced up. In a fit of pique, I rashly wiped out all the work I had done. I wish I had not done that. Because it's hard to motivate myself to redo a job that I had already pretty much completed.

Trying to work around the limitations of ffmpeg, I used the imagemagick suite to decode the football game into over a quarter million of still png images, consuming over 400 GB of hard drive space. And they are still there, and I guess they will remain, until I get off my lazy ass and finish the project.

No real point here, I guess. Just wanted to get my frustration off my chest. :)
Printer Friendly | Permalink | Reply | Top
 
DU AdBot (1000+ posts) Click to send private message to this author Click to view 
this author's profile Click to add 
this author to your buddy list Click to add 
this author to your Ignore list Thu Apr 25th 2024, 06:47 PM
Response to Original message
Advertisements [?]
 Top

Home » Discuss » DU Groups » Computers & Internet » Open Source and Free Software Group Donate to DU

Powered by DCForum+ Version 1.1 Copyright 1997-2002 DCScripts.com
Software has been extensively modified by the DU administrators


Important Notices: By participating on this discussion board, visitors agree to abide by the rules outlined on our Rules page. Messages posted on the Democratic Underground Discussion Forums are the opinions of the individuals who post them, and do not necessarily represent the opinions of Democratic Underground, LLC.

Home  |  Discussion Forums  |  Journals |  Store  |  Donate

About DU  |  Contact Us  |  Privacy Policy

Got a message for Democratic Underground? Click here to send us a message.

© 2001 - 2011 Democratic Underground, LLC