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

[BUG] When padding an image, the dimensions get stretched #1015

Merged
merged 1 commit into from
Nov 25, 2024

Conversation

BritishWerewolf
Copy link
Contributor

@BritishWerewolf BritishWerewolf commented Nov 6, 2024

Fix that padding was stretching the original image, and the right side was overflowing the container.

Example

This is the code I used to generate the images.
I resize the images, so that they can be padded again to the same height as the original image.

RawImage.fromBlob(file)
.then(image => image.resize((256 / image.height) * image.width, 256))
.then(image => image.pad([128, 128, 128, 128]))
.then(image => image.save('padded.png'));
Original image Old solution New solution
A Moana Loungefly backpack on a white background A Moana Loungefly backpack, where this is 128 pixels of padding along the top and left, however the image is cropped on the right and bottom A Moana Loungefly backpack where there is an even 128 pixels of padding all around, and the image is still 256 pixels tall

As you can see above, the old solution will add the 128 pixel padding to the top and left, however the image is resized and drawn with the same size as the canvas, hence it is cropped by 128 pixels on the right and bottom.

However, the new solution will correctly draw the image with the provided padding all around as we now draw the image with the same dimensions that it was passed in with.

Explanation

The current implementation was drawing the image with a width of newWidth, where newWidth will be the entire width.
This is incorrect, since the width should remain unchanged when drawing, otherwise the image will become stretched.

As a result, we are getting the correct left padding and top padding, but the right and bottom of the image are getting cropped off since the image is being stretched out.

From the docs, the sWidth and sHeight should match dWidth and dHeight.

@xenova
Copy link
Collaborator

xenova commented Nov 16, 2024

Hi there, thanks for the PR! 👋 Can you provide some example code/input to see the incorrect behavior observed, and then the proposed corrected behaviour?

@BritishWerewolf
Copy link
Contributor Author

@xenova, I have updated the original PR to demonstrate how the proposed changes fix the previous bug.
I hope this is clear enough, although if you need further explanation please let me know.
Thank you.

@xenova xenova changed the base branch from v3 to main November 22, 2024 09:49
Copy link
Collaborator

@xenova xenova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again!

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@xenova xenova merged commit 1768b8b into huggingface:main Nov 25, 2024
1 check passed
@BritishWerewolf BritishWerewolf deleted the pad-bug branch November 26, 2024 21:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants