Skip to content

Instantly share code, notes, and snippets.

@hellekin
Created January 22, 2009 00:12
Show Gist options
  • Save hellekin/50351 to your computer and use it in GitHub Desktop.
Save hellekin/50351 to your computer and use it in GitHub Desktop.
Write "hello, world" on a PNG
#!/usr/bin/env ruby
require 'rubygems'
require 'mini_magick'
BLANK = 'blank.png' # 1x1px white png
$text = 'hello, world' # text to write
img = MiniMagick::Image.from_file(BLANK)
font_size = 12 # points
margin = 6 # 3px on each side
h = font_size + margin
w = 7 * $text.length # 7px per letter
img.combine_options do |c|
c.resize "#{w}X#{h}!"
c.gravity 'Center'
c.font '-*-arial-*-r-*-*-' + font_size.to_s + '-*-*-*-*-*-*-1'
c.pointsize font_size.to_s
c.draw "text 0,0 '#{$text}'"
c.fill '#000000'
end
img.write("out.png")
puts `identify out.png`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment