Last active
November 18, 2023 05:56
-
-
Save veganstraightedge/3d9908742ee9802100ce36fdde943076 to your computer and use it in GitHub Desktop.
REQUIRES `imagemagick` installed. ONLY TESTED ON MY LAPTOP. BUYER BEWARE! Export four individual image files from one source image file. (Source is one big file from entire flatbed scanner or four Polaroids. Each individual Polaroid gets exported to its own file.)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source_files = ARGV[0] | |
# optional file type args | |
# passed in like: ruby crop.rb path/to/files source_extension export_extension | |
# ruby crop.rb path/to/files heic png | |
source_extension_arg = ARGV[1] | |
export_extension_arg = ARGV[2] | |
# both extensions default to heic if not passed in | |
SOURCE_EXTENSION = source_extension_arg.nil? ? "heic" : source_extension_arg | |
EXPORT_EXTENSION = export_extension_arg.nil? ? "heic" : export_extension_arg | |
RECTANGLES = { | |
a: "4641x5643+455+611", # top_left | |
b: "4641x5643+5125+600", # top_right | |
c: "4641x5643+455+6232", # bottom_left | |
d: "4641x5643+5125+6210" # bottom_right | |
} | |
# go to directory passed into script as a CLI arg | |
Dir.chdir(source_files) do | |
# find only files that have the right file extension | |
# TODO: hardcoded to .png above (for now?), is this YAGNI? | |
Dir.glob("*.#{SOURCE_EXTENSION}").each do |current_file| | |
# STEP: delete already exported files | |
# already exported files’ names are formatted: 1a.png (NUMBER + LETTER + .png) | |
if current_file =~ /\d+[a-z].#{SOURCE_EXTENSION}/ | |
export_current_file = current_file | |
# delete already exported files so magick doesn’t need to confirm overwrite [y/N] | |
# TODO: what is the magick arg to confirm overwrite | |
File.delete export_current_file | |
# then move onto next file, aka don't delete source files | |
next | |
end | |
# STEP: crop and export from source files | |
# source files’ names are formatted: 1.png (NUMBER + .SOURCE_EXTENSION) | |
if current_file =~ /\d+.#{SOURCE_EXTENSION}/ | |
# get the source filename’s number to use in export’s filename with letter | |
number, _extension = current_file.split(".") | |
# go through each exportable image area | |
RECTANGLES.each do |letter, whxy_args| | |
# export files’ names are formatted: 1a.png (NUMBER + LETTER + .EXPORT_EXTENSION) | |
export_filename = "#{number}#{letter}.#{EXPORT_EXTENSION}" | |
command = <<~IMAGEMAGICK_COMMAND | |
magick #{current_file} -crop #{whxy_args} #{export_filename} | |
IMAGEMAGICK_COMMAND | |
system command | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment