Skip to content

Commit

Permalink
Abstracted out image annotation so multiple captions can be supported
Browse files Browse the repository at this point in the history
  • Loading branch information
emaillenin committed Aug 5, 2014
1 parent 02be2e9 commit b790deb
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 58 deletions.
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
source 'https://rubygems.org'

gem 'rmagick'

gem 'psych'
gem 'psych', '2.0.0'
12 changes: 12 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
GEM
remote: https://rubygems.org/
specs:
psych (2.0.0)
rmagick (2.13.2)

PLATFORMS
ruby

DEPENDENCIES
psych
rmagick
58 changes: 17 additions & 41 deletions index.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'RMagick'
include Magick

require_relative 'src/template/image_mojo_template'
Dir[File.dirname(__FILE__) + '/src/**/*.rb'].each { |file| require file }

template = ImageMojoTemplate.new(ARGV[0])

Expand Down Expand Up @@ -39,52 +39,28 @@
self.align = LeftAlign
end

# unless template.logo.nil?
# watermark_text = Magick::Draw.new
# watermark_text.annotate(imgl, start_x, start_y, end_x - 5, end_y - 10, template.logo['text']) do
# self.gravity = Magick::EastGravity
# self.pointsize = 15
# self.font_family = template.logo['font']
# self.font_weight = Magick::BoldWeight
# self.stroke = 'transparent'
# self.fill = 'white'
# self.kerning = 1
# self.align = LeftAlign
# end
# end

unless template.caption.nil?

if template.caption['background']
background_caption = Magick::Draw.new
background_caption.opacity(template.caption['background_opacity'])
background_caption.fill(template.caption['background_color'])
background_caption.rectangle(0, template.caption['background_start_y'], src.columns, template.caption['background_start_y'] + template.caption['background_height'])
background_caption.draw(imgl)
imgl.alpha(Magick::ActivateAlphaChannel)

end

caption_text = Magick::Draw.new
caption_text.annotate(imgl, src.columns, src.rows, src.columns/2 + template.caption['offset_x'], src.rows/2 + template.caption['offset_y'], template.caption['text']) do
self.gravity = Magick::CenterGravity
self.pointsize = template.caption['size']
self.font_family = template.caption['font']
unless template.hashtag.nil?
watermark_text = Magick::Draw.new
watermark_text.annotate(imgl, 0, 0, 10, 25, "##{template.hashtag['text']}") do
self.gravity = Magick::EastGravity
self.pointsize = 18
self.font_family = template.hashtag['font']
self.font_weight = Magick::BoldWeight
self.stroke = 'transparent'
self.fill = template.caption['color']
self.stroke = template.caption['stroke']
self.stroke_width = 1
self.font_weight = Magick::BoldWeight
# self.rotation = 270
self.fill = 'white'
self.kerning = 1
self.interline_spacing = 7
self.align = CenterAlign
self.align = LeftAlign
end
end

output = "#{File.dirname(template.image['path'])}/mojo_#{File.basename(template.image['path'])}"
puts "Writing to #{output}"
imgl.write(output)

template.all_templates.keys.select { |key| key.to_s.start_with?('caption') }.each do |caption|
a = ImageAnnotate.new(src, imgl, template.all_templates[caption])
a.apply_template
puts "Writing to #{output}"
imgl.write(output)
end


exit
37 changes: 37 additions & 0 deletions src/image_annotate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class ImageAnnotate
def initialize(source_image,image_list, caption)
@caption = caption
@image_list = image_list
@source_image = source_image
end

def apply_template
caption = @caption
if caption['background']
background_caption = Magick::Draw.new
background_caption.opacity(caption['background_opacity'])
background_caption.fill(caption['background_color'])
background_caption.rectangle(0, caption['background_start_y'], @source_image.columns, caption['background_start_y'] + caption['background_height'])
background_caption.draw(@image_list)
@image_list.alpha(Magick::ActivateAlphaChannel)
end

