-
-
Notifications
You must be signed in to change notification settings - Fork 647
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Possible to add padding and keep 1 to 1 aspect ratio? #790
Comments
It wouldn't be hard in something like pyvips / ruby-vips / etc. I suppose I'd crop off the edges, take a 3D histogram, find the peak (which would be the most common colour in the edge areas), then use that colour to expand the original. |
@jcupitt Thanks for your quick reply! Would I be asking to much to get this added as a feature by any chance? I'm going to be poking around to see what I can find/accomplish . Cant say I'm an expert but gonna give it a try! |
I made you a tiny pyvips program to do this: import sys
import pyvips
im = pyvips.Image.new_from_file(sys.argv[1])
# the margin of pixel we extract to get the average edge
margin = 10
# paste black over the centre, take the histogram of the whole image
square = pyvips.Image.black(im.width - 2 * margin, im.height - 2 * margin)
hist = im.insert(square, margin, margin).hist_find()
# zap the 0 column to remove the black square
onepx = pyvips.Image.black(1, 1)
hist = hist.insert(onepx, 0, 0)
# then the histogram peak is the most common value in each band
bg = [x.maxpos()[1] for x in hist.gaussblur(1).bandsplit()]
# extend image out with that background
size = max(im.width, im.height)
im = im.embed((size - im.width) / 2, (size - im.height) / 2, size, size,
extend='background', background=bg)
im.write_to_file(sys.argv[2]) Run it like this:
For your test image, I get: |
@jcupitt you're the man, thank you very much for doing this! +1 +1 +1 |
No problem, I'll close. |
Hi there,
Just started recently using this code base and would like to state that its working great so far! Thank you very much.
I would like to see if its already possible to or if its in the works to be able to add padding to an image and preserve the aspect ratio. We use a tool that does this but tends to be very very slow. As you can see, in the second image, it adds the padding and finds the most weighted color to use.
For example:
Original image:
Padded image with 1 to 1 aspect ratio:
Thanks and hope all this makes sense :)
The text was updated successfully, but these errors were encountered: