[Image-SIG] Problem with 16 bit scaling and drawing, and workaround

Chuck Lloyd chuck.lloyd at gmail.com
Fri Sep 17 17:28:24 CEST 2010

I'm working with 16bit gray-scale images but I'm having some problems
with drawing and higher order interpolation.

Nearest-neighbor interpolation works fine, but anything else corrupt the

I'm using version 1.1.7 on win32 that comes with Python(x,y).

If I convert the images to mode "I" (32bit int) and then back to "I;16" it
seems to work fine.

I looked through libImaging for the interpolation code.
It looks like it is off by half in the width, uUsing a 32bit
stride rather than 16 bit stride.

I don't have time right now to learn the codebase and make the fix
but maybe someone more familiar, more motivated or with more time can.

The code below shows the problem and workaround.

import Image, ImageDraw

def draw_doodle( img ):
    draw = ImageDraw.Draw(img)

    (width,height) = img.size
    width = width - 1
    height = height -1
    draw.line( (0, 0, width, height ), fill="#FFFFFF")
    draw.line( (width, 0, 0, height ), fill="#CCCCCC")
    draw.line( (0, height/2, width, height/2), fill="#888888")

# Make a blank tiff
file = r'dummy.tif'
blank = Image.new("I;16", (80,123))
blank.save( file )

# Draw on image
im16 = Image.open( file )
draw_doodle( im16 )
print im16
#Scale it
im16s = im16.resize((80,126), Image.BILINEAR )

# Now do it after converting to I type
im16 = Image.open( file )
im32 = im16.convert("I")

draw_doodle( im32 )
print im32

#Scale it
im32s = im32.resize((80,126), Image.BILINEAR )
im = im32s.convert("I;16")
print im

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/image-sig/attachments/20100917/8734f732/attachment-0001.html>

More information about the Image-SIG mailing list