Skip to content

Instantly share code, notes, and snippets.

@kamui
Created March 22, 2011 16:42
Show Gist options
  • Save kamui/881538 to your computer and use it in GitHub Desktop.
Save kamui/881538 to your computer and use it in GitHub Desktop.
Annotate text that auto fits within an image and benchmark on both graphicsmagick and imagemagick. Uses mini_magick
#!/usr/bin/env ruby
# Convert command line
#
#gm convert -font helvetica -fill white -pointsize 20 -draw "text 500,600 'WALLPAPERZ ARE GOOD'" test.jpg output.jpg
#
#convert -background transparent -fill white -gravity center -size 1024x200 -font Helvetica -pointsize 20 #caption:"I'm a wallpaper, you know what to do" test.jpg +swap -gravity south -composite output.jpg
#
# Mogrify command line
#
#mogrify -font helvetica -fill white -pointsize 20 -gravity center -annotate +0+300 'WOW123' -format png test.jpg
#
#gm mogrify -font helvetica -fill white -pointsize 20 -gravity center -draw "text 0,300 'WOW123'" -format png test.jpg
require 'rubygems'
require 'mini_magick'
require 'benchmark'
#MiniMagick.processor = :gm
def create_image
#caption = "I LOVE WALLPAPERZ!!!!!1"
caption = "I HATE IMAGEMAGICK! I HATE IMAGEMAGICK!"
image = MiniMagick::Image.open("test.jpg")
image.combine_options do |c|
c.font "helvetica"
c.fill "white"
c.pointsize 20
c.gravity "center"
c.draw "text 0,300 '#{caption}'"
end
image.write "output.jpg"
end
Benchmark.bm do |r|
iterations = 1000
r.report("imagemagick:") {(1..iterations).each { create_image }}
MiniMagick.processor = :gm
r.report("graphicsmagick:") {(1..iterations).each { create_image }}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment