diff --git a/Gemfile b/Gemfile index c2eee42..e5d0c95 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,5 @@ +source 'https://rubygems.org' + gem 'rmagick' -gem 'psych' \ No newline at end of file +gem 'psych', '2.0.0' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..46fd3ea --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,12 @@ +GEM + remote: https://rubygems.org/ + specs: + psych (2.0.0) + rmagick (2.13.2) + +PLATFORMS + ruby + +DEPENDENCIES + psych + rmagick diff --git a/index.rb b/index.rb index 458860c..8c0526a 100644 --- a/index.rb +++ b/index.rb @@ -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]) @@ -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 \ No newline at end of file diff --git a/src/image_annotate.rb b/src/image_annotate.rb new file mode 100644 index 0000000..b0ae511 --- /dev/null +++ b/src/image_annotate.rb @@ -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 \ No newline at end of file diff --git a/src/template/image_mojo_template.rb b/src/template/image_mojo_template.rb index 689b3b3..4562943 100644 --- a/src/template/image_mojo_template.rb +++ b/src/template/image_mojo_template.rb @@ -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 \ No newline at end of file diff --git a/template.yml b/template.yml index 68f3025..5c683f2 100644 --- a/template.yml +++ b/template.yml @@ -1,5 +1,6 @@ + image: - path: /Users/leninraj/Downloads/shakib.jpg + path: /Users/leninraj/Downloads/samson.jpg copyright: text: WWW.DUGGOUT.COM @@ -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 \ No newline at end of file + background_height: 120 + background_start_y: 415 diff --git a/template_multiple_captions.yml b/template_multiple_captions.yml new file mode 100644 index 0000000..c33bfdd --- /dev/null +++ b/template_multiple_captions.yml @@ -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 \ No newline at end of file