2010-01-15
added -d and -c options
| rmoar.rb | file | annotate | diff | revisions |
1.1 --- a/rmoar.rb Fri Jan 15 17:57:04 2010 +0100 1.2 +++ b/rmoar.rb Fri Jan 15 18:23:12 2010 +0100 1.3 @@ -42,29 +42,24 @@ 1.4 TITLE_PSIZE = 69 1.5 MOTIVATOR_PSIZE = 42 / 2 1.6 1.7 - 1.8 # user provided options 1.9 -Params = Struct.new(:src, :title, :motivator, :output).new 1.10 +Params = Struct.new(:src, :title, :motivator, :output, :width, :height, :crop).new 1.11 1.12 # parse ARGV 1.13 opts = OptionParser.new do |o| 1.14 o.banner = "usage: #{__FILE__} [options] image" 1.15 - 1.16 - o.on('-h', '--help', 'show this help') do 1.17 - puts o 1.18 - exit 1.19 - end 1.20 - 1.21 - o.on('-t', '--title TITLE', String, 'set the title') do |t| 1.22 - Params.title = t 1.23 - end 1.24 - 1.25 - o.on('-m', '--motivator SENTENCE', String, 'set the motivator') do |m| 1.26 - Params.motivator = m 1.27 - end 1.28 - 1.29 - o.on('-o', '--output FILENAME', String, 'output file') do |f| 1.30 - Params.output = f 1.31 + o.on('-h', '--help', 'show this help') { puts o; exit } 1.32 + o.on('-t', '--title TITLE', String, 'set the title') { |t| Params.title = t } 1.33 + o.on('-m', '--motivator SENTENCE', String, 'set the motivator') { |m| Params.motivator = m } 1.34 + o.on('-o', '--output FILENAME', String, 'output file') { |f| Params.output = f } 1.35 + o.on('-c', '--crop', 'crop src image while resize') { Params.crop = true } 1.36 + o.on('-d', '--dimensions WxH', String, 'resize src (ex: 800x600)') do |d| 1.37 + w, h = d.split('x').map { |e| e.to_i } 1.38 + if w == 0 or h == 0 1.39 + raise ArgumentError.new("bad dimensions argument: #{d}") 1.40 + end 1.41 + Params.width = w 1.42 + Params.height = h 1.43 end 1.44 end 1.45 opts.parse! 1.46 @@ -77,18 +72,25 @@ 1.47 end 1.48 1.49 # load src 1.50 -isrc = Magick::Image.read(Params.src).first 1.51 -isrc.border!(BORDER_WIDTH, BORDER_WIDTH, 'black') 1.52 -isrc.border!(BORDER_WIDTH, BORDER_WIDTH, 'white') 1.53 +img = Magick::Image.read(Params.src).first 1.54 +if Params.width and Params.height 1.55 + if Params.crop 1.56 + img.resize_to_fill!(Params.width, Params.height) 1.57 + else 1.58 + img.resize_to_fit!(Params.width, Params.height) 1.59 + end 1.60 +end 1.61 +img.border!(BORDER_WIDTH, BORDER_WIDTH, 'black') 1.62 +img.border!(BORDER_WIDTH, BORDER_WIDTH, 'white') 1.63 1.64 -THUMB_WIDTH = isrc.columns 1.65 -THUMB_HEIGHT = isrc.rows 1.66 +THUMB_WIDTH = img.columns 1.67 +THUMB_HEIGHT = img.rows 1.68 IMG_WITDH = THUMB_WIDTH + 100 1.69 IMG_HEIGHT = THUMB_HEIGHT + 200 1.70 TITLE_YOFFSET = THUMB_HEIGHT / 2 - 20 1.71 1.72 # create black caneva 1.73 -img = Magick::Image.new(IMG_WITDH, IMG_HEIGHT) do 1.74 +background = Magick::Image.new(IMG_WITDH, IMG_HEIGHT) do 1.75 self.background_color = 'black' 1.76 end 1.77 1.78 @@ -101,7 +103,7 @@ 1.79 1.80 # add title 1.81 if Params.title 1.82 - text.annotate(img, 0, 0, 0, TITLE_YOFFSET, Params.title) do 1.83 + text.annotate(background, 0, 0, 0, TITLE_YOFFSET, Params.title) do 1.84 self.fill = 'white' 1.85 end 1.86 end 1.87 @@ -110,12 +112,12 @@ 1.88 text.pointsize = MOTIVATOR_PSIZE 1.89 text.font_family = 'roman' 1.90 if Params.motivator 1.91 - text.annotate(img, 0, 0, 0, TITLE_YOFFSET + 42, Params.motivator) do 1.92 + text.annotate(background, 0, 0, 0, TITLE_YOFFSET + 42, Params.motivator) do 1.93 self.fill = 'white' 1.94 end 1.95 end 1.96 1.97 -moar = img.composite(isrc, Magick::CenterGravity, 0, -60, Magick::OverCompositeOp) 1.98 +moar = background.composite(img, Magick::CenterGravity, 0, -60, Magick::OverCompositeOp) 1.99 1.100 # show or save the result 1.101 if Params.output