Created
January 21, 2013 13:29
-
-
Save nkabardin/4586043 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding : utf-8 -*- | |
require 'rmagick' | |
class Caption < Object | |
WIDTH = 1440 | |
HEIGHT = 900 | |
FONT_SIZE = 20 | |
MARGIN_LEFT = 30 | |
MARGIN_BOTTOM = 30 | |
def make_caption(text, infile, outfile) | |
f = File.open infile | |
i = Magick::Image.read f | |
f.close | |
i = i[0] | |
i.resize_to_fill! WIDTH, HEIGHT | |
d = Magick::Draw.new | |
d.font('GillSans.ttc') # put your font near the script | |
d.font_size(FONT_SIZE) | |
d.fill("black") | |
d.text(MARGIN_LEFT+1, HEIGHT-MARGIN_BOTTOM+1, text) | |
d.fill("white") | |
d.text(MARGIN_LEFT, HEIGHT-MARGIN_BOTTOM, text) | |
d.draw i | |
i.write outfile | |
i.destroy! | |
end | |
def caption_dir(indir, outdir) | |
Dir.foreach(indir) do |filename| | |
inpath = indir+"/"+filename | |
if inpath.end_with? "jpg", "jpeg" | |
text = filename.split(".").first | |
text[" - "] = " — " if text.include? " - " | |
outpath = [outdir, filename].join "/" | |
puts "Making caption for " + filename | |
make_caption text, inpath, outpath | |
end | |
end | |
end | |
end | |
c = Caption.new | |
c.caption_dir "/Volumes/HDD/Downloads/Media/Pictures/Sotheby`s", "/Volumes/HDD/Downloads/Media/Pictures/Sothebys_captionated_for_Narkomany" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment