[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] Crop Image with Numeric



Hi there. I'm recreating PIL's bounding box calculation in Pygame, but what I've got is slow. I'm not a Numeric expert, but if you are, maybe you can help! What can be done to the following code to speed it up? It crops a 32 bit picture based on the alpha channel transparency, and assumes your picture will never be larger than 10000x10000.

def BoundingBox(pic):
    if pic.get_bitsize() != 32:
        return [0, 0, pic.get_width(), pic.get_height()]
    left = 10000
    top = 10000
    right = 0
    bottom = 0
    alpha = pygame.surfarray.array_alpha(pic).astype(Numeric.UInt8)
    for x in range(len(alpha)):
        for y in range(len(alpha[x])):
            if alpha[x][y] > 0:
                left = min(left, x)
                top = min(top, y)
                right = max(right, x)
                bottom = max(bottom, y)

    return [left, top, right-left, bottom-top]