Skip to content

Instantly share code, notes, and snippets.

@gangmax
Created May 23, 2012 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gangmax/2773708 to your computer and use it in GitHub Desktop.
Save gangmax/2773708 to your computer and use it in GitHub Desktop.
The is an example to use ruby control command line tool(ImageMagick) to deal with image processing.
# encoding: UTF-8
#
# This program is used to convert all the png files under the given path,
# from colorful to grey, from big to small(70% as original), from png to
# gif, and then keep the file name unchanged(still *.png).
#
# Running this program needs you have "imagemagick" installed.
#
require 'find'
unless ARGV.length == 1
puts "Usage: image_converter.rb /home/user/images"
exit
end
Find.find(ARGV[0]) do |f|
if f.end_with?('.png')
bw_name = f + '.bw.png'
bw_gif_name = bw_name + '.gif'
`convert -colorspace GRAY '#{f}' '#{bw_name}'`
`rm '#{f}'`
`convert '#{bw_name}' -resize 70% '#{bw_gif_name}'`
`rm '#{bw_name}'`
`mv '#{bw_gif_name}' '#{f}'`
puts "convert '#{f}' ok."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment