diff options
| author | Charlotte Koch <charlotte@magentastripe.com> | 2023-12-23 15:53:00 -0800 |
|---|---|---|
| committer | Charlotte Koch <charlotte@magentastripe.com> | 2023-12-23 15:53:00 -0800 |
| commit | f7d69c1bfaaf9c47cae44688e89abbb4215cbc04 (patch) | |
| tree | b148443c8e1e2cbc90f0afb4b3b18bee2dcd2479 | |
| parent | 1057d75fd7f9961048aa5280e00d44cc0a0fe32b (diff) | |
Add a tool to help split an image onto two pages
This is mostly for such things as world maps which span both the left and
right pages in an open book
| -rw-r--r-- | Gemfile | 1 | ||||
| -rw-r--r-- | script/image_splitter.rb | 47 |
2 files changed, 48 insertions, 0 deletions
@@ -6,4 +6,5 @@ gem "asciidoctor-epub3", git: "https://github.com/magentastripe/asciidoctor-epub3", branch: "magentastripe" gem "epubcheck-ruby" +gem "mini_magick" gem "rghost" diff --git a/script/image_splitter.rb b/script/image_splitter.rb new file mode 100644 index 0000000..0add63c --- /dev/null +++ b/script/image_splitter.rb @@ -0,0 +1,47 @@ +#!/usr/bin/env ruby + +require 'optparse' +require 'mini_magick' + +image = nil +left_out = nil +right_out = nil + +# Parse and verify command-line arguments. +parser = OptionParser.new do |opts| + opts.on("--input PATH") { |path| image = File.expand_path(path) } + opts.on("--left-out PATH") { |path| left_out = File.expand_path(path) } + opts.on("--right-out PATH") { |path| right_out = File.expand_path(path) } +end +parser.parse!(ARGV) + +if not image + $stderr.puts("FATAL: expected an image.") + exit 1 +end + +if not [left_out, right_out].all? + $stderr.puts("FATAL: expected L and R output images.") + exit 1 +end + +if not File.file?(image) + $stderr.puts("FATAL: no such file: #{image}") + exit 1 +end + +# Split the image into two halves. +image_f = MiniMagick::Image.open(image) +w, h = image_f.dimensions +new_w = w/2 +$stderr.puts("Input image #{File.basename(image)} has dimensions #{w}x#{h}") +$stderr.puts("Will create two images of size #{new_w}x#{h}") + +lefthand_crop = image_f.crop("#{new_w}x#{h}+0+0") +lefthand_crop.write(left_out) + +# Need to open the original image twice in order to get back to the +# "starting point." +image_f = MiniMagick::Image.open(image) +righthand_crop = image_f.crop("#{new_w}x#{h}+#{new_w}+0") +righthand_crop.write(right_out) |
