Sha256: 257af4185b1e12e17e1ee68850ee4a060d7873665c2774087632c198ed7ee29f

Contents?: true

Size: 1.6 KB

Versions: 67

Compression:

Stored size: 1.6 KB

Contents

#! /usr/local/bin/ruby -w
require 'RMagick'

buttons = Magick::ImageList.new

# Read 25 alphabet image files, scale to 1/4 size
letter = 'A'
26.times {
    if letter != 'M'            # "M" is not the same size as the other letters
        tiny = Magick::Image.read('images/Button_' + letter + '.gif').first
        tiny.scale! 0.25
        buttons << tiny
    end
    letter.succ!
    }

# Create a image that will hold the alphabet images in 5 rows and 5 columns.
cells = Magick::ImageList.new
cells.new_image buttons.columns*5, buttons.rows*5 do
    self.background_color = "#000000ff"     # transparent
    end
cells.matte = true

offset = Magick::Rectangle.new(0,0,0,0)

# Create 2 arrays from which we can randomly choose row,col pairs
row = [0]*5 + [1]*5 + [2]*5 + [3]*5 + [4]*5
col = (0..4).to_a * 5

# The coalesce method composites the 2nd image over the 1st, the 3rd image
# over the result, and so forth, respecting the page offset of the images.

srand 1234
25.times { |i|
    # Randomly select a row and column for this copy of the "tinya" image.
    # Compute the x,y position of this copy in pixels and store the
    # result in the image's page attribute. Append a copy of the image
    # to the image array in "a".
    n = rand row.length
    x = row.delete_at n
    y = col.delete_at n

    button = buttons[i]
    offset.x = x*button.columns
    offset.y = y*button.rows
    button.page = offset
    button.matte = true
    cells << button
    }

puts "This may take a few seconds..."
cells.delay = 10
cells.iterations = 10000
res = cells.coalesce
res.write "coalesce_anim.gif"
res[25].write "coalesce.gif"
exit



Version data entries

67 entries across 67 versions & 2 rubygems

Version Path
rmagick-2.13.4 doc/ex/coalesce.rb
rmagick-2.13.3 doc/ex/coalesce.rb
rmagick-2.13.3.rc1 doc/ex/coalesce.rb
rmagick-2.13.2 doc/ex/coalesce.rb
rdp-rmagick-0.0.0 doc/ex/coalesce.rb
rmagick-2.13.1 doc/ex/coalesce.rb
rmagick-2.12.2 doc/ex/coalesce.rb
rmagick-2.12.1 doc/ex/coalesce.rb
rmagick-2.12.0 doc/ex/coalesce.rb
rmagick-2.11.1 doc/ex/coalesce.rb
rmagick-2.10.0 doc/ex/coalesce.rb
rmagick-2.11.0 doc/ex/coalesce.rb
rmagick-1.10.1 doc/ex/coalesce.rb
rmagick-1.10.0 doc/ex/coalesce.rb
rmagick-1.11.1 doc/ex/coalesce.rb
rmagick-1.11.0 doc/ex/coalesce.rb
rmagick-1.13.0 doc/ex/coalesce.rb
rmagick-1.12.0 doc/ex/coalesce.rb
rmagick-1.15.10 doc/ex/coalesce.rb
rmagick-1.15.0 doc/ex/coalesce.rb