Archive for November 2007
Creating a transparent GIF in RMagick
RMagick is the Ruby interface to ImageMagick.
Suppose you’d like to create an image drawn in black, on a white transparent background:
require 'RMagick'
include Magick
img = Image.new(200, 100)
img.transparent_color = 'white'
# draw stuff ...
img.transparent('white').write('bw.gif')
RMagick is a tad underdocumented. The transparent_color bit is missing for example. By default ‘black’ is the transparent color. The img.transparent(‘white’) call doesn’t change that. It merely replaces all white pixels with transparent ones. The result, without setting transparent_color to white, is an all black image in bw.gif. Also, take note that RMagick transformations leave the original intact and return a new image with the transform applied.