caption_text = Magick::Draw.new
caption_text.annotate(@image_list, @source_image.columns, @source_image.rows, @source_image.columns/2 + caption['offset_x'], @source_image.rows/2 + caption['offset_y'], caption['text']) do
self.gravity = Magick::CenterGravity
self.pointsize = caption['size']
self.font_family = caption['font']
self.font_weight = Magick::BoldWeight
self.stroke = 'transparent'
self.fill = caption['color']
self.stroke = caption['stroke']
self.stroke_width = 1
self.font_weight = Magick::BoldWeight
# self.rotation = 270
self.kerning = 1
self.interline_spacing = 7
self.align = CenterAlign
end
@image_list
end
end
12 changes: 7 additions & 5 deletions src/template/image_mojo_template.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
require 'psych'
class ImageMojoTemplate

attr_accessor :copyright, :caption, :image
attr_accessor :all_templates, :copyright, :caption, :image, :hashtag
def initialize(config_file)
template = Psych.load(File.read(config_file))
@copyright = template['copyright']
@caption = template['caption']
@image = template['image']
puts "Using template file #{config_file}"
@all_templates = Psych.load(File.read(config_file))
@copyright = @all_templates['copyright']
@caption = @all_templates['caption']
@image = @all_templates['image']
@hashtag = @all_templates['hashtag']
end
end
27 changes: 16 additions & 11 deletions template.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

image:
path: /Users/leninraj/Downloads/shakib.jpg
path: /Users/leninraj/Downloads/samson.jpg

copyright:
text: WWW.DUGGOUT.COM
Expand All @@ -9,23 +10,27 @@ copyright:
opacity: 0.6
font: Roboto

logo:
text: DuggOut.com
hashtag:
text: EngVsInd
x_offset: 25
y_offset: 25
font: Roboto

caption:
text: BANNED FOR SIX MONTHS
caption_player:
text: INDIA TEAM SQUAD \nANNOUNCED
blur: 0.4
font: Roboto
size: 60
color: white
stroke: white
offset_x: 0
offset_y: 200
background: true
background_color_1: rgba(12,133,199,0.3)
background_color: rgba(150,93,14,0.6)
offset_y: -415
background: false
# background_color: rgba(150,93,14,0.6) # brown
# background_color_white: rgba(225,225,243,0.4)
# background_color: rgba(10,10,10,0.6) # black
background_color: rgba(23,168,230,0.7) # blue
# background_color_pink: rgba(150,14,93,0.5)
background_opacity: 0.2
background_height: 100
background_start_y: 450
background_height: 120
background_start_y: 415
70 changes: 70 additions & 0 deletions template_multiple_captions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
image:
path: /Users/leninraj/Downloads/virat.jpg

copyright:
text: WWW.DUGGOUT.COM
height: 100
width: 25
x_offset: 25
opacity: 0.6
font: Roboto

hashtag:
text: ENGvsIND
x_offset: 25
y_offset: 25
font: Roboto

caption_player:
text: VIRAT KOHLI
font: Roboto
size: 80
color: white
stroke: white
offset_x: 0
offset_y: 422
background: true
background_color_brown: rgba(150,93,14,0.4)
background_color_white: rgba(225,225,243,0.4)
background_color_black: rgba(10,10,10,0.4)
background_color: rgba(23,168,230,0.7) # blue
background_color_pink: rgba(150,14,93,0.5)
background_opacity: 0.2
background_height: 100
background_start_y: 990

caption_1st_innings:
text: 1st MATCH - 1 AND 8
font: Roboto
size: 50
color: white
stroke: white
offset_x: 0
offset_y: 512
background: true
background_color: rgba(150,93,14,0.6) # brown
background_color_white: rgba(225,225,243,0.4)
background_color_black: rgba(10,10,10,0.4)
background_color_blue: rgba(23,168,230,0.7) # blue
background_color_pink: rgba(150,14,93,0.5)
background_opacity: 0.2
background_height: 100
background_start_y: 1090

caption_2nd_innings:
text: 2nd MATCH - 25 AND 0
font: Roboto
size: 50
color: white
stroke: white
offset_x: 0
offset_y: 612
background: true
background_color: rgba(150,93,14,0.6)
background_color_white: rgba(225,225,243,0.4)
background_color_black: rgba(10,10,10,0.4)
background_color_blue: rgba(23,168,230,0.7) # blue
background_color_pink: rgba(150,14,93,0.5)
background_opacity: 0.2
background_height: 100
background_start_y: 1190

0 comments on commit b790deb

Please sign in to comment.