Pregunta

I'm trying to rescale a 32x32 pixel image to 28x28 using PIL's resize function. I begin by reading in a PGM image whose pixel values are all either 255 or 0 (I verified by looking at the file). However, when I rescale, some pixel values turn negative.

currIm = Image.open(os.path.join(currSrc,currFile))
currIm2 = currIm.resize((28,28), Image.BICUBIC

imData = numpy.asarray(currIm)
imData2 = numpy.asarray(currIm2)

All the values in imData are either 0.0 or 255.0, which is what I expect. However, the 'anti-aliased' pixels in imData2 tend to be negative with values varying from 0 to -50 and generally around -30.

Why does this happen and how can I fix it? For what it's worth, both currIm & currIm2 have mode = 'F' (Float??)

Note:This does not seem to be a problem with Image.BILINEAR interpolation

¿Fue útil?

Solución

The PIL implementation may actually be correct.

Some interpolation functions, including bicubic, are prone to "overshoot". Basically if the change in values in a small area is dramatic enough, the interpolation function can yield values outside the original range.

Take a look at the Wikipedia article on Bicubic Interpolation. There's an example image demonstrating some of the effects and references to acutance, ringing, and clipping.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top