Skip to content

Instantly share code, notes, and snippets.

@rashivkp
Forked from venj/split.rb
Created March 14, 2017 14:33
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 rashivkp/ccdd72dc1f284043521f7b4766cc2575 to your computer and use it in GitHub Desktop.
Save rashivkp/ccdd72dc1f284043521f7b4766cc2575 to your computer and use it in GitHub Desktop.
Split a image into two halves.
#!/usr/bin/env ruby
require "fileutils"
(puts "Usage: #{File.basename $0} widthxheight"; exit 0) if ARGV.size != 1
width, height = ARGV[0].split("x").collect(&:to_i)
(puts "Please enter a valid size."; exit 0) if ((width.to_i == 0) or (height.to_i == 0))
half_width = width / 2
FileUtils.cd("images") do
Dir['**/*'].each do |f|
next if f == "split_image.rb"
puts "Done processing #{f}."
name = File.basename(f).split('.')[0]
system("convert -crop #{half_width}x#{height}+0+0 #{name}.jpeg #{name}-1.jpg")
system("convert -crop #{half_width}x#{height}+#{half_width}+0 #{name}.jpeg #{name}-2.jpg")
system("mv #{name}.jpeg ../images2")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment