[Image-SIG] pattern fill

Andre Kuehne andre.kuehne at gmx.net
Mon Aug 23 18:09:37 CEST 2004


Hi

Here is a code snippet i wrote some time ago for filling a region (box)
of an image (img) with a tile (src):


def fillRegion(img, box, src):
    (left,top,right,bottom) = box
    (srcWidth, srcHeight) = src.size
    y = top
    remY = (bottom-top)
    while remY > 0:
        x = left
        remX = (right-left)
        while remX > 0:
            if remX >= srcWidth:
                if remY >= srcHeight:
                    alphaPaste(img,src,(x,y))
                    putWidth = srcWidth
                    putHeight = srcHeight
                else:
                    frac = src.crop((0, 0, srcWidth, remY))
                    alphaPaste(img,frac,(x,y))
                    putWidth = srcWidth
                    putHeight = remY
            else:
                if remY >= srcHeight:
                    frac = src.crop((0, 0, remX, srcHeight))
                    alphaPaste(img,frac,(x,y))
                    putWidth = remX
                    putHeight = srcHeight
                else:
                    frac = src.crop((0, 0, remX, remY))
                    alphaPaste(img,frac,(x,y))
                    putWidth = remX
                    putHeight = remY
            x += putWidth
            remX -= putWidth
        y += putHeight
        remY -= putHeight

def alphaPaste(dst, src, box):
    if src.mode == "RGBA":
        dst.paste(src, box, src)
    else:
        dst.paste(src, box)


If someone has a more elegant way to do this, it would be nice to see.

regards

-- 
     _   _
_/\_| |_( )_
Andre Kuehne



More information about the Image-SIG mailing list