Search code examples
pythonpygamepngsprite

Loop to make all PNGs in a list fit their borders


I have a list of PNGs that are 120px by 80px each. But my character in the PNG is really smaller, only occupying the middle. Is there a way to loop through all the elements and make it so that the border of these PNGs exactly fit the image? Kind of like a really tight picture frame.


Solution

  • I'll assume that the list actually contains pygame.Surfaces, so what you can do is use pygame.Surface.get_bounding_rect to get the smallest rectangle required to fit all the data and then subsurface all the surfaces in that list, so basically you'd have this:

    image_list = [...]
    image_list = [image.subsurface(image.get_bounding_rect()) for image in image_list]