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

Add option to maintain aspect ratio on resize #971

Merged
merged 4 commits into from
Nov 25, 2024

Conversation

BritishWerewolf
Copy link
Contributor

In some cases it is required to resize an image, but maintain the aspect ratio.
This is usually because we want to pad the image.

Here's an example of something I was thinking of.

const image = await RawImage.fromURL("https://picsum.photos/600/400");
const resized = await image.resize(300, null); // 300x200
const padHeight = (300 - image.height) / 2;
const padded = await image.pad([0, 0, padHeight, padHeight]); // 300x300

In this example, we resize the image, maintain the aspect ratio and then pad either side.

@xenova xenova changed the base branch from v3 to main November 6, 2024 14:42
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 for the PR! Very useful indeed 👍 Just some minor suggestions:

src/utils/image.js Outdated Show resolved Hide resolved
tests/utils/utils.test.js Show resolved Hide resolved
BritishWerewolf and others added 2 commits November 6, 2024 23:30
Reduce function calls for optimisation.

Co-authored-by: Joshua Lochner <[email protected]>
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 for your contribution! This will be included in an upcoming release.

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

src/utils/core.js Outdated Show resolved Hide resolved
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.

Merging now ✅

@xenova xenova merged commit 23647e2 into huggingface:main Nov 25, 2024
4 checks passed
@BritishWerewolf BritishWerewolf deleted the resize-ar branch November 26, 2024 21:29
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