Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 288 lines (238 sloc) 10.892 kB
b39952f Change "aClass" convention to just "class" in method signatures. Nume…
rmagick authored
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4e8cecb Add summary attribute to tables and reformat using latest version of …
rmagick authored
3
636db86 Upgrade to XHTML 1.0 Transitional.
rmagick authored
4 <html xmlns="http://www.w3.org/1999/xhtml">
4e8cecb Add summary attribute to tables and reformat using latest version of …
rmagick authored
5 <head>
6 <meta name="generator" content=
f197d2e Minor edits
rmagick authored
7 "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org" />
4e8cecb Add summary attribute to tables and reformat using latest version of …
rmagick authored
8
df65846 Replace version number placeholder "MAJOR.MINOR.TEENY" with "0.0.0"
rmagick authored
9 <title>RMagick 0.0.0: Common Tasks</title>
4e8cecb Add summary attribute to tables and reformat using latest version of …
rmagick authored
10 <meta http-equiv="Content-Type" content=
636db86 Upgrade to XHTML 1.0 Transitional.
rmagick authored
11 "text/html; charset=us-ascii" />
12 <meta name="GENERATOR" content="Quanta Plus" />
501d99c Add new topic "Specifying the compression level"
rmagick authored
13 <meta name="Copyright" content=
3829d21 Change copyright date to 2006
rmagick authored
14 "Copyright (C) 2006 by Timothy P. Hunter" />
07ed4d9 Major revision: convert to XHTML 1.0 and CSS2
rmagick authored
15 <link rel="stylesheet" type="text/css" href="css/doc.css" />
ff86f33 Add "out-of-memory" advice
rmagick authored
16 <script type="text/javascript" src="scripts/doc.js">
17 </script>
18 <style type="text/css">
07ed4d9 Major revision: convert to XHTML 1.0 and CSS2
rmagick authored
19 /*<![CDATA[*/
20
21 /* Styles local to this page. */
22 #drop_shadow {
23 margin-left: auto;
24 margin-right: auto;
25 width: 250px;
26 }
27
28
29 /*]]>*/
ff86f33 Add "out-of-memory" advice
rmagick authored
30 </style>
4e8cecb Add summary attribute to tables and reformat using latest version of …
rmagick authored
31 </head>
32
33 <body>
df65846 Replace version number placeholder "MAJOR.MINOR.TEENY" with "0.0.0"
rmagick authored
34 <h6 id="header">RMagick 0.0.0 User's Guide and Reference</h6>
07ed4d9 Major revision: convert to XHTML 1.0 and CSS2
rmagick authored
35
36 <div class="nav">
c039841 Insert optequiv.html page
rmagick authored
37 &laquo;&nbsp;<a href="optequiv.html">Prev</a> | <a href=
07ed4d9 Major revision: convert to XHTML 1.0 and CSS2
rmagick authored
38 "index.html">Contents</a> | <a href=
39 "magick.html">Next</a>&nbsp;&raquo;
4e8cecb Add summary attribute to tables and reformat using latest version of …
rmagick authored
40 </div>
41
07ed4d9 Major revision: convert to XHTML 1.0 and CSS2
rmagick authored
42 <h1>Common Tasks</h1>
43
44 <div id="toc">
45 <h2>Table of Contents</h2>
46
3ba337f Added entry for to_blob and from_blob
rmagick authored
47 <ul style="margin-left: 15px; padding-top: 1em;">
07ed4d9 Major revision: convert to XHTML 1.0 and CSS2
rmagick authored
48 <li><a href="#info">Getting information about an
49 image</a></li>
50
51 <li><a href="#convert">Converting an image to another
52 format</a></li>
4e8cecb Add summary attribute to tables and reformat using latest version of …
rmagick authored
53
07ed4d9 Major revision: convert to XHTML 1.0 and CSS2
rmagick authored
54 <li><a href="#thumb">Making thumbnails</a></li>
4e8cecb Add summary attribute to tables and reformat using latest version of …
rmagick authored
55
07ed4d9 Major revision: convert to XHTML 1.0 and CSS2
rmagick authored
56 <li><a href="#resizing">Resizing to a maximum (or minimum)
57 size</a></li>
4e8cecb Add summary attribute to tables and reformat using latest version of …
rmagick authored
58
3ba337f Added entry for to_blob and from_blob
rmagick authored
59 <li><a href="#blob">Writing to or reading from a string
60 instead of a file</a></li>
61
07ed4d9 Major revision: convert to XHTML 1.0 and CSS2
rmagick authored
62 <li><a href="#gray">Converting a color image to
63 grayscale</a></li>
64
a2879a1 Improve the documentation of the compression and quality attributes.
rmagick authored
65 <li><a href="#compressing">Compressing image files</a></li>
501d99c Add new topic "Specifying the compression level"
rmagick authored
66
07ed4d9 Major revision: convert to XHTML 1.0 and CSS2
rmagick authored
67 <li><a href="#shadow">Making a drop shadow</a></li>
68 </ul>
4e8cecb Add summary attribute to tables and reformat using latest version of …
rmagick authored
69 </div>
70
07ed4d9 Major revision: convert to XHTML 1.0 and CSS2
rmagick authored
71 <h2 id="info">Getting information about an image</h2>
72
73 <p>One of the most fundamental operations on an image is simply
74 getting basic information about the image. RMagick assigns dozens
75 of <a href="imageattrs.html">attributes</a> to an image. All you
76 have to do is read the image and then call the attribute methods.
77 Here's a Ruby program that takes image filenames from the command
78 line and then prints a variety of information about each image to
79 the terminal.</p>
80 <pre class="example">
81 require 'RMagick'
82 ARGV.each { |file|
83 puts file
84 img = Magick::Image::read(file).first
85 puts " Format: #{img.format}"
86 puts " Geometry: #{img.columns}x#{img.rows}"
87 puts " Class: " + case img.class_type
88 when Magick::DirectClass
89 "DirectClass"
90 when Magick::PseudoClass
91 "PseudoClass"
92 end
93 puts " Depth: #{img.depth} bits-per-pixel"
94 puts " Colors: #{img.number_colors}"
95 puts " Filesize: #{img.filesize}"
96 puts " Resolution: #{img.x_resolution.to_i}x#{img.y_resolution.to_i} "+
97 "pixels/#{img.units == Magick::PixelsPerInchResolution ?
98 "inch" : "centimeter"}"
99 if img.properties.length &gt; 0
100 puts " Properties:"
101 img.properties { |name,value|
102 puts %Q| #{name} = "#{value}"|
d9b079a Initial revision
tim authored
103 }
07ed4d9 Major revision: convert to XHTML 1.0 and CSS2
rmagick authored
104 end
105 }
d9b079a Initial revision
tim authored
106 </pre>
07ed4d9 Major revision: convert to XHTML 1.0 and CSS2
rmagick authored
107
108 <h2 id="convert">Converting an image to another format</h2>
109
110 <p>Converting an image to another format is as simple as writing
f405c78 Replace remaining uses of &times;Magick with plain ImageMagick.
rmagick authored
111 the image to a file. ImageMagick uses the output filename
07ed4d9 Major revision: convert to XHTML 1.0 and CSS2
rmagick authored
112 suffix (".jpg" for JPEG, ".gif" for GIF, for example) or prefix
113 ("ps:" for PostScript, for example) to determine the format of
114 the output image.</p>
115
116 <h2 id="thumb">Making thumbnails</h2>
117
118 <p>RMagick gives you four different methods for resizing an
119 image: <a href="image3.html#resize"><code>resize</code></a>,
120 <a href="image3.html#sample"><code>sample</code></a>, <a href=
121 "image3.html#scale"><code>scale</code></a>, and <a href=
122 "image3.html#thumbnail"><code>thumbnail</code></a>. All four are
123 equally easy to use. Specify the number of columns and rows you
124 want the thumbnail to have, like this:</p>
125 <pre class="example">
126 img = Image.new "bigimage.gif"
127 thumb = img.scale(125, 125)
128 thumb.write "thumb.gif"
d9b079a Initial revision
tim authored
129 </pre>
4e8cecb Add summary attribute to tables and reformat using latest version of …
rmagick authored
130
131 <p>Alternatively, just pass a single <code>Float</code> argument
132 that represents the change in size. For example, to
133 proportionally reduce the size of an image to 25% of its original
134 size, do this:</p>
135 <pre class="example">
07ed4d9 Major revision: convert to XHTML 1.0 and CSS2
rmagick authored
136 img = Image.new "bigimage.gif"
137 thumb = img.scale(0.25)
138 thumb.write "thumb.gif"
d9b079a Initial revision
tim authored
139 </pre>
4e8cecb Add summary attribute to tables and reformat using latest version of …
rmagick authored
140
07ed4d9 Major revision: convert to XHTML 1.0 and CSS2
rmagick authored
141 <p>The <code>resize</code> method gives you more control by
4e8cecb Add summary attribute to tables and reformat using latest version of …
rmagick authored
142 allowing you to specify a <a href=
143 "constants.html#FilterType">filter</a> to use when scaling the
144 image. Some filters produce a better-looking thumbnail at the
145 expense of extra processing time. You can also use a
07ed4d9 Major revision: convert to XHTML 1.0 and CSS2
rmagick authored
146 <code>blur</code> argument, which specifies how much blurriness
147 or sharpness the resize method should introduce.</p>
4e8cecb Add summary attribute to tables and reformat using latest version of …
rmagick authored
148
07ed4d9 Major revision: convert to XHTML 1.0 and CSS2
rmagick authored
149 <p>The <code>sample</code> method, unlike the other two, does not
4e8cecb Add summary attribute to tables and reformat using latest version of …
rmagick authored
150 do any color interpolation when resizing.</p>
151
07ed4d9 Major revision: convert to XHTML 1.0 and CSS2
rmagick authored
152 <p>The <code>thumbnail</code> method is faster than
153 <code>resize</code> if the thumbnail is less than 10% of the size
4e8cecb Add summary attribute to tables and reformat using latest version of …
rmagick authored
154 of the original image.</p>
155
f197d2e Minor edits
rmagick authored
156 <h3>flickr-style thumbnails</h3>
157
caaa744 Fix broken links
rmagick authored
158 <p><a href="http://www.flickr.com">flickr</a> thumbnails are 75
159 pixels wide and 75 pixels tall. If the original image isn't
160 square, the thumbnail is cropped in its larger dimension so that
161 the image isn't distorted. You can get make this kind of
162 thumbnail with the <a href=
163 "image3.html#resize_to_fill">resize_to_fill</a> method.</p>
f197d2e Minor edits
rmagick authored
164 <pre class="example">
165 thumb = img.resize_to_fill(75, 75)
166 </pre>
167
07ed4d9 Major revision: convert to XHTML 1.0 and CSS2
rmagick authored
168 <h2 id="resizing">Resizing to a maximum (or minimum) size</h2>
4e8cecb Add summary attribute to tables and reformat using latest version of …
rmagick authored
169
170 <p>Say you need to make all your thumbnails no bigger than 64x64
171 but with the same aspect ratio as the original. Or, you don't
172 want to resize the image if it's already smaller than 64x64. The
173 <a href=
07ed4d9 Major revision: convert to XHTML 1.0 and CSS2
rmagick authored
174 "image1.html#change_geometry"><code>change_geometry</code></a>
4e8cecb Add summary attribute to tables and reformat using latest version of …
rmagick authored
175 method can help.</p>
176
07ed4d9 Major revision: convert to XHTML 1.0 and CSS2
rmagick authored
177 <p>The <code>change_geometry</code> method accepts an
f405c78 Replace remaining uses of &times;Magick with plain ImageMagick.
rmagick authored
178 ImageMagick <a href="imusage.html#geometry">geometry string</a>
07ed4d9 Major revision: convert to XHTML 1.0 and CSS2
rmagick authored
179 argument and a block. The geometry string specifies how to change
180 the image's size: one or two numbers to specify the new size and
181 optional flags to describe any constraints. The
182 <code>change_geometry</code> method parses the geometry string
183 and computes new width and height values. Then it calls the
184 block, passing the values it computed.</p>
4e8cecb Add summary attribute to tables and reformat using latest version of …
rmagick authored
185
186 <p>Within the block you can do whatever you want with the new
187 values. Typically you'll call one of the resize methods mentioned
48c9103 Minor fixes.
rmagick authored
188 in the previous section and make the resized image the return
07ed4d9 Major revision: convert to XHTML 1.0 and CSS2
rmagick authored
189 value from the block. The <code>change_geometry</code> method
190 then returns that value to its caller.</p>
4e8cecb Add summary attribute to tables and reformat using latest version of …
rmagick authored
191
b39952f Change "aClass" convention to just "class" in method signatures. Nume…
rmagick authored
192 <h3>Simple thumbnails</h3>
193
194 <p>If you just want to make sure your thumbnail is no bigger than
195 a certain width and height, use the <a href=
196 "image3.html#resize_to_fit">resize_to_fit</a> method.</p>
f197d2e Minor edits
rmagick authored
197 <pre class="example">
198 thumb = img.resize_to_fit(75, 75)
199 </pre>
200
3ba337f Added entry for to_blob and from_blob
rmagick authored
201 <h2><a id="blob" name="blob">Writing to or reading from a string
202 instead of a file</a></h2>
203
204 <p>Use the <a href="image1.html#from_blob">Image.from_blob</a>
205 method to construct an Image object from a string. Use the
206 <a href="image3.html#to_blob">Image#to_blob</a> method to convert
207 an image to a string. A blob is simply an in-memory version of an
208 image file. That is, you could use <code>File.read</code> to read
209 an JPEG file into a string, then create an image by using that
210 string as an argument to <code>from_blob</code>. Similarly, if
211 you create a string version of an image with
212 <code>to_blob</code>, then write the string to a file, any image
213 viewer will be able to display it just as if you had written the
214 image directly to a file. Blobs are very useful in web
215 applications when you want to modify an image and then stream it
216 back to the client.</p>
217
01de9cc Document Image#export_pixels_to_str
rmagick authored
218 <p>Use <a href=
219 "image2.html#import_pixels">Image#import_pixels</a> to load pixel
220 data from a string buffer into an image. The pixel data must be
221 in scanline order, right-to-left and top-to-bottom. The data can
222 be packed as 8-bit bytes, 16-bit halfwords, 32-bit fullwords, or
223 as C floats or doubles. The reciprocal method is <a href=
224 "image3.html#export_pixels_to_str">Image#export_pixels_to_str</a>.</p>
225
07ed4d9 Major revision: convert to XHTML 1.0 and CSS2
rmagick authored
226 <h2 id="gray">Converting a color image to grayscale</h2>
227
228 <p>Use the <a href=
501d99c Add new topic "Specifying the compression level"
rmagick authored
229 "image3.html#quantize"><code>quantize</code></a> method with the
230 <a href=
4e8cecb Add summary attribute to tables and reformat using latest version of …
rmagick authored
231 "constants.html#ColorspaceType">Magick::GRAYColorspace</a>
232 argument. If you want real "grayscale," quantize the image to 256
233 colors. If you want to convert a color image to black-and-white,
75bc108 Remove links to the demo.rb example
rmagick authored
234 use 2 colors. (See the <code>demo.rb</code> example.)</p>
501d99c Add new topic "Specifying the compression level"
rmagick authored
235
a2879a1 Improve the documentation of the compression and quality attributes.
rmagick authored
236 <h2 id="compressing">Compressing image files</h2>
237
238 <p>Many image formats, including JPEG, PDF, and BMP, support
239 compressed image files. The type of compression used depends on
240 the format. Specify the compression type by assigning a <a href=
b1605ce Clarify meaning of `quality' attribute
rmagick authored
241 "constants.html#CompressionType">CompressionType</a> value to the
242 <a href="info.html#compression">compression</a> optional argument
a2879a1 Improve the documentation of the compression and quality attributes.
rmagick authored
243 to the <a href="image3.html#write">write</a> method.</p>
244
245 <p>The JPEGCompression and ZipCompression types support multiple
b1605ce Clarify meaning of `quality' attribute
rmagick authored
246 levels of compression. Use the <a href=
247 "info.html#quality">quality</a> optional argument to the
a2879a1 Improve the documentation of the compression and quality attributes.
rmagick authored
248 <code>write</code> method. The quality attribute is a number
b1605ce Clarify meaning of `quality' attribute
rmagick authored
249 between 0 and 100, with 100 representing the least compression.
250 When you compress an image using JPEGCompression, more
251 compression usually results in a lower-quality image. When you
252 compress an image using ZipCompression, more compression usually
253 takes longer.</p>
501d99c Add new topic "Specifying the compression level"
rmagick authored
254
f405c78 Replace remaining uses of &times;Magick with plain ImageMagick.
rmagick authored
255 <p>For more information, see the ImageMagick documentation for
501d99c Add new topic "Specifying the compression level"
rmagick authored
256 the <code>-quality</code> option to the utility commands.</p>
257 <pre class="example">
258 img.write("myimage.jpg") { self.quality = 50 }
259 </pre>
4e8cecb Add summary attribute to tables and reformat using latest version of …
rmagick authored
260
07ed4d9 Major revision: convert to XHTML 1.0 and CSS2
rmagick authored
261 <h2 id="shadow">Making a drop shadow</h2>
4e8cecb Add summary attribute to tables and reformat using latest version of …
rmagick authored
262
07ed4d9 Major revision: convert to XHTML 1.0 and CSS2
rmagick authored
263 <p>Here's one way to make a drop shadow behind text. Make the
264 shadow first by drawing the text in a light gray color. Position
265 the text slightly to the right and down from where the real text
266 will be. Then use the <a href=
267 "image1.html#blur_image"><code>blur_image</code></a> method to
48c9103 Minor fixes.
rmagick authored
268 make the shadow by blurring the text. Finally, draw the text
07ed4d9 Major revision: convert to XHTML 1.0 and CSS2
rmagick authored
269 again in whatever color you want. <em>(Click the image to see the
270 Ruby program that created it.)</em></p>
4e8cecb Add summary attribute to tables and reformat using latest version of …
rmagick authored
271
07ed4d9 Major revision: convert to XHTML 1.0 and CSS2
rmagick authored
272 <div id="drop_shadow">
273 <a href="javascript:popup('drop_shadow.rb.html')"><img src=
274 "ex/drop_shadow.gif" title="Click to see the example script"
275 alt="drop shadow example" /></a>
4e8cecb Add summary attribute to tables and reformat using latest version of …
rmagick authored
276 </div>
636db86 Upgrade to XHTML 1.0 Transitional.
rmagick authored
277 <hr />
4e8cecb Add summary attribute to tables and reformat using latest version of …
rmagick authored
278
f197d2e Minor edits
rmagick authored
279 <p class="spacer"></p>
4e8cecb Add summary attribute to tables and reformat using latest version of …
rmagick authored
280
07ed4d9 Major revision: convert to XHTML 1.0 and CSS2
rmagick authored
281 <div class="nav">
c039841 Insert optequiv.html page
rmagick authored
282 &laquo; <a href="optequiv.html">Prev</a> | <a href=
07ed4d9 Major revision: convert to XHTML 1.0 and CSS2
rmagick authored
283 "index.html">Contents</a> | <a href="magick.html">Next</a>
284 &raquo;
285 </div>
4e8cecb Add summary attribute to tables and reformat using latest version of …
rmagick authored
286 </body>
d9b079a Initial revision
tim authored
287 </html>
Something went wrong with that request. Please try again.