Skip to content

Instantly share code, notes, and snippets.

@kfriend
Created August 31, 2013 04:14
Show Gist options
  • Save kfriend/6396190 to your computer and use it in GitHub Desktop.
Save kfriend/6396190 to your computer and use it in GitHub Desktop.
Ruby: Create image thumbnails using OS X sips
thumb_width = 117
thumb_height = 141
raise ArgumentError, 'No images given. You must supply at least one image to process' if ARGV.empty?
ARGV.each do |image|
if File.file?(image)
width = `sips -g pixelWidth "#{image}"`.match(/pixelWidth: \d+$/im).to_s.match(/\d+/).to_s.to_i
height = `sips -g pixelHeight "#{image}"`.match(/pixelHeight: \d+$/im).to_s.match(/\d+/).to_s.to_i
if width > height
target = height
else
target = width
end
`sips -c #{target} #{target} "#{image}"`
`sips -z #{thumb_height} #{thumb_width} "#{image}"`
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment