[Image-SIG] Problem with Image.resize() using nearest-neighbor resampling?

Chris Ball ceball at gmail.com
Tue Jul 14 15:16:01 CEST 2009


I'd like to resize an image by scaling each of its pixels by the same

I've tried using Image.resize() with the filter set to NEAREST, but I
find that the pixels in the original image do not all end up the same
size in the new image. Do I have the wrong idea about what NEAREST

Here is an example to demonstrate my problem (using numpy to avoid
needing an image file, and to make it easier to show the results in
text; you could instead use a 4x4 image and the .show() method of
Image to see what I'm talking about):

python> import Image,numpy
python> a = numpy.array([[255,0,255,0],[0,255,0,255],
    ... [255,0,255,0],[0,255,0,255]],dtype=numpy.uint8)
python> a
array([[255,   0, 255,   0],
       [  0, 255,   0, 255],
       [255,   0, 255,   0],
       [  0, 255,   0, 255]], dtype=uint8)
python> i=Image.fromarray(a)
python> i2=i.resize((6*4,6*4),Image.NEAREST)

I expected each pixel in i to have been enlarged to be 6x6 in i2, but
that doesn't seem to have happened. Here is the first column of the

python> numpy.array(i2)[:,0]
array([255, 255, 255, 255, 255, 255, 255,   0,   0,   0,   0,   0, 255,
       255, 255, 255, 255, 255, 255,   0,   0,   0,   0,   0], dtype=uint8)

Why hasn't each pixel been enlarged to have a side of length 6? The
first pixel has been enlarged to have side of length 7, the next
length 5, then 7, then 5.


P.S. I'm using PIL 1.1.6.

More information about the Image-SIG mailing list