require 'rubygems' require 'rmagick' FORMAT = "P\%d.JPG" #2241200 - 2241209, (0, 0, 2736, 3648), 100, 100 #8282885 - 8282890, (0, 800, 2736, 2000), 300, 300 #8293075 - 8293095, (1350, 1600, 800, 600), 10, 500 # question start & end file numbers and return full path file lists def get_files begin puts "Enter the path... (nothing for current)" STDOUT.flush fpath = gets.chomp.strip end until File.exist?(fpath) or fpath.length == 0 fpath += '/' unless fpath.length == 0 puts "Enter start file... (only number portion)" STDOUT.flush sfile = gets.chomp.strip puts "End of file... (only number portion)" STDOUT.flush efile = gets.chomp.strip spos = sfile.to_i epos = efile.to_i fcount = epos - spos + 1 flist = [] spos.step(epos) do |pos| fname = fpath + FORMAT % pos flist << fname if File.exist?(fname) end flist end # make actual animated object and returns it def process(flist) sample = Magick::ImageList.new(flist[0]) rows, cols = sample.rows, sample.columns puts "First picture's size : #{cols} X #{rows}" puts "Enter the cropping area (x y width height)" STDOUT.flush dim = gets.chomp.strip.split.collect { |v| v.to_i } puts "Some vertically oriented pictures may be not recognized correctly, if so rotate now." puts "Enter -1,none,1 for rotation (-90, none, 90 degrees)" STDOUT.flush resp = gets.chomp.strip if resp.length == 0 then rotate = nil else rotate = resp.to_i * 90 sample.rotate!(rotate) rows, cols = cols, rows end puts "Do you want to show the crop area? (y/N)" STDOUT.flush resp = gets.chomp.strip if resp.downcase == 'y' then area = sample.crop(dim[0], dim[1], dim[2], dim[3]) area.display end puts "Enter the GIF's width... (none for same size)" STDOUT.flush resp = gets.chomp.strip if resp.length == 0 then resize = false width, height = dim[2], dim[3] else resize = true width = resp.to_i #height = (width.to_f / cols * rows).to_i height = (dim[3].to_f / dim[2] * width).to_i puts "resizing to #{width} : #{height}" end puts "Any comment in right lower corner ?" STDOUT.flush comment = gets.chomp.strip if comment.length != 0 then d = Magick::Draw.new puts "Enter the font size (default : 12)" STDOUT.flush resp = gets.chomp.strip d.pointsize = resp.to_i unless resp.length == 0 dinfo = d.get_type_metrics(comment) puts "Text width : #{dinfo.width}, please enter the margin (default 10)" STDOUT.flush resp = gets.chomp.strip margin = (resp.length == 0) ? 10 : resp.to_i # d.text_align(Magick::RightAlign) d.text_antialias(true) else d = nil end puts "Now processing..." anim = Magick::ImageList.new 0.step(flist.length-1) do |i| puts "# #{i+1}/#{flist.length}" pfile = Magick::ImageList.new(flist[i]) pfile.rotate!(rotate) unless rotate.nil? # if last true argument were absent, result file would contain original picture informations area = pfile.crop(dim[0], dim[1], dim[2], dim[3], true) area.resize!(width, height) if resize unless d.nil? then d.annotate(area, dinfo.width, dinfo.height, width - dinfo.width - margin, height - margin, comment) end anim << area.copy end puts "Enter the delay between frames in ms unit... (none for 10ms)" STDOUT.flush resp = gets.chomp.strip anim.delay = (resp.length == 0) ? 10 : resp.to_i / 10 puts "Enter the delay after the last frame in ms unit... (none for 10ms)" STDOUT.flush resp = gets.chomp.strip anim.cur_image.delay = (resp.length == 0) ? 10 : resp.to_i / 10 anim end files = get_files result = process(files) puts "Enter the result file name... (without .gif)" resp = gets.chomp.strip STDOUT.flush result.write("#{resp}.GIF") # puts "Total #{files.length} files", files