RawImage.resize in Node (sharp affine) darkens image borders: a uniform image does not stay uniform

Open Beginner friendly
#1,885 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

  • #1919 by @2Zest — closed without merging

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
javascript, node.js

Research direction

Start by reading the Node implementation of RawImage.resize and compare its sharp affine path with the Lanczos resize path. Reproduce the issue with a uniform image and check the resized borders; the fix is done when uniform input stays uniform at the edges for the affected resampling modes.

Written by the indexing model from the issue text.

Description

System Info

@huggingface/transformers 4.3.2 (same code on main 656d6273), sharp 0.35.5, Node.js 26.13.0, Linux x86_64. Python reference: Pillow 13.0.0.

Environment/Platform
  • Server-side (e.g., Node.js, Deno, Bun)
Description

In Node, RawImage.resize with nearest/bilinear/bicubic uses sharp().affine([sx, 0, 0, sy]). The affine transform interpolates against a black background outside the image, so the last row and column are blended with black. Resizing a uniform gray image (value 200) to twice its size gives 100 in the last column and 50 in the corner. Downscaling is affected too: a uniform 300x200 image resized to 224x224 with bicubic gives values between 186 and 219. Pillow returns 200 everywhere in both cases.

All image processors that resize (ViT, CLIP, SigLIP, DPT, Segformer, Donut, ...) pass this to the model in Node, so pixel_values differ from Python at the image borders (up to 3.5 after normalization in my tests, e.g. Segformer 224x224 -> 512x512).

Reproduction
import { RawImage } from "@huggingface/transformers";
const image = new RawImage(new Uint8ClampedArray(10 * 10 * 3).fill(200), 10, 10, 3);
const out = await image.resize(20, 20, { resample: 2 });
console.log(Array.from(out.data.slice(0, 20 * 3).filter((_, i) => i % 3 === 0)));
[
  200, 200, 200, 200, 200,
  200, 200, 200, 200, 200,
  200, 200, 200, 200, 200,
  200, 200, 200, 200, 100
]

Expected all 200, as with Pillow:

from PIL import Image
import numpy as np
a = np.asarray(Image.new("RGB", (10, 10), (200, 200, 200)).resize((20, 20), Image.BILINEAR))
print(a.min(), a.max())  # 200 200

Using sharp().resize({ width, height, fit: "fill", kernel }) as the lanczos branch does, or passing an edge-extending background to affine, avoids the black bleed.

Credit: https://ucsbplse.github.io/

Dominant language
JavaScript
Stars
16.4k
Forks
1.2k
Avg merge
4d 11h
Merged PRs (30d)
10

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from huggingface/transformers.js

All issues in huggingface/transformers.js

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.