Skip to content

Instantly share code, notes, and snippets.

@detunized
Created November 23, 2016 20:49
Show Gist options
  • Save detunized/5e0318cc86ee0202959d178665a81d9d to your computer and use it in GitHub Desktop.
Save detunized/5e0318cc86ee0202959d178665a81d9d to your computer and use it in GitHub Desktop.
Makes a series of A3 posters for cutting from a bunch of images
#!/usr/bin/env ruby
# This script makes a series of A3 posters for cutting from a bunch of images.
require "rational"
R_4_3 = Rational(4, 3)
R_3_4 = Rational(3, 4)
system "rm -rf out"
system "mkdir out"
images = {}
Dir.glob("*.jpg") do |file|
info = `identify '#{file}'`
w, h = info[/(\d+x\d+)/, 1].split("x").map(&:to_i)
ratio = Rational(w, h)
new_w = 0
new_h = 0
rotate = false
if ratio == R_4_3
new_w = w
new_h = h
elsif ratio == R_3_4
new_w = h
new_h = w
rotate = true
elsif w > h # horizontal
if ratio > R_4_3
new_h = h / 3 * 3
new_w = new_h / 3 * 4
else
new_w = w / 4 * 4
new_h = new_w / 4 * 3
end
else # vertical
if ratio < R_3_4
new_h = w / 3 * 3
new_w = new_h / 3 * 4
else
new_w = h / 4 * 4
new_h = new_w / 4 * 3
end
rotate = true
end
images[file] = {w: w, h: h, new_w: new_w, new_h: new_h, rotate: rotate}
end
min_w = images.map { |k, v| v[:new_w] }.min
min_h = images.map { |k, v| v[:new_h] }.min
images.each do |file, info|
if info[:rotate]
system "convert '#{file}' -resize #{min_h}x#{min_w}^ -gravity center -extent #{min_h}x#{min_w} -rotate 90 +repage 'out/#{file}'"
else
system "convert '#{file}' -resize #{min_w}x#{min_h}^ -gravity center -extent #{min_w}x#{min_h} +repage 'out/#{file}'"
end
end
# Verify
Dir.glob("out/*.jpg") do |file|
info = `identify '#{file}'`
w, h = info[/(\d+x\d+)/, 1].split("x").map(&:to_i)
ratio = Rational(w, h)
if ratio != R_4_3
puts "#{file} aspect raito is not 4:3 but #{ratio}"
end
end
# Make tiles
system "montage out/*.jpg -tile 4x4 -geometry +1+1 -quality 100% out/poster_%d.jpg"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment