RawImage.resize in Node (sharp affine) darkens image borders: a uniform image does not stay uniform
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
- Domain
- computer-vision
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
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from huggingface/transformers.js
-
wav2vec2 ASR drops repeated letters ("slipper" → "sliper"): blank tokens are skipped before CTC merges repeatsPossibly taken @chessing234 claimed this 3 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
huggingface/transformers.js#1947 ·
Maintainers usually reply within 1 day
-
getSession: an unhandled rejection of bufferOrPathPromise ends Node when an external data download failsPossibly taken @chessing234 claimed this 3 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
huggingface/transformers.js#1946 · 1 comment ·
Maintainers usually reply within 1 day
-
Expose the browser build as a subpath export (./dist/transformers.web.js)Possibly taken @anshusaurav claimed this 4 days ago. Open
Difficulty 1/5 Under an hour Newbie friendliness 88/100
huggingface/transformers.js#1945 · 1 comment ·
Maintainers usually reply within 1 day
-
TextStreamer drops apostrophes with WordPiece tokenizers (clean_up_tokenization_spaces)Possibly taken @erdemozkan claimed this 7 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
huggingface/transformers.js#1940 ·
Maintainers usually reply within 1 day
-
Image processors with shortest_edge + longest_edge upscale every image to longest_edge (DETR, YOLOS)May be free again A pull request for this issue was closed without being merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
huggingface/transformers.js#1886 · 1 comment ·
Maintainers usually reply within 1 day
All issues in huggingface/transformers.js
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 73/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 80/100
speakeasy-api/speakeasy#2159 ·
Maintainers usually reply within 1 day
-
Queued deferred selectors still run after unmountPossibly taken @LexaTang claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
ianstormtaylor/slate#6375 ·
Maintainers usually reply within 1 day
-
sync cache dist/imgOpen
Difficulty 1/5 Under an hour Newbie friendliness 82/100
oleeskild/digitalgarden#435 ·