Skip to content
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

Closed
jrowinski88 opened this issue Oct 30, 2017 · 5 comments
Closed

Possible to add padding and keep 1 to 1 aspect ratio? #790

jrowinski88 opened this issue Oct 30, 2017 · 5 comments
Labels

Comments

@jrowinski88
Copy link

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:
cobain1

Padded image with 1 to 1 aspect ratio:

cobain2

Thanks and hope all this makes sense :)

@jcupitt
Copy link
Member

jcupitt commented Oct 30, 2017

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.

@jrowinski88
Copy link
Author

@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!

@jcupitt
Copy link
Member

jcupitt commented Oct 30, 2017

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:

john@kiwi:~/try$ python extend_avg.py ~/pics/greybg.png x.png

For your test image, I get:

x

@jrowinski88
Copy link
Author

@jcupitt you're the man, thank you very much for doing this! +1 +1 +1

@jcupitt
Copy link
Member

jcupitt commented Nov 24, 2017

No problem, I'll close.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants