From 0aba81d9c841504fbcc05ca677490153383a6985 Mon Sep 17 00:00:00 2001 From: Lenin Raj Rajasekaran Date: Sun, 8 Jun 2014 12:30:52 +0530 Subject: [PATCH] Refactoring & Usage of templates --- .gitignore | 1 + Gemfile | 3 ++ index.rb | 64 +++++++++++++++++++++++++++++ src/template/image_mojo_template.rb | 11 +++++ template.yml | 19 +++++++++ 5 files changed, 98 insertions(+) create mode 100644 Gemfile create mode 100644 index.rb create mode 100644 src/template/image_mojo_template.rb create mode 100644 template.yml diff --git a/.gitignore b/.gitignore index f2c1360..04b17f4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ *.gem *.rbc +.idea /.config /coverage/ /InstalledFiles diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..c2eee42 --- /dev/null +++ b/Gemfile @@ -0,0 +1,3 @@ +gem 'rmagick' + +gem 'psych' \ No newline at end of file diff --git a/index.rb b/index.rb new file mode 100644 index 0000000..f2b48e5 --- /dev/null +++ b/index.rb @@ -0,0 +1,64 @@ +require 'RMagick' +include Magick + +require_relative 'src/template/image_mojo_template' + +template = ImageMojoTemplate.new(ARGV[0]) + +imgl = Magick::ImageList.new + +src = Magick::Image.read(template.image['path']).first + +puts "#{src.columns}x#{src.rows}" + +start_x = template.copyright['x_offset'] +start_y = (src.rows/2) - template.copyright['height'] +end_x = start_x + template.copyright['width'] +end_y = (src.rows/2) + template.copyright['height'] + +imgl.from_blob(File.open(template.image['path']).read) + +gc = Magick::Draw.new + +gc.fill_opacity(template.copyright['opacity']) + +gc.rectangle(start_x, start_y, end_x, end_y) + +gc.font(template.copyright['opacity']) + +gc.draw(imgl) + +imgl.alpha(Magick::ActivateAlphaChannel) + +watermark_text = Magick::Draw.new +watermark_text.annotate(imgl, start_x, start_y, end_x - 5, end_y - 10, template.copyright['text']) do + self.gravity = Magick::EastGravity + self.pointsize = 15 + self.font_family = 'Arial' + self.font_weight = Magick::BoldWeight + self.stroke = 'transparent' + self.fill = 'white' + self.rotation = 270 + self.kerning = 1 + self.align = LeftAlign +end + +caption_text = Magick::Draw.new +caption_text.annotate(imgl, src.columns, src.rows, src.columns/2, src.rows/2, template.caption['text']) do + self.gravity = Magick::CenterGravity + self.pointsize = template.caption['size'] + self.font_family = template.caption['font'] + self.font_weight = Magick::BoldWeight + self.stroke = 'transparent' + self.fill = template.caption['color'] + self.stroke = template.caption['stroke'] + self.stroke_width = 1 + # self.rotation = 270 + self.kerning = 1 + self.interline_spacing = 2.5 + self.align = CenterAlign +end + +imgl.write("mojo_#{File.basename(template.image['path'])}") + +exit \ No newline at end of file diff --git a/src/template/image_mojo_template.rb b/src/template/image_mojo_template.rb new file mode 100644 index 0000000..689b3b3 --- /dev/null +++ b/src/template/image_mojo_template.rb @@ -0,0 +1,11 @@ +require 'psych' +class ImageMojoTemplate + + attr_accessor :copyright, :caption, :image + def initialize(config_file) + template = Psych.load(File.read(config_file)) + @copyright = template['copyright'] + @caption = template['caption'] + @image = template['image'] + end +end \ No newline at end of file diff --git a/template.yml b/template.yml new file mode 100644 index 0000000..b39e95e --- /dev/null +++ b/template.yml @@ -0,0 +1,19 @@ +image: + path: /Users/leninraj/Downloads/161895.3.jpg + +copyright: + text: WWW.DUGGOUT.COM + height: 100 + width: 25 + x_offset: 50 + opacity: 0.6 + font: Roboto + +caption: + text: Chris Gayle is playing his \n100th test match today! + font: Roboto + size: 45 + color: white + stroke: Transparent + offset_x: 0 + offset_y: 100 \ No newline at end of file