Skip to content

Instantly share code, notes, and snippets.

@marcinbunsch
Created May 17, 2011 10:02
Show Gist options
  • Save marcinbunsch/976231 to your computer and use it in GitHub Desktop.
Save marcinbunsch/976231 to your computer and use it in GitHub Desktop.
Meme generator using local images
#!/usr/bin/env ruby
#
# OMG THIS CODE IS SO UGLY
# but...
# IT WORKS!!
#
# Hacked together by http://github.com/marcinbunsch
#
# This assumes the following:
# - That you're on a Mac
# - That you have ImageMagick installed
# - That you have the impact font in /Library/Fonts/Impact.ttf
# - That you have a ~/Drawer folder with source jpeg files
# - That you have a Dropbox account
#
# Example:
# If I have a yuno.jpg file in ~/Drawer and run:
# meme "Y U NO" "HAVE YOUR OWN MEME GENERATOR" --source yuno --name yuno.generator --dropbox
# It will create a file yuno.generator.jpg based on yuno.jpg in ~/Dropbox/Public/memes and copy the link to clipboard
#
require 'optparse'
# Font
FONT = '/Library/Fonts/Impact.ttf'
# Directory which holds the source files for memes
DRAWER = "#{ENV['HOME']}/Drawer"
# Dropbox that will be used for hosting memes
DROPBOX = "#{ENV['HOME']}/Dropbox/Public/memes"
system "mkdir -p #{DROPBOX}"
# Put your dropbox id here
DROPBOX_ID = "YOUR_DROPBOX_ID"
DROPBOX_URL = "http://dl.dropbox.com/u/#{DROPBOX_ID}"
options = {
:output => 'output.jpg',
:source => nil,
:font_top => 48,
:font_bottom => 48,
:top_y => 10,
:bottom_y => 10
}
OptionParser.new do |opts|
opts.banner = "Usage: meme STRING STRING [options]"
opts.on("-n name", "--name name", "Name of output file") do |v|
options[:output] = "#{v}.jpg"
end
opts.on("-s s", "--source s", "Source meme") do |v|
options[:source] = Dir.glob(File.join(DRAWER, v + "*")).first
end
opts.on("-o", "--open", "Open after generation") do
options[:open] = true
end
opts.on("-d", "--dropbox", "Copy to dropbox after generation") do
options[:dropbox] = true
end
opts.on("-t size", "--top size", "Font size of top text") do |size|
options[:font_top] = size
end
opts.on("-b size", "--bottom size", "Font size of bottom text") do |size|
options[:font_bottom] = size
end
opts.on("--top-y pos", "Y position of top text") do |y|
options[:top_y] = y
end
opts.on("--bottom-y pos", "Y position of bottom text") do |y|
options[:bottom_y] = y
end
end.parse!
if !options[:source]
puts "Source not found!"
exit
end
kern = 1
texts = []
texts.push ARGV[0].upcase.gsub('\N', '\n') if ARGV[0]
texts.push ARGV[1].upcase.gsub('\N', '\n') if ARGV[1]
commands = ["convert #{options[:source]} -font #{FONT} -fill white"]
if texts.size == 1
commands.push "-pointsize #{options[:font_bottom]}"
commands.push "-stroke black -strokewidth 4 -gravity North -kerning #{kern} -annotate +0+#{options[:bottom_y]} \"#{texts.first}\" "
commands.push "-stroke none -strokewidth 4 -gravity North -kerning #{kern} -annotate +0+#{options[:bottom_y]} \"#{texts.first}\" "
elsif texts.size == 2
commands.push "-pointsize #{options[:font_top]}"
commands.push "-stroke black -strokewidth 4 -gravity North -kerning #{kern} -annotate +0+#{options[:top_y]} \"#{texts.first}\" "
commands.push "-stroke none -strokewidth 4 -gravity North -kerning #{kern} -annotate +0+#{options[:top_y]} \"#{texts.first}\" "
commands.push "-pointsize #{options[:font_bottom]}"
commands.push "-stroke black -strokewidth 4 -gravity South -kerning #{kern} -annotate +0+#{options[:bottom_y]} \"#{texts[1]}\" "
commands.push "-stroke none -strokewidth 4 -gravity South -kerning #{kern} -annotate +0+#{options[:bottom_y]} \"#{texts[1]}\" "
else
exit 1
end
commands.push options[:output]
system commands.join(" \\\n")
puts "Generated into #{options[:output]}"
if options[:open]
system "open #{options[:output]}"
end
if options[:dropbox]
basename = File.basename(options[:output])
system "cp #{options[:output]} #{File.join(DROPBOX, basename)}"
path = DROPBOX.split("/Dropbox/Public/").last
url = "#{DROPBOX_URL}/#{path}/#{basename}"
system "echo #{url} | pbcopy"
puts "Dropbox url copied to clipboard!"
system "rm #{options[:output]}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment