-
Notifications
You must be signed in to change notification settings - Fork 2
/
demo.rb.html
343 lines (277 loc) · 9.79 KB
/
demo.rb.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<!DOCTYPE public PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta name="generator" content="ex2html.rb" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/popup.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<script>
hljs.configure({ cssSelector: "pre" });
hljs.highlightAll();
</script>
<title>RMagick example: demo.rb</title>
</head>
<body>
<h1>demo.rb</h1>
<div class="bodybox">
<div class="bodyfloat">
<pre class="language-ruby">
# frozen_string_literal: true
#
# Simple demo program for RMagick
#
# Concept and algorithms lifted from Magick++ demo script written
# by Bob Friesenhahn.
#
require 'rmagick'
#
# RMagick version of Magick++/demo/demo.cpp
#
FONT = 'Helvetica'
begin
puts 'Read images...'
model = Magick::ImageList.new('images/model.miff')
model.border_color = 'black'
model.background_color = 'black'
model.cur_image[:Label] = 'RMagick'
smile = Magick::ImageList.new('images/smile.miff')
smile.border_color = 'black'
smile.cur_image[:Label] = 'Smile'
#
# Create image stack
#
puts 'Creating thumbnails'
# Construct an initial list containing five copies of a null
# image. This will give us room to fit the logo at the top.
# Notice I specify the width and height of the images via the
# optional "size" attribute in the parm block associated with
# the read method. There are two more examples of this, below.
example = Magick::ImageList.new
5.times { example.read('NULL:black') { |options| options.size = '70x70' } }
puts ' add noise...'
example << model.add_noise(Magick::LaplacianNoise)
example.cur_image[:Label] = 'Add Noise'
puts ' annotate...'
example << model.cur_image.copy
example.cur_image[:Label] = 'Annotate'
draw = Magick::Draw.new
draw.annotate(example, 0, 0, 0, 20, 'RMagick') do |options|
options.pointsize = 18
options.font = FONT
options.stroke = 'gold'
options.fill = 'gold'
options.gravity = Magick::NorthGravity
end
puts ' blur...'
example << model.blur_image(0.0, 1.5)
example.cur_image[:Label] = 'Blur'
puts ' border...'
example << model.border(6, 6, 'gold')
example.cur_image[:Label] = 'Border'
puts ' channel...'
example << model.channel(Magick::RedChannel)
example.cur_image[:Label] = 'Channel'
puts ' charcoal...'
example << model.charcoal
example.cur_image[:Label] = 'Charcoal'
puts ' composite...'
example << model.composite(smile, 35, 65, Magick::OverCompositeOp)
example.cur_image[:Label] = 'Composite'
puts ' contrast...'
example << model.contrast(false)
example.cur_image[:Label] = 'Contrast'
puts ' convolve...'
kernel = [1, 1, 1, 1, 4, 1, 1, 1, 1]
example << model.convolve(3, kernel)
example.cur_image[:Label] = 'Convolve'
puts ' crop...'
example << model.crop(25, 50, 80, 80)
example.cur_image[:Label] = 'Crop'
puts ' despeckle...'
example << model.despeckle
example.cur_image[:Label] = 'Despeckle'
puts ' draw...'
example << model.cur_image.copy
example.cur_image[:Label] = 'Draw'
gc = Magick::Draw.new
gc.fill 'black'
gc.fill_opacity 0
gc.stroke 'gold'
gc.stroke_width 2
gc.circle 60, 90, 60, 120
gc.draw(example)
puts ' edge...'
example << model.edge(0)
example.cur_image[:Label] = 'Detect Edges'
puts ' emboss...'
example << model.emboss
example.cur_image[:Label] = 'Emboss'
puts ' equalize...'
example << model.equalize
example.cur_image[:Label] = 'Equalize'
puts ' explode...'
example << model.implode(-1)
example.background_color = '#000000ff'
example.cur_image[:Label] = 'Explode'
puts ' flip...'
example << model.flip
example.cur_image[:Label] = 'Flip'
puts ' flop...'
example << model.flop
example.cur_image[:Label] = 'Flop'
puts ' frame...'
example << model.frame
example.cur_image[:Label] = 'Frame'
puts ' gamma...'
example << model.gamma_correct(1.6)
example.cur_image[:Label] = 'Gamma'
puts ' gaussian blur...'
example << model.gaussian_blur(1, 1.5)
example.cur_image[:Label] = 'Gaussian Blur'
# To add an Image in one of ImageMagick's built-in formats,
# call the read method. The filename specifies the format and
# any parameters it needs. The gradient format can be created in
# any size. Specify the desired size by assigning it, in the form
# "WxH", to the optional "size" attribute in the block associated
# with the read method. Here we create a gradient image that is
# the same size as the model image.
puts ' gradient...'
example.read('gradient:#20a0ff-#ffff00') do |options|
options.size = Magick::Geometry.new(model.columns, model.rows)
end
example.cur_image[:Label] = 'Gradient'
puts ' grayscale...'
example << model.cur_image.quantize(256, Magick::GRAYColorspace)
example.cur_image[:Label] = 'Grayscale'
puts ' implode...'
example << model.implode(0.5)
example.cur_image[:Label] = 'Implode'
puts ' median filter...'
example << model.median_filter(0)
example.cur_image[:Label] = 'Median Filter'
puts ' modulate...'
example << model.modulate(1.10, 1.10, 1.10)
example.cur_image[:Label] = 'Modulate'
puts ' monochrome...'
example << model.cur_image.quantize(2, Magick::GRAYColorspace, false)
example.cur_image[:Label] = 'Monochrome'
puts ' negate...'
example << model.negate
example.cur_image[:Label] = 'Negate'
puts ' normalize...'
example << model.normalize
example.cur_image[:Label] = 'Normalize'
puts ' oil paint...'
example << model.oil_paint(3.0)
example.cur_image[:Label] = 'Oil Paint'
# The plasma format is very similar to the gradient format, above.
puts ' plasma...'
example.read('plasma:fractal') do |options|
options.size = Magick::Geometry.new(model.columns, model.rows)
end
example.cur_image[:Label] = 'Plasma'
puts ' quantize...'
example << model.cur_image.quantize
example.cur_image[:Label] = 'Quantize'
puts ' raise...'
example << model.raise
example.cur_image[:Label] = 'Raise'
puts ' reduce noise...'
example << model.reduce_noise(3.0)
example.cur_image[:Label] = 'Reduce Noise'
puts ' resize...'
example << model.resize(0.50)
example.cur_image[:Label] = 'Resize'
puts ' roll...'
example << model.roll(20, 10)
example.cur_image[:Label] = 'Roll'
puts ' rotate...'
example << model.rotate(45).transparent('black')
example.cur_image[:Label] = 'Rotate'
puts ' scale...'
example << model.scale(0.60)
example.cur_image[:Label] = 'Scale'
puts ' segment...'
example << model.segment
example.cur_image[:Label] = 'Segment'
puts ' shade...'
example << model.shade(false, 30, 30)
example.cur_image[:Label] = 'Shade'
puts ' sharpen...'
example << model.sharpen(0.0, 1.0)
example.cur_image[:Label] = 'Sharpen'
puts ' shave...'
example << model.shave(10, 10)
example.cur_image[:Label] = 'Shave'
puts ' shear...'
example << model.shear(45, 45).transparent('black')
example.cur_image[:Label] = 'Shear'
puts ' spread...'
example << model.spread(3)
example.cur_image[:Label] = 'Spread'
puts ' solarize...'
example << model.solarize(50.0)
example.cur_image[:Label] = 'Solarize'
puts ' swirl...'
temp = model.copy
temp.background_color = '#000000ff'
example << temp.swirl(90)
example.cur_image[:Label] = 'Swirl'
puts ' unsharp mask...'
example << model.unsharp_mask(0.0, 1.0, 1.0, 0.05)
example.cur_image[:Label] = 'Unsharp Mask'
puts ' wave...'
temp = model.copy
temp.cur_image[:Label] = 'Wave'
temp.background_color = '#000000ff'
example << temp.wave(25, 150)
#
# Create image montage - notice the optional
# montage parameters are supplied via a block
#
puts 'Montage images...'
montage = example.montage do |options|
options.geometry = '130x194+10+5>'
options.gravity = Magick::CenterGravity
options.border_width = 1
rows = (example.size + 4) / 5
options.tile = Magick::Geometry.new(5, rows)
options.compose = Magick::OverCompositeOp
# Use the ImageMagick built-in "granite" format
# as the background texture.
# options.texture = Image.read("granite:").first
options.background_color = 'white'
options.font = FONT
options.pointsize = 18
options.fill = '#600'
options.filename = 'RMagick Demo'
# options.shadow = true
# options.frame = "20x20+4+4"
end
# Add the ImageMagick logo to the top of the montage. The "logo:"
# format is a fixed-size image, so I don't need to specify a size.
puts 'Adding logo image...'
logo = Magick::Image.read('logo:').first
logo.crop!(98, 0, 461, 455).resize!(0.45)
# Create a new Image for the composited montage and logo
montage_image = Magick::ImageList.new
montage_image << montage.composite(logo, 245, 0, Magick::OverCompositeOp)
# Write the result to a file
montage_image.compression = Magick::RLECompression
montage_image.alpha(Magick::DeactivateAlphaChannel)
puts 'Writing image ./rm_demo_out.png'
montage_image.write 'rm_demo_out.png'
# Uncomment the following lines to display image to screen
# puts "Displaying image..."
# montage_image.display
rescue StandardError
puts "Caught exception: #{$ERROR_INFO}"
end
exit
</pre>
</div>
</div>
<div id="close"><a href="javascript:window.close();">Close window</a></div>
</body>
</html>