Created
January 15, 2025 17:59
-
-
Save cp/889d4f3c88d7401978c956c911cb74d1 to your computer and use it in GitHub Desktop.
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
require "rmagick" | |
crop = [ | |
0.46277403831481934, # x_min (proportion from left) | |
0.5259760022163391, # y_min (proportion from top) | |
0.36609894037246704, # x_max (proportion from left) | |
0.4222343862056732, # y_max (proportion from top) | |
] | |
input_path = "./input.jpeg" | |
begin | |
image = Magick::Image.read(input_path).first | |
width = image.columns | |
height = image.rows | |
x = (width * crop[0]).round | |
y = (height * crop[1]).round | |
crop_width = (width * (crop[2] - crop[0])).round | |
crop_height = (height * (crop[3] - crop[1])).round | |
cropped_image = image.crop(x, y, crop_width, crop_height) | |
cropped_image.write("output.jpeg") | |
rescue Magick::ImageMagickError => e | |
puts "Error processing image: #{e.message}" | |
rescue StandardError => e | |
puts "Error: #{e.message}" | |
ensure | |
image&.destroy! | |
cropped_image&.destroy! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment