Skip to content

Image.resize() incorrectly centers non-nearest filters #986

@yromanenko

Description

@yromanenko

(Discovered in the context of scipy, see scipy/scipy#4112 for more details)

Using an interpolation method other than 'nearest' with Image.resize results in an image shift away from (0,0) in the result (among other problems)

It behaves as though the filter kernel starts at (-0.5, -0.5), but is walked across the image without a corresponding change in step size.

Repeated application of Image.resize with Image.BILINEAR results in the image shrinking and walking off:
linear
even more weird, but still walking with Image.BICUBIC:
cubic
while 'Image.NEAREST' behaves as expected:
nearest

I started looking at Geometry.c, and it seems like it needs a bit of love, but I am very unfamiliar with the code so I'm hesitant to start rewriting things :-)

Here's the test code:


from PIL import Image

src = Image.open("src.png").convert('F')
cbc = src
lnr = src
nrs = src

for idx in xrange(0, 128):
    cbc = cbc.resize(tuple(2 * i for i in cbc.size), Image.BICUBIC)
    cbc = cbc.resize(src.size, Image.BICUBIC)

    lnr = lnr.resize(tuple(2 * i for i in lnr.size), Image.BILINEAR)
    lnr = lnr.resize(src.size, Image.BILINEAR)

    nrs = nrs.resize(tuple(2 * i for i in nrs.size), Image.NEAREST)
    nrs = nrs.resize(src.size, Image.NEAREST)

    nrs.convert('L').save("nrs_%03d.png" % (idx))
    lnr.convert('L').save("lnr_%03d.png" % (idx))
    cbc.convert('L').save("cbc_%03d.png" % (idx))

Thanks

Platform information:

Python 2.7.8 (default, Jul 13 2014, 17:11:32)
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugAny unexpected behavior, until confirmed feature.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions