Skip to content

Instantly share code, notes, and snippets.

@solarsailer
Last active August 29, 2015 14:11
Show Gist options
  • Save solarsailer/a7613791d34f18bac782 to your computer and use it in GitHub Desktop.
Save solarsailer/a7613791d34f18bac782 to your computer and use it in GitHub Desktop.
Convert a list of images (dup and crop a base image and create a thumbnail).
#!/usr/bin/env ruby
# Example (crop at 100x100):
# convert_images.rb -x 100 -y 100
require 'optparse'
# This script needs imagemagick.
if !system("which convert > /dev/null 2>&1")
puts "Abort! Require ImageMagick."
exit
end
# -------------------------------------------------------
# Options.
# -------------------------------------------------------
x = 0
y = 0
w = 1136
h = 640
OptionParser.new do |opts|
opts.banner = "Usage: convert_screenshots [options]"
opts.on("-x", "--left [VALUE]", "X (0 by default)") do |o|
x = o
end
opts.on("-y", "--top [VALUE]", "Y (0 by default)") do |o|
y = o
end
opts.on("-w", "--width [VALUE]", "Width (#{w} by default)") do |o|
w = o
end
opts.on("-h", "--height [VALUE]", "Height (#{h} by default)") do |o|
h = o
end
end.parse!
# -------------------------------------------------------
# Actual code.
# -------------------------------------------------------
files = Dir[Dir.getwd + "/*.png"]
files.each_with_index do |file, i|
puts "Process #{file}…"
`convert -crop #{w}x#{h}+#{x}+#{y} "#{file}" "#{i}.png"`
`convert -resize 400x225! -format jpg "#{i}.png" "preview-#{i}.jpg"`
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment