Sharp: Nearest neighbor upsampling stretching image

Created on 27 Nov 2018  路  6Comments  路  Source: lovell/sharp

Images are being stretched when resized with sharp.kernel.nearest, however the stretching only happens when the resolution is not an exact scale like 8 (1), 16 (2), or 24 (3).

Exact scale (x32) with kernel nearest:

image

Non exact scale with kernel nearest:

screenshot 172

Non exact scale with default kernel:

screenshot 171

bug ready-to-ship

All 6 comments

Hello, please can you share the input image and (standalone) code sample.

Here's the original image:
image

And here's the code:

const imageBuffer = /* ... */;
const size = 250; // causes stretching

const avatar = await sharp(imageBuffer).extract({
  left: 8,
  top: 8,
  width: 8,
  height: 8
}).resize({
  width: size,
  height: size,      
  kernel: sharp.kernel.nearest
}).png().toBuffer();

Thank you, I've been able to reproduce this.

If I extract the 8x8 image:

extract

then use vips at the command line to resize with a scale of 31.25 (=250/8)

$ vips resize extract.png out.png 31.25 --kernel=nearest

the result is:

out

so it looks like this is happening within libvips itself.

Hello, should be fixed in git master, thanks for the report! It was to do with the way centre vs. corner sampling is implemented.

With this 3x3 pixel PNG:

3x3

If I run:

$ vips resize 3x3.png 3x3-nearest.png 83.33333333333 --kernel nearest 
$ vips resize 3x3.png 3x3-cubic.png 83.33333333333

I now see:

3x3-nearest

3x3-cubic

Thanks @jcupitt !

sharp v0.23.0 / libvips v8.8.1 is now available. Thanks for reporting this @edwjusti.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tomercagan picture tomercagan  路  3Comments

emmtte picture emmtte  路  3Comments

terbooter picture terbooter  路  3Comments

natural-law picture natural-law  路  3Comments

AVVS picture AVVS  路  3Comments