Skip to content

Instantly share code, notes, and snippets.

@chenzx
Created January 7, 2013 09:36
Show Gist options
  • Save chenzx/4473712 to your computer and use it in GitHub Desktop.
Save chenzx/4473712 to your computer and use it in GitHub Desktop.
Use ImageMagicK's convert.exe to batch convert image files
def do_batch_convert(from_reldir, to_reldir)
Dir.new(from_reldir).entries.each { |subpath|
from_path = File.join(from_reldir, subpath)
to_path = File.join(to_reldir, subpath)
if File.file?(from_path) and /\.(jpg|png)$/.match(from_path.downcase) then
if not File.exist?(to_reldir) then
Dir.mkdir(to_reldir)
end
puts "convert from #{from_path} to #{to_path}"
system(".\\convert.exe #{from_path} -resize 25% #{to_path}")
#不是Windows不识别"a/b.jpg"这种相对路径,而是它把convert.exe解释为系统自带的那个版本了(而不是当前目录下的)
#使用 .\convert.exe 即可(但Python反而不识别.\的形式?)噢,必须写成.\\才行
#`.\\convert.exe #{from_path} -resize 25% #{to_path}`
end
}
end
do_batch_convert(ARGV[0], ARGV[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment