Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

242 lines (198 sloc) 8.781 kB
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns="">
<meta name="generator" content=
"HTML Tidy for Linux/x86 (vers 1st July 2004), see" />
<title>RMagick: Common Tasks</title>
<meta http-equiv="Content-Type" content=
"text/html; charset=us-ascii" />
<meta name="GENERATOR" content="Quanta Plus" />
<meta name="Copyright" content=
"Copyright (C) 2004 by Timothy P. Hunter" />
<link rel="stylesheet" type="text/css" href="css/doc.css" />
<script type="text/javascript" src="scripts/doc.js">
<style type="text/css">
/* Styles local to this page. */
#drop_shadow {
margin-left: auto;
margin-right: auto;
width: 250px;
<h6 id="header">RMagick User's Guide and Reference</h6>
<div class="nav">
&laquo;&nbsp;<a href="imusage.html">Prev</a> | <a href=
"index.html">Contents</a> | <a href=
<h1>Common Tasks</h1>
<div id="toc">
<h2>Table of Contents</h2>
<ul style="margin-left: 15px; padding-top: 1em">
<li><a href="#info">Getting information about an
<li><a href="#convert">Converting an image to another
<li><a href="#thumb">Making thumbnails</a></li>
<li><a href="#resizing">Resizing to a maximum (or minimum)
<li><a href="#gray">Converting a color image to
<li><a href="#compressing">Compressing image files</a></li>
<li><a href="#shadow">Making a drop shadow</a></li>
<h2 id="info">Getting information about an image</h2>
<p>One of the most fundamental operations on an image is simply
getting basic information about the image. RMagick assigns dozens
of <a href="imageattrs.html">attributes</a> 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.</p>
<pre class="example">
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
when Magick::PseudoClass
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 &gt; 0
puts " Properties:" { |name,value|
puts %Q| #{name} = "#{value}"|
<h2 id="convert">Converting an image to another format</h2>
<p>Converting an image to another format is as simple as writing
the image to a file. &times;Magick 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.</p>
<h2 id="thumb">Making thumbnails</h2>
<p>RMagick gives you four different methods for resizing an
image: <a href="image3.html#resize"><code>resize</code></a>,
<a href="image3.html#sample"><code>sample</code></a>, <a href=
"image3.html#scale"><code>scale</code></a>, and <a href=
"image3.html#thumbnail"><code>thumbnail</code></a>. All four are
equally easy to use. Specify the number of columns and rows you
want the thumbnail to have, like this:</p>
<pre class="example">
img = "bigimage.gif"
thumb = img.scale(125, 125)
thumb.write "thumb.gif"
<p>Alternatively, just pass a single <code>Float</code> 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:</p>
<pre class="example">
img = "bigimage.gif"
thumb = img.scale(0.25)
thumb.write "thumb.gif"
<p>The <code>resize</code> method gives you more control by
allowing you to specify a <a href=
"constants.html#FilterType">filter</a> 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
<code>blur</code> argument, which specifies how much blurriness
or sharpness the resize method should introduce.</p>
<p>The <code>sample</code> method, unlike the other two, does not
do any color interpolation when resizing.</p>
<p>The <code>thumbnail</code> method is faster than
<code>resize</code> if the thumbnail is less than 10% of the size
of the original image.</p>
<h2 id="resizing">Resizing to a maximum (or minimum) size</h2>
<p>Say you need to make all your thumbnails no bigger than 64x64
but with the same aspect ratio as the original. Or, you don't
want to resize the image if it's already smaller than 64x64. The
<a href=
method can help.</p>
<p>The <code>change_geometry</code> method accepts an
&times;Magick <a href="imusage.html#geometry">geometry string</a>
argument and a block. The geometry string specifies how to change
the image's size: one or two numbers to specify the new size and
optional flags to describe any constraints. The
<code>change_geometry</code> method parses the geometry string
and computes new width and height values. Then it calls the
block, passing the values it computed.</p>
<p>Within the block you can do whatever you want with the new
values. Typically you'll call one of the resize methods mentioned
in the previous section and make the resized image the return
value from the block. The <code>change_geometry</code> method
then returns that value to its caller.</p>
<h2 id="gray">Converting a color image to grayscale</h2>
<p>Use the <a href=
"image3.html#quantize"><code>quantize</code></a> method with the
<a href=
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 <a href=
<h2 id="compressing">Compressing image files</h2>
<p>Many image formats, including JPEG, PDF, and BMP, support
compressed image files. The type of compression used depends on
the format. Specify the compression type by assigning a <a href=
"constants.html#CompressionType">CompressionType</a> value to the
<a href="info.html#compression">compression</a> optional argument
to the <a href="image3.html#write">write</a> method.</p>
<p>The JPEGCompression and ZipCompression types support multiple
levels of compression. Use the <a href=
"info.html#quality">quality</a> optional argument to the
<code>write</code> method. The quality attribute is a number
between 0 and 100, with 100 representing the least compression.
When you compress an image using JPEGCompression, more
compression usually results in a lower-quality image. When you
compress an image using ZipCompression, more compression usually
takes longer.</p>
<p>For more information, see the &times;Magick documentation for
the <code>-quality</code> option to the utility commands.</p>
<pre class="example">
img.write("myimage.jpg") { self.quality = 50 }
<h2 id="shadow">Making a drop shadow</h2>
<p>Here's one way to make a drop shadow behind text. Make the
shadow first by drawing the text in a light gray color. Position
the text slightly to the right and down from where the real text
will be. Then use the <a href=
"image1.html#blur_image"><code>blur_image</code></a> method to
make the shadow by blurring the text. Finally, draw the text
again in whatever color you want. <em>(Click the image to see the
Ruby program that created it.)</em></p>
<div id="drop_shadow">
<a href="javascript:popup('drop_shadow.rb.html')"><img src=
"ex/drop_shadow.gif" title="Click to see the example script"
alt="drop shadow example" /></a>
<hr />
<p class="spacer"> </p>
<div class="nav">
&laquo; <a href="imusage.html">Prev</a> | <a href=
"index.html">Contents</a> | <a href="magick.html">Next</a>
Jump to Line
Something went wrong with that request. Please try again.