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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions src/utils/image.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

/**
* @file Helper module for image processing.
*
* These functions and classes are only used internally,
* @file Helper module for image processing.
*
* These functions and classes are only used internally,
* meaning an end-user shouldn't need to access anything here.
*
*
* @module utils/image
*/

Expand Down Expand Up @@ -91,7 +91,7 @@ export class RawImage {
this.channels = channels;
}

/**
/**
* Returns the size of the image (width, height).
* @returns {[number, number]} The size of the image (width, height).
*/
Expand All @@ -101,9 +101,9 @@ export class RawImage {

/**
* Helper method for reading an image from a variety of input types.
* @param {RawImage|string|URL} input
* @param {RawImage|string|URL} input
* @returns The image object.
*
*
* **Example:** Read image from a URL.
* ```javascript
* let image = await RawImage.read('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg');
Expand Down Expand Up @@ -181,7 +181,7 @@ export class RawImage {

/**
* Helper method to create a new Image from a tensor
* @param {Tensor} tensor
* @param {Tensor} tensor
*/
static fromTensor(tensor, channel_format = 'CHW') {
if (tensor.dims.length !== 3) {
Expand Down Expand Up @@ -355,7 +355,7 @@ export class RawImage {
case 'nearest':
case 'bilinear':
case 'bicubic':
// Perform resizing using affine transform.
// Perform resizing using affine transform.
// This matches how the python Pillow library does it.
img = img.affine([width / this.width, 0, 0, height / this.height], {
interpolator: resampleMethod
Expand All @@ -368,7 +368,7 @@ export class RawImage {
img = img.resize({
width, height,
fit: 'fill',
kernel: 'lanczos3', // PIL Lanczos uses a kernel size of 3
kernel: 'lanczos3', // PIL Lanczos uses a kernel size of 3
});
break;

Expand Down Expand Up @@ -408,13 +408,14 @@ export class RawImage {
// Draw image to context, padding in the process
ctx.drawImage(canvas,
0, 0, this.width, this.height,
left, top, newWidth, newHeight
left, top, this.width, this.height
xenova marked this conversation as resolved.
Show resolved Hide resolved
);

// Create image from the padded data
const paddedImage = new RawImage(
ctx.getImageData(0, 0, newWidth, newHeight).data,
newWidth, newHeight, 4);
newWidth, newHeight, 4
);

// Convert back so that image has the same number of channels as before
return paddedImage.convert(numChannels);
Expand Down Expand Up @@ -447,7 +448,7 @@ export class RawImage {
// Create canvas object for this image
const canvas = this.toCanvas();

// Create a new canvas of the desired size. This is needed since if the
// Create a new canvas of the desired size. This is needed since if the
// image is too small, we need to pad it with black pixels.
const ctx = createCanvasFunction(crop_width, crop_height).getContext('2d');

Expand Down Expand Up @@ -495,7 +496,7 @@ export class RawImage {
// Create canvas object for this image
const canvas = this.toCanvas();

// Create a new canvas of the desired size. This is needed since if the
// Create a new canvas of the desired size. This is needed since if the
// image is too small, we need to pad it with black pixels.
const ctx = createCanvasFunction(crop_width, crop_height).getContext('2d');

Expand Down Expand Up @@ -742,4 +743,4 @@ export class RawImage {
}
});
}
}
}
Loading