Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
196 lines (195 sloc) 7.96 KB
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<meta name="generator" content=
"HTML Tidy for Linux/x86 (vers 1st September 2002), see">
<title>RMagick: Common Tasks</title>
<meta http-equiv="Content-Type" content=
"text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="Quanta Plus">
<link type="text/css" rel="stylesheet" href="rubymgen.css">
<div class="header">RMagick User's Guide and Reference</div>
<table align="right" class="nav" cellpadding="3">
<td style="text-align: left"><a href="imusage.html">&lt;-
<td style="text-align: center"><a href=
<td style="text-align: right"><a href="magick.html">Next
<div class="stdtitle">
Common Tasks
<table width="100%" cellspacing="0" cellpadding="5">
<tr bgcolor="#C3C3C3">
<h3>Table Of Contents</h3>
<tr class="toc">
<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) size</a></li>
<li><a href="#gray">Converting a color image to
<li><a href="#shadow">Making a drop shadow</a></li>
<h3>Getting information about an image</h3>
<p><a name="info">One</a> 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>
<table border="0" width="100%">
<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}"|
<h3>Converting an image to another format</h3>
<p><a name="convert">Converting</a> 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>
<h3>Making thumbnails</h3>
<a name="thumb">RMagick</a> gives you four different methods
for resizing an image: <a href=
"image.html#Image.resize"><var>resize</var></a>, <a href=
"image.html#Image.sample"><var>sample</var></a>, <a href=
"image.html#Image.scale"><var>scale</var></a>, and <a href=
"image.html#Image.thumbnail"><var>thumbnail</var></a>. All four are
equally easy to use. Specify the number of columns and rows you
want the thumbnail to have, like this:
<table border="0" width="100%">
<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 <var>resize</var> 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
<var>blur</var> argument, which specifies how much blurriness
or sharpness the resize method should introduce.</p>
<p>The <var>sample</var> method, unlike the other two, does not
do any color interpolation when resizing.</p>
<p>The <var>thumbnail</var> method is faster than
<var>resize</var> if the thumbnail is less than 10% of the size
of the original image.</p>
<h3><a name="resizing">Resizing</a> to a maximum (or minimum) size
<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="image.html#Image.change_geometry">
<var>change_geometry</var></a> method can help.
<p>The <var>change_geometry</var> 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 <var>change_geometry</var>
method parses the geometry string and computes new width and
height values. Then it calls the block, passing the values it
<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. Return the resized image.
The return value from <var>change_geometry</var> is the return
value from the block.
<h3>Converting a color image to grayscale</h3>
<a name="gray">Use</a> the <a href=
"image.html#Image.quantize"><var>quantize</var></a> method with the <a
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="demo.rb.html"
<h3>Making a drop shadow</h3>
<p><a name="shadow">Here's</a> one way to make a drop shadow
behind text. Make the shadow first by drawing the text in
a light gray color. Use the <a href=
method to blur the text, then position the
shadow slightly to the right and down from where the real
text will be. Then draw the text again in whatever color you want.
<i>(Click the image to see the Ruby program that created it.)
<div align="center">
<a href="drop_shadow.rb.html" target="_blank"><img src=
"ex/drop_shadow.gif" alt="drop shadow example"></a>
<table align="right" class="nav" cellpadding="3">
<td style="text-align: left"><a href="imusage.html">&lt;-
<td style="text-align: center"><a href=
<td style="text-align: right"><a href="magick.html">Next
Something went wrong with that request. Please try again.