c4se記:さっちゃんですよ☆

.。oO(さっちゃんですよヾ(〃l _ l)ノ゙☆)

.。oO(此のblogは、主に音樂考察Programming に分類されますよ。ヾ(〃l _ l)ノ゙♬♪♡)

音樂は SoundCloud に公開中です。

考察は現在は主に Scrapbox で公表中です。

Programming は GitHub で開發中です。

ImageMagicで影付き文字を入れるRuby scriptを増強しました

[ImageMagicで影付き文字を入れる。]( http://c4se.hatenablog.com/entry/2012/10/19/220403 )を増強しました。

使い方

Cygwinで動かしています。

$ ruby insertCopyright.rb DIRNAME

code

# vim:fileencoding=utf-8 ff=dos et sw=2 ts=2 sts=2:

$IMAGEMAGIC_DIR = 'C:/Program Files (x86)/ImageMagick-6.7.9-Q16'
$DEFAULT_COPYRIGHT = '(C) Ranyuen'

#
#
class Annotation
  attr_accessor :text, :color, :shadowColor, :fontSize

  #
  #
  # @param [String] text
  # @param [String] color
  # @param [String] shadowColor
  # @param [number] fontSize
  # @return [Annotation]
  def initialize text=$DEFAULT_COPYRIGHT, color='white', shadowColor='black',
                 fontSize=20
    @text = text
    @color = color
    @shadowColor = shadowColor
    @fontSize = fontSize
  end

  #
  #
  # @return [number]
  def width
    @text.length * @fontSize / 2
  end

  #
  #
  # @return [number]
  def height
    @fontSize / 2
  end
end

# Identify an image file by ImageMagic.
#
# @param [String] image_path
# @return [Array] [width px, height px]
def identify image_path
  wxh = %x{"#{$IMAGEMAGIC_DIR}/identify" -format "%wx%h" "#{image_path}"}
  wxh.split(/\n/)[0].split(/x/).map(&:to_i)
end

# Calculate size to fit 1000px of length min of width and height.
#
# @param [number] width
# @param [number] height
# @param [number] minWidth 短辺の最小値
# @return [Array]
def fit_size width, height, minWidth=1000
  min = [width, height].min
  if min > minWidth
    mag_rate = minWidth * 1.0 / min
  else
    mag_rate = 1
  end
  [(width * mag_rate).floor, (height * mag_rate).floor]
end


# Insert copyright and convert an image file by ImageMagic.
#
# @param [String] input_image_path
# @param [String] output_image_path
# @param [Annotation] annotation
def convert input_image_path, output_image_path, annotation
  (width, height) = fit_size *(identify input_image_path)
  left = width - annotation.width - 10
  top = height - annotation.height - 10
  system %Q{"#{$IMAGEMAGIC_DIR}/convert" "#{input_image_path}" } +
         %Q{-geometry #{width}x#{height} } +
         %Q{-pointsize #{annotation.fontSize} } +
         %Q{-fill #{annotation.shadowColor} } +
         %Q{-annotate +#{left+1}+#{top+1} "#{annotation.text}" } +
         %Q{-fill #{annotation.color} } +
         %Q{-annotate +#{left}+#{top} "#{annotation.text}" } +
         %Q{"#{output_image_path}" }
end

# Convert all image files in the directory.
#
# @param [String] input_dir_path
# @param [String] output_dir_path
# @param [Annotation] annotation
def convert_dir input_dir_path, output_dir_path, annotation
  Dir.glob("#{input_dir_path}/*.{jpg,png}") do |filename|
    p filename
    convert filename,
            "#{output_dir_path}/#{File.basename filename, File.extname(filename)}_c.jpg",
            annotation
  end
end

convert_dir ARGV[0], ARGV[0], Annotation.new(ARGV[1] || $DEFAULT_COPYRIGHT)

codeは、大体下から順番に書いています。