/
comtasks.html
195 lines (195 loc) · 7.96 KB
/
comtasks.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta name="generator" content=
"HTML Tidy for Linux/x86 (vers 1st September 2002), see www.w3.org">
<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">
</head>
<body>
<div class="header">RMagick User's Guide and Reference</div>
<table align="right" class="nav" cellpadding="3">
<tr>
<td style="text-align: left"><a href="imusage.html"><-
Prev</a></td>
<td style="text-align: center"><a href=
"index.html">Contents</a></td>
<td style="text-align: right"><a href="magick.html">Next
-></a></td>
</tr>
</table>
<div class="stdtitle">
Common Tasks
</div>
<table width="100%" cellspacing="0" cellpadding="5">
<tr bgcolor="#C3C3C3">
<td>
<h3>Table Of Contents</h3>
</td>
</tr>
<tr class="toc">
<td>
<ul>
<li><a href="#info">Getting information about an
image</a></li>
<li><a href="#convert">Converting an image to another
format</a></li>
<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
grayscale</a></li>
<li><a href="#shadow">Making a drop shadow</a></li>
</ul>
</td>
</tr>
</table>
<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%">
<tr>
<td>
<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
"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
}
</pre>
</td>
</tr>
</table>
<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. ×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%">
<tr>
<td>
<pre class="example">
img = Image.new "bigimage.gif"
thumb = img.scale(125, 125)
thumb.write "thumb.gif"
</pre>
</td>
</tr>
</table>
<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 = Image.new "bigimage.gif"
thumb = img.scale(0.25)
thumb.write "thumb.gif"
</pre>
<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
</h3>
<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>
<p>The <var>change_geometry</var> method accepts an ×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
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. Return the resized image.
The return value from <var>change_geometry</var> is the return
value from the block.
</p>
<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
href="constants.html#ColorspaceType">Magick::GRAYColorspace</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"
target="_blank">demo.rb</a>.
<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=
"image.html#Image.gaussian_blur"><var>gaussian_blur</var></a>
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.)
</i></p>
<div align="center">
<a href="drop_shadow.rb.html" target="_blank"><img src=
"ex/drop_shadow.gif" alt="drop shadow example"></a>
</div>
<hr>
<table align="right" class="nav" cellpadding="3">
<tr>
<td style="text-align: left"><a href="imusage.html"><-
Prev</a></td>
<td style="text-align: center"><a href=
"index.html">Contents</a></td>
<td style="text-align: right"><a href="magick.html">Next
-></a></td>
</tr>
</table>
</body>
</html>