Skip to content

Instantly share code, notes, and snippets.

@HakubJozak
Created September 2, 2024 21:49
Show Gist options
  • Select an option

  • Save HakubJozak/340179af19039b60625bdab569d7c925 to your computer and use it in GitHub Desktop.

Select an option

Save HakubJozak/340179af19039b60625bdab569d7c925 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'pathname'
require 'fileutils'
# Check if at least one argument is passed and at least two images are selected
if ARGV.length < 2
system("zenity --error --text='Usage: combine_images.rb <fairphone|iphone> <image1> <image2> ...'")
exit 1
end
# Extract the first argument and the list of images
images = ARGV
mode = ARGV.first =~ /heic$/i ? 'iphone' : 'fairphone'
# Set output directory and file name
output_dir = Pathname(File.dirname(images.first)).join('combined')
FileUtils.mkdir_p(output_dir.to_s)
# Generate a unique output file name
output = output_dir.join("#{mode}-0000")
output = Pathname(output.to_s.succ) while output.sub_ext('.jpg').exist?
output_file = output.sub_ext('.jpg')
# Function to combine images
def combine_images(images, output_file)
command = "magick montage #{images.join(' ')} -tile x1 -geometry +0+0 #{output_file}"
system(command)
end
# Handle different modes
case mode
when "fairphone"
combine_images(images, output_file)
when "iphone"
rotated_images = images.map do |image|
rotated_image = "#{File.dirname(image)}/rotated_#{File.basename(image)}"
system("magick #{image} -rotate 90 #{rotated_image}")
rotated_image
end
combine_images(rotated_images, output_file)
# Optionally, remove the rotated images
rotated_images.each { |image| File.delete(image) }
else
system("zenity --error --text='Invalid mode. Use either \"fairphone\" or \"iphone\".'")
exit 1
end
# Notify the user
unless $?.success?
system("zenity --error --text='Failed to combine images.'")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment