[Numpy-discussion] Optimization Question

Alex Schultz alex_sch at telus.net
Sat Aug 6 14:53:09 CDT 2005


Hi,

I made nice function that composts two RGBA image arrays, however I'm 
having issues with it being too slow. Does any body have any suggestions 
on how to optimize this (the subfunction 'maknewpixels' is a separation 
for profiling purposes, and it takes almost as much time as the other 
parts of the function):

def compost(lowerlayer, upperlayer):

    def makenewpixels(ltable, utable, h, w, upperalphatable, 
loweralphatable):
        newpixels = Numeric.zeros((h, w, 4), 'b')
        newpixels[:,:,3] = Numeric.clip(upperalphatable + 
loweralphatable, 0, 255).astype('b')
        newpixels[:,:,0:3] = (utable + ltable).astype('b')
        return newpixels
       
    h = Numeric.size(lowerlayer, 0)
    w = Numeric.size(lowerlayer, 1)
    upperalphatable = upperlayer[:,:,3]
    loweralphatable = lowerlayer[:,:,3]
    #calculate the upper layer's portion of the composted picture
    utable = Numeric.swapaxes(Numeric.swapaxes(upperlayer[:,:,0:3], 0, 
2) * upperalphatable / 255, 2, 0)
    #calculate the lower layer's portion of the composted picture
    ltable = Numeric.swapaxes(Numeric.swapaxes(lowerlayer[:,:,0:3], 0, 
2) * (255 - upperalphatable) / 255, 2, 0)
    return makenewpixels(ltable, utable, h, w, upperalphatable, 
loweralphatable)

    Thanks,
          Alex Schultz




More information about the Numpy-discussion mailing list