Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#!/usr/bin/env ruby
#
# Gridify draws a grid on images, producing a .grid image
# gridify.rb <pixels> <color> <file1> <file2> <file3> ...
#
# Dependencies:
# ruby
# rubygems
# ImageMagick
# RMagick gem
require 'rubygems'
require 'RMagick'
#
# draw_line is useful for debugging and testing
# it paints a red line on the part of the image passed in x
#
def draw_line(filename, image, center, orientation=:vertical, color='red')
cols = image.columns
rows = image.rows
length = orientation==:vertical ? rows : cols
redline = []
(3*length).times do
redline << Magick::Pixel.from_color(color)
end
if orientation==:vertical
image.store_pixels(center-1,0,3,length, redline)
else
image.store_pixels(0,center-1,length,3, redline)
end
# ext = File.extname(filename)
# image.write(filename.sub(ext, ".autosplit#{ext}"))
end
pixels = ARGV.shift.to_i
color = ARGV.shift
ARGV.each do |filename|
print "Drawing a #{color} line every #{pixels} pixels on #{filename}\n"
image = Magick::ImageList.new(filename)
x = pixels.to_i
while x < image.columns
print "\tdrawing vertical line at #{x}\n"
draw_line(filename, image, x, :vertical, color)
x = x + pixels
end
y = pixels.to_i
while y < image.rows
print "\tdrawing horizontal line at #{y}\n"
draw_line(filename, image, y, :horizontal, color)
y = y + pixels
end
ext = File.extname(filename)
image.write(filename.sub(ext, ".grid#{ext}"))
GC.start
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment