Last active
April 30, 2023 02:32
-
-
Save jcupitt/8939150 to your computer and use it in GitHub Desktop.
auto-crop in ruby-vips
This file contains 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
#!/usr/bin/env ruby | |
# "trim" is nnow built in, so this is easy | |
require 'vips' | |
im = Vips::Image.new_from_file(ARGV[0]) | |
left, top, width, height = im.find_trim | |
im = im.crop(left, top, width, height) | |
im.write_to_file(ARGV[1]) |
Easier now:
#!/usr/bin/env ruby
require 'rubygems'
require 'vips'
im = Vips::Image.new_from_file(ARGV[0])
left, top, width, height = im.find_trim
im = im.extract_area(left, top, width, height)
im.write_to_file(ARGV[1])
Thanks @dkam, updated.
It should work. Your test PNG image has a solid alpha, so find_trim
cant see the edges.
Try:
#!/usr/bin/env ruby
require 'vips'
im = Vips::Image.new_from_file(ARGV[0])
left, top, width, height = im.find_trim background: 0
im = im.extract_area(left, top, width, height)
im.write_to_file(ARGV[1])
If I run:
$ vips flatten ~/pics/marble.png x.png
$ ./trim4.rb x.png y.png
I get:
thank you. hm. seems background: 0
did it. but the filesize is bigger after
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is it still working? Or is there now a vips command line operator instead of script to do this?