Class: Imagetools::Imageconcat

Inherits:
Object
  • Object
show all
Defined in:
lib/imagetools/imageconcat.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Imageconcat

Returns a new instance of Imageconcat.



74
75
76
# File 'lib/imagetools/imageconcat.rb', line 74

def initialize(opts)
  @opts = opts
end

Class Method Details

.get_image_files(opts, argv) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/imagetools/imageconcat.rb', line 40

def self.get_image_files(opts, argv)
  image_files = []
  # ディレクトリが処理対象かどうかを決める
  dir = nil
  if argv.size == 1
    # 引き数が1個の場合は最初の引き数
    path = File.expand_path(argv[0])
    if FileTest.directory?(path)
      dir = path
    end
  elsif argv.size == 0
    # 引き数がない場合カレントディレクトリ
    dir = File.expand_path('.')
  end
  if dir
    concat_number = opts[:n] || 2
    # ディレクトリが指定されていた場合、指定ディレクトリ内のIMG_ファイルの最新n個を対象とする
    # 最新の基準は(ファイル名基準)
    match_files = Dir.glob("#{dir}/IMG_*.{jpg,jpeg,png}", File::FNM_CASEFOLD).sort
    # 後ろからn個を取得(小さい方の数とする)
    count = [match_files.size, concat_number].min
    image_files = match_files[-count..-1]
  else
    # それ以外は指定された引き数を全て対象とする
    argv.each do |arg|
      arg = File.expand_path(arg)
      if FileTest.file?(arg) && (arg =~ /\.jpe?g$/i || arg =~ /\.png/i)
        image_files << arg
      end
    end
  end
  image_files
end

.run(argv) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/imagetools/imageconcat.rb', line 10

def self.run(argv)
  STDOUT.sync = true
  opts = {}
  opt = OptionParser.new(argv)
  opt.version = VERSION
  opt.banner = "Usage: #{opt.program_name} [-h|--help] <dir> or <image1 image2 image3 ...>"
  opt.separator('')
  opt.separator("Examples:")
  opt.separator("    #{opt.program_name} ~/tmp # concat two recent IMG_*jpg images.")
  opt.separator("    #{opt.program_name} image1.jpg image2.jpg image3.jpg  # concat specified images.")
  opt.separator('')      
  opt.separator("Options:")
  opt.on_head('-h', '--help', 'Show this message') do |v|
    puts opt.help
    exit       
  end
  opt.on('-v', '--verbose', 'Verbose message') {|v| opts[:v] = v}
  opt.on('--dry-run', 'Message only') {|v| opts[:dry_run] = v}
  opt.on('-o OUTNAME', '--output=OUTNAME', 'Output file') {|v| opts[:o] = v}
  opt.on('-n NUM', '--number=NUM', 'Cancat image Number') {|v| opts[:n] = v.to_i}
  opt.parse!(argv)
  image_files = get_image_files(opts, argv)
  if image_files.size < 2
    puts opt.help
    exit        
  end
  command = Imageconcat.new(opts)
  command.run(image_files)
end

Instance Method Details

#run(image_files) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/imagetools/imageconcat.rb', line 78

def run(image_files)
  dirname = File.dirname(image_files[-1])
  output_name = "photo.jpg"
  if @opts[:o]
    output_name = @opts[:o]
  end
  output_path = File.join(dirname, output_name)
  concat_images(image_files, output_path)
end