Skip to content

Instantly share code, notes, and snippets.

@pbosetti
Created December 30, 2010 09:36
Show Gist options
  • Save pbosetti/759628 to your computer and use it in GitHub Desktop.
Save pbosetti/759628 to your computer and use it in GitHub Desktop.
Makes an image by stripping a bunch of images
#!/usr/bin/env ruby
# transimage.rb
#
# Created by Paolo Bosetti on 2010-12-03.
# Copyright (c) 2010 University of Trento. All rights reserved.
#
require "rubygems"
require "RMagick"
name_templ = "test"
name_rng = 7..98
file_list = []
name_rng.each do |i|
file_list << "#{name_templ}_#{i}.jpg"
end
current = Magick::Image.read(file_list[0])[0]
cols = current.columns
rows = current.rows
rest = cols % file_list.count
strip_w = Array.new(file_list.count, (cols / file_list.count).to_i)
if rest > 0 then
strip_w[-1] = (cols / file_list.count).to_i + rest
end
puts "Original #{cols}x#{rows}: Splitting in column bins:"
puts "#{strip_w.inspect} (checksum = #{strip_w.inject(0) {|v, s| s + v}})"
result = Magick::Image.new(cols, rows)
position = 0
strip_w.each_with_index do |w, i|
puts "Strip #{i+1}/#{name_rng.count} (#{file_list[i]}) from #{position} to #{position + w}"
current = Magick::Image.read(file_list[i])[0]
pixels = current.get_pixels(position, 0, w, rows)
result.store_pixels(position, 0, w, rows, pixels)
position += w
end
result.write "#{name_templ}_tri.jpg"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment