PIL interpolation access

irond_will irond_will at yahoo.com
Tue Nov 30 17:55:25 EST 2004


I managed to get steps 2 and 4 fixed (grabbing and putting the
pixels).  I converted some code found at
http://effbot.org/zone/pil-numpy.htm written by Fredrick Lundh. 
Currently, it only works for RBG bitmaps.

I'm certain that there are better ways to do some of this, but it's
pretty zippy as it stands.  Now I really need to figure out the
bicubic interp, since that's the part that takes hours right now.


import image
import numarray

def image2numarray(im):

    #This function returns an array of RGB pixel values
    #The dimensions correspond as follows:
    #Dim 1 = column or x value
    #Dim 2 = row or y value
    #Dim 3 = color value or [R,G,B] for [0, 1, 2]
    
    if im.mode not in ("L", "F","RGB"):
        raise ValueError, "Unsupported image type"
    if im.mode == "L":
        a = fromstring(im.tostring(), UInt8)
        a.shape = im.size[1], im.size[0]
    elif im.mode == "F":
        a = fromstring(im.tostring(), Float32)
        a.shape = im.size[1], im.size[0]
    else:
        a = fromstring(im.tostring(), UInt8)
        a.shape = (im.size[1], im.size[0],3)

    a.transpose((1,0,2)) 
    
    return vect_math.array_flip(a,1)

def numarray2image(a):
    #This function returns an array of RGB pixel values
    #The dimensions correspond as follows:
    #Dim 1 = column or x value
    #Dim 2 = row or y value
    #Dim 3 = color value or [R,G,B] for [0, 1, 2]
    #The standard adopted here is that the lower-left corner is [0,0]
    
    a = a.astype('UInt8')
    b = vect_math.array_flip(a,1)
    b.transpose((1,0,2))
    return Image.fromstring('RGB', (b.shape[1], b.shape[0]),
b.tostring())

def array_flip(X,ndim):

    #flips X along specified axis

    return take(X,tuple(range(X.shape[ndim]-1,-1,-1)),axis=ndim)

"Caleb Hattingh" <caleb1 at telkomsa.net> wrote in message news:<opsia6ht1d1js0xs at news.telkomsa.net>...
> Hi
> 
> I can't help you directly, but I am also finding im.putpixel to be  
> extremely slow - as the docs recommend, can you try using the  
> pixel-placing method of Draw?  This is what I am going to try for my  
> application (resampling algorithms).
> 
> Thx
> Caleb



More information about the Python-list mailing list