Skip to content

Instantly share code, notes, and snippets.

@mikesorae
Last active December 23, 2015 21:00
Show Gist options
  • Save mikesorae/6693834 to your computer and use it in GitHub Desktop.
Save mikesorae/6693834 to your computer and use it in GitHub Desktop.
# -*- encoding: utf-8 -*-
# -sで指定したディレクトリにある画像をリサイズして-dで指定したディレクトリに出力するよ。
# 今のとこサイズは決め打ちだけどいじりたかったらopt.onに追加してね。
require 'pathname'
require 'fileutils'
require 'RMagick'
require 'optparse'
PHOTO_WIDTH = 640
PHOTO_HEIGHT = 960
required = [:src, :dst]
$args = {}
OptionParser.new do |opt|
opt.on("-s SOURCE_PATH") do |v| $args[:src] = v end
opt.on("-d DEST_PATH") do |v| $args[:dst] = v end
opt.parse ARGV
required.each do |key|
raise "#{key} is empty" if $args[key] == nil
end
end
puts "src = #{$args[:src]}"
puts "dst = #{$args[:dst]}"
def resize_photos_in_dir(dir)
Dir.open(dir).sort.each do |file|
next if /^[.]{1,2}$|[.]DS_Store$/ =~ file
file_path = File.join(dir, file)
if File.directory? file_path
resize_photos_in_dir file_path
else
resize_photo file_path
end
end
end
def resize_photo path
tmp_array = path.split('/')
tmp_array.delete_at 0
tmp_array.insert 0, $args[:dst]
dest_path = tmp_array.join '/'
pathname = Pathname.new dest_path
if not File.exist? pathname.parent.to_s
FileUtils.mkdir_p pathname.parent.to_s
end
puts "#{path} -> #{dest_path}"
img = Magick::Image.read(path).first
sml = img.resize_to_fit PHOTO_WIDTH, PHOTO_HEIGHT
sml.write dest_path
end
# do crop
resize_photos_in_dir $args[:src]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment