The Secret Diary of Han, Aged 0x29

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 =, 100)
img.transparent_color = 'white'

# draw stuff ...


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.


Written by Han

November 27, 2007 at 17:58

Posted in Uncategorized