File: demo.rb

package info (click to toggle)
ruby-rmagick 2.13.2-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,620 kB
  • ctags: 1,785
  • sloc: ansic: 16,759; ruby: 9,717; makefile: 14; sh: 14
file content (324 lines) | stat: -rw-r--r-- 9,216 bytes parent folder | download
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
#!/usr/bin/env ruby
#
# Simple demo program for RMagick
#
# Concept and algorithms lifted from Magick++ demo script written
# by Bob Friesenhahn.
#
require 'RMagick'
include Magick

#
#   RMagick version of Magick++/demo/demo.cpp
#

Font = "Helvetica"

begin
    puts "Read images..."

    model = ImageList.new(File.join(File.dirname(__FILE__), "../doc/ex/images/model.miff"))
    model.border_color = "black"
    model.background_color = "black"
    model.cur_image[:Label] = "RMagick"

    smile = ImageList.new(File.join(File.dirname(__FILE__), "../doc/ex/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 = ImageList.new
    5.times { example.read("NULL:black") { self.size = "70x70"} }

    puts "   add noise..."
    example << model.add_noise(LaplacianNoise)
    example.cur_image[:Label] = "Add Noise"

    puts "   annotate..."
    example << model.cur_image.copy
    example.cur_image[:Label] = "Annotate"
    draw = Draw.new
    draw.annotate(example, 0, 0, 0, 20, "RMagick") {
        self.pointsize = 18;
        self.font = Font;
        self.stroke = "gold"
        self.fill = "gold"
        self.gravity = NorthGravity
        }

    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(RedChannel)
    example.cur_image[:Label] = "Channel"

    puts "   charcoal..."
    example << model.charcoal
    example.cur_image[:Label] = "Charcoal"

    puts "   composite..."
    example << model.composite(smile, 35, 65, 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 = 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") {
        self.size = Geometry.new(model.columns, model.rows)
        }
    example.cur_image[:Label] = "Gradient"

    puts "   grayscale..."
    example << model.cur_image.quantize(256, 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, 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") {
        self.size = Geometry.new(model.columns, model.rows)
        }
    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.matte = true
    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 {
        self.geometry = "130x194+10+5>"
        self.gravity = CenterGravity
        self.border_width = 1
        rows = (example.size + 4) / 5
        self.tile = Geometry.new(5,rows)
        self.compose = OverCompositeOp

        # Use the ImageMagick built-in "granite" format
        # as the background texture.

#       self.texture = Image.read("granite:").first
        self.background_color = "white"
        self.font = Font;
        self.pointsize = 18;
        self.fill = "#600"
        self.filename = "RMagick Demo"
#       self.shadow = true
#       self.frame = "20x20+4+4"
    }

    # 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 = Image.read("logo:").first
    if /GraphicsMagick/.match Magick_version then
        logo.resize!(200.0/logo.rows)
    else
        logo.crop!(98, 0, 461, 455).resize!(0.45)
    end

    # Create a new Image for the composited montage and logo
    montage_image = ImageList.new
    montage_image << montage.composite(logo, 245, 0, OverCompositeOp)

    # Write the result to a file
    montage_image.compression = RLECompression
    montage_image.matte = false
    puts "Writing image ./rm_demo_out.miff"
    montage_image.write "rm_demo_out.miff"

    # Uncomment the following lines to display image to screen
    # puts "Displaying image..."
    # montage_image.display

rescue
    puts "Caught exception: #{$!}"
end

exit