<- Prev | Contents | Next ->

Common Tasks

## Getting information about an image

One of the most fundamental operations on an image is simply getting basic information about the image. RMagick assigns dozens of attributes to an image. All you have to do is read the image and then call the attribute methods. Here's a Ruby program that takes image filenames from the command line and then prints a variety of information about each image to the terminal.

        require 'RMagick'
        ARGV.each { |file|
            puts file
            img = Magick::Image::read(file).first
            puts "   Format: #{img.format}"
            puts "   Geometry: #{img.columns}x#{img.rows}"
            puts "   Class: " + case img.class_type
                                    when Magick::DirectClass
                                        "DirectClass"
                                    when Magick::PseudoClass
                                        "PseudoClass"
                                end
            puts "   Depth: #{img.depth} bits-per-pixel"
            puts "   Colors: #{img.number_colors}"
            puts "   Filesize: #{img.filesize}"
            puts "   Resolution: #{img.x_resolution.to_i}x#{img.y_resolution.to_i} "+
                "pixels/#{img.units == Magick::PixelsPerInchResolution ?
                "inch" : "centimeter"}"
            if img.properties.length > 0
                puts "   Properties:"
                img.properties { |name,value|
                    puts %Q|      #{name} = "#{value}"|
                }
            end
            }

## Converting an image to another format

Converting an image to another format is as simple as writing the image to a file. ImageMagick uses the output filename suffix (".jpg" for JPEG, ".gif" for GIF, for example) or prefix ("ps:" for PostScript, for example) to determine the format of the output image.

## Making thumbnails

RMagick gives you three different methods for resizing an image: resize, sample, and scale. All three are equally easy to use. Specify the number of columns and rows you want the thumbnail to have, like this:

       img = Image.new "bigimage.gif"
       thumb = img.scale(125, 125)
       thumb.write "thumb.gif"

Alternatively, just pass a single `Float` argument that represents the change in size. For example, to proportionally reduce the size of an image to 25% of its original size, do this:

       img = Image.new "bigimage.gif"
       thumb = img.scale(0.25)
       thumb.write "thumb.gif"

The resize method gives you more control by allowing you to specify a filter to use when scaling the image. Some filters produce a better-looking thumbnail at the expense of extra processing time. You can also use a blur argument, which specifies how much blurriness or sharpness the resize method should introduce.

The sample method, unlike the other two, does not do any color interpolation when resizing.

Finally, if you're using ImageMagick 5.5.2 or later, you can use the new thumbnail method. The thumbnail method is faster than resize if the thumbnail is less than 10% of the size of the original image.

## Converting a color image to grayscale

Use the quantize method with the Magick::GRAYColorspace argument. If you want real "grayscale," quantize the image to 256 colors. If you want to convert a color image to black-and-white, use 2 colors. See demo.rb.

## Making a drop shadow

Here's one way to make a drop shadow behind text. Use the gaussian_blur method to blur a light gray copy of the text, position the "shadow" slightly to the right and down, then draw the foreground text slightly to the left and up. (Click the image to see the Ruby program that created it.)

<- Prev | Contents | Next ->