Skip to content

Instantly share code, notes, and snippets.

@julian-a-avar-c
Last active January 4, 2022 04:22
Show Gist options
  • Save julian-a-avar-c/12289c4f47d34f1db8f8e0998fb903f3 to your computer and use it in GitHub Desktop.
Save julian-a-avar-c/12289c4f47d34f1db8f8e0998fb903f3 to your computer and use it in GitHub Desktop.
stitches a collection of images vertically, and upscales/downscales to a specific width
#!/usr/bin/env ruby
# Licensed under MIT. (c) 2022 Julian A Avar Campopiano
# Requires ruby3.0.0 and imagemagick
# Usage:
# stitch.rb [options] -i=[glob] -o=[glob]
# Example:
# stitch.rb -w=min -i=*.jpg
# stitch.rb -w=259.3 -i=photo_{a,b}.png -o=out_file.png
def get_input()
input = ARGV.map do |arg|
width = arg.match(/^$|^(--width|-w)=(min|max|avg|\d+(\.\d+)?)$/)
input_img = arg.match(/^$|^(--input|-i)=(\S+)$/)
output_img = arg.match(/^$|^(--output|-o)=(\S+)$/)
if width != nil
[:width, width[2]]
elsif input_img != nil
[:input, input_img[2]]
elsif output_img != nil
[:output, output_img[2]]
else
nil
end
end.to_h
input[:width] = "min" unless input.has_key?(:width)
input[:input] = "*.*" unless input.has_key?(:input)
input[:output] = "out.png" unless input.has_key?(:output)
return input
end
def main()
input = get_input()
puts input.to_a.map { |pair| "#{pair[0].to_s}=#{pair[1].to_s}"}.join("\n")
all_images_width = `magick identify -format '%[fx:w] ' #{input[:input]}`.split(" ").map { |w| w.to_i }
width = case input[:width]
in "min" then all_images_width.min
in "max" then all_images_width.max
in "avg" then all_images_width.then{ |a| a.sum.to_f / a.size }.floor
in w then w.to_f.floor
end
`magick #{input[:input]} -resize #{width} -append #{input[:output]}`
end
main() if $PROGRAM_NAME == __FILE__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment