Showing posts with label ImageMagick. Show all posts
Showing posts with label ImageMagick. Show all posts

2009-07-09

More on ImageMagick

I've talked about ImageMagick before, and I figured it would be fun to cover some more features available with it. The reason I like ImageMagick is that it is really convenient for when you want to do the same thing to a large group of pictures. You may find yourself with some digital pictures that you want to make look more artsy by changing them to black and white or maybe to a sepia tone.

The first thing I always do before using ImageMagick is to make backup copies of all my pictures I'm going to be editing. Once you're ready to get started, converting pictures to black and white is really easy:

$ mogrify -monochrome input.jpg

It really is that simple. The above will only convert the image named input.jpg. If you wanted to do the same thing to all .jpg images in a directory, you could do this:

$ mogrify -monochrome *.jpg

Sepia tone allows for a little more tweaking, because it allows you to input a threshold as a percent of the intensity, ranging from 0 - 99.9%. This is something that you would want to play with to get the desired result, but from my experience 80% is usually a good starting point. We could convert an image to sepia tone like this:

$ mogrify -sepia-tone 80% input.jpg

Again, you could use the same command on all .jgp images in a directory:

$ mogrify -sepia-tone 80% *.jpg

This still only scratches the surface of what you can do with ImageMagick. I'll be covering more of the features in future posts.

See you next time

2009-06-03

Convert Raster Graphics to Vector Graphics

Have you ever wanted to convert a raster image (.jpg, .png, etc.) to a vector image (.svg)? Maybe not yet, but in my post on Installing Songbird, when we added Songbird to the menu, we did not have an icon to set for the menu item. It turns out that this is because we need a .svg image and the Songbird tarball only came with a .png image. No worries, though, we'll go through converting raster to vector and, specifically, how to convert the songbird.png image to a vector image for use on our menu.

In order to do this, we're going to make use of two programs: ImageMagick and Inkscape. ImageMagick is a powerful command line utility for all kinds of graphics manipulations. Inkscape is more widely known as an open source GUI vector graphics editor; however, it also comes with its own set of command line tools. Let's make sure we have these two programs installed:

# apt-get update
# apt-get install imagemagick inkscape

While it is possible to do this though the Inkscape GUI, I find the whole process easier and faster right from the command line. We will use ImageMagick to resize images, if necessary (refer to my first ImageMagick post if you have questions about resizing images with ImageMagick), and we will use Inkscape to do the actual conversion. The command to do the actual conversion is actually very simple:

$ inkscape InputImage.png --export-plain-svg=OutputImage.svg

When you run this command, expect to see a message similar to the following:

(inkscape:30498): GLib-GObject-CRITICAL **: g_object_set_qdata_full: assertion `quark > 0' failed

This is OK, your image still got converted. Now, let's use this to get the songbird.png image in place for use on our menu. Let's start by copying the songbird.png image into out home directory to make things easier:

$ cp /usr/share/Songbird/songbird.png ~/

Now we can use the mogrify command to resize this to a 48x48 pixel image and then use the inkscape command to convert this to songbird.svg:

$ mogrify -resize 48x48 songbird.png
$ inkscape songbird.png --export-plain-svg=songbird.svg

Now, we'll just copy our new songbird.svg image into the same directory as all the other menu icons:

# cp songbird.svg /usr/share/icons/hicolor/scalable/apps/

If we set up Songbird in your menu the way I described before, then all we will need to do is to restart X (Ctrl + Alt + Backspace) and our new songbird.svg image should should show up in our menu.

See you next time

2009-05-31

Batch Image Editing with ImageMagick

If you take a lot of digital pictures, you may find that there are times when you want to do the same thing to a group of images. You could use the GIMP to edit all of your pictures one at a time, but that is a time-consuming process, especially if you just want to make thumbnails of your vacation pictures. The good thing is that there is a simple, yet powerful, command line utility that you can use to edit photos en masse: ImageMagick.

Let's start by making sure that we have ImageMagick installed:

# apt-get update
# apt-get install imagemagick

I'm going to start by going over two of the features of ImageMagick that I use the most frequently: resizing and cropping images. There are a lot more features available in ImageMagick, and I will probably cover more of them future posts.
  • Batch Resizing Images
    Digital SLR cameras are great for taking high quality pictures, the problem is that they typically take very large pictures that are too large for a lot of uses, such as posting on a blog or using in a presentation. Generally, the first thing I will do is open one of my pictures in the gthumb or some other image viewer that allows me to zoom in and out to find the percentage I want to shrink the images to.

    The next thing I do is I will make copies of all my images that I am going to edit so that I can retain the originals; I do this in a separate directory to minimize my own confusion:

    $ cd /[path-to-images]/
    $ mkdir thumb
    $ cp *.jpg thumb/
    $ cd thumb

    Now, we just use the mogrify command with the -resize option to resize our images. Let's say we want to resize them all to 20% of their original size:

    $ mogrify -resize 20% *.jpg

    Depending on the number of images you are resizing, this command may take a little bit to run, but it is a lot faster than editing each image by hand. If you wanted to resize your images to a specific size, say to create background images for your Mini 9, we would specify 1024x600 pixels rather than a percent:

    $ mogrify -resize 1024x600 *.jpg

    That pretty much does it for resizing pictures with ImageMagick.

  • Batch Cropping Images
    For my post on Installing Debian, I ran the installation in VirtualBox and took screen shots of the VirtualBox window to get the images I posted. The problem with this was, there was some of the window showing in the screen shots that I did not want to include in my post as you can see below:

    The good news is that this window border was identical on all of my screen shots, so I just had to figure out what I needed to crop on one image, and I could use that information in ImageMagick to crop all of the screen shots that I took.

    In order to figure out what I needed to crop, I opened one of the images in the GIMP. Using either the rectangle selection or the crop tool, I selected the region that I wanted to crop out of the image. Then looking on the tool window, I was able to see the position of the top-left corner of the selection as well as the dimensions of the selection:

    The Position section, indicated above in red, is what we will use for our offset with mogrify, and the Size section, indicated above in blue, is what we will use to define the size of the area we want to crop. We just need to use the information above with the mogrify command with the -crop option. But first, we'll copy the images to keep a record of the originals:

    $ cd /[path-to-images]/
    $ mkdir cropped-images
    $ cp *.png cropped-images/
    $ cd cropped-images
    $ mogrify -crop 639x480+1+44! *.png

    The exclamation point in the above command is there to make ImageMagick move the viewport for the image to the same as the crop offset. That may sound a little confusing, so I've included a screen shot of what a picture looks like in the GIMP if you don't use the exclamation point in the above command:

    So, you are best to use the exclamation point in the command, and when you use it, you're picture will look like this:

    That's about all there is for batch cropping images with ImageMagick

See you next time.