[Image-SIG] bug in nearest-neighbour resize

Nicolas Limare nicolas.limare at cmla.ens-cachan.fr
Wed Mar 2 08:01:43 CET 2011


Hi all,

I think there is a bug in the nearest neighbour resizing filter: when
resizing an image with an integer ratio, the original pixels are not
all expanded by this ratio. For example:

>>> from PIL import Image
>>> Image.VERSION
'1.1.7'
>>> im = Image.open("input.png")
>>> for x in range(0, 6):
...     im.getpixel((x, 0))
... 
(255, 0, 0)
(0, 255, 0)
(0, 0, 255)
(255, 0, 0)
(0, 255, 0)
(0, 0, 255)

The first column of my input image is RGBRGB...

>>> zim = im.resize((im.size[0] * 3, im.size[0] * 3), Image.NEAREST)
>>> for x in range(0, 18):
...     zim.getpixel((x, 0))
... 
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 0, 255)
(0, 0, 255)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 0, 255)
(0, 0, 255)

Note that the first pixel is repeated 3 times, the second 4 times,
then 2, 3, 4, amd 2 times. I would expect each pixel to be repeated 3
times, and I think that is what "nearest neighbour" means. 

I could track the bug down to libImaging/Geometry.c, function
ImagingScaleAffine(), lines 672-697. there must be something wrong in
these two loops, probably related to the integer rounding, but I
wasn't able to figure out the exact meaning of all the variables.

-- 
Nicolas LIMARE - CMLA - ENS Cachan    http://www.cmla.ens-cachan.fr/~limare/
IPOL - image processing on line                          http://www.ipol.im/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://mail.python.org/pipermail/image-sig/attachments/20110302/fc64c694/attachment.pgp>


More information about the Image-SIG mailing list