Skip to content

Instantly share code, notes, and snippets.

@ramses-lopez
Created April 29, 2023 03:46
Show Gist options
  • Save ramses-lopez/72e27f7edeba73c811771f200e9684ed to your computer and use it in GitHub Desktop.
Save ramses-lopez/72e27f7edeba73c811771f200e9684ed to your computer and use it in GitHub Desktop.
Use ruby-vips to crop one image into smaller images
require 'vips'
input_image = Vips::Image.new_from_file('all_pics.webp')
x = 0
y = 0
width = 100
height = 100
gap = 28
(0..6).to_a.each do |i|
(0..6).to_a.each do |j|
puts "Cropping image #{i} #{j}"
x = j * (width + gap)
y = i * (height + gap)
puts "x: #{x}, y: #{y}"
input_image
.crop(x, y, width, height)
.write_to_file("cropped_pics/#{i}_#{j}.webp")
puts "Image #{i} #{j} cropped!\n\n"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment