Search code examples
pygamepygame-surface

Does a surface in pygame contain more than it can show (load a 40x40 picture on a 20x20 surface)?


If I load a picture of 40x40 pixels on a surface of 20x20 pixels. Does the surface contain all 40x40 pixels even tho it only shows 20x20?


Solution

  • Assuming you are using a code like this one:

    src_surface = pygame.image.load('foo.png')  # size: 40x40px
    
    dest_surf = pygame.Surface((20, 20))
    dest_surf.blit(src_surf)
    

    Your image will be blit like that:

    In red is the image you want to blit, in black the Surface where the image will be blit.

    To avoid losing the part of the source surface not in the destination surface, you can use pygame.transform.scale like that for example:

    src_surf = pygame.image.load('foo.png')
    
    width, height = src_surf.get_size()
    width = int(width / 2)
    height = int(height / 2)
    
    dest_surf = pygame.transform.scale(src_surf, (width, height))