From: ara.t.howard@... Date: 2006-04-07T02:46:03+09:00 Subject: Re: Creating images from pixel data (RMagick?) On Thu, 6 Apr 2006, Tim Blair wrote: > require 'RMagick' > class RippedImage > def initialize (job_id, width, height) > @job_id = job_id.to_s; > @width = width.to_i; > @height = height.to_i; > @canvas = Magick::Image.new(@width, @height) > @pixels = [] > end > def draw (c) > # the colour is a 6 digit hex char which needs to > # be converted to numeric values for RGB > col = [] > c.scan(/../).each { |bit| col << bit.hex } > @pixels << Magick::Pixel.new(col[0], col[1], col[2], 0) > end > def finalize (type="gif") > @canvas.store_pixels(0, 0, @width, @height, @pixels) > @canvas.write("#{@job_id}.#{type}") > end > end > > x = 512 > y = 384 > colours = ["ff0000", "00ff00", "0000ff"] > img = RippedImage.new(1, x, y) > (x*y).times do > img.draw(colours[rand(colours.length)]) > end > img.finalize are you really limited to using string pixel values? if not i would say you should be able to generate these images in __way__ under 1s. even if you are, using import_pixels will be tons faster: your original: jib:~/eg/ruby > time ruby a.rb real 0m4.028s user 0m3.620s sys 0m0.070s using import_pixels: jib:~/eg/ruby > time ruby a.rb real 0m0.829s user 0m0.810s sys 0m0.000s jib:~/eg/ruby > cat a.rb require 'rubygems' require 'RMagick' class RippedImage LITTLE_ENDIAN = [42].pack('i').unpack('c').first.nonzero? ? true : false BIG_ENDIAN = ! LITTLE_ENDIAN MAP = BIG_ENDIAN ? 'ARGB' : 'BGRA' def initialize width, height @width = Integer width @height = Integer height @pixels = [] end def << c @pixels << c.hex self end def draw path img = Magick::Image.new @width, @height img.import_pixels 0, 0, @width, @height, MAP, @pixels.pack('i*'), Magick::CharPixel img.opacity = 255 # because the pack leaves it 0 - totally transparent img.write path end end x = 512 y = 384 colours = ["ff0000", "00ff00", "0000ff"] img = RippedImage.new x, y (x*y).times{ img << (colours[rand(colours.length)]) } img.draw "#{ 1 }.png" so that's a four times speed-up right there. i suspect 1s is a reasonable number too. kind regards. -a -- be kind whenever possible... it is always possible. - h.h. the 14th dali lama