Posts Tagged ‘ surface

Pygame Window Info

(This post has its own page here)

While working on my ‘Tablet Pressure‘ code, I realized I needed a way to be able to query the position of the active Pygame screen relative to the desktop. There didn’t seem to be any built-in way in Pygame to do this.

After doing some searching, I ran across Python‘s ctypes bindings. These let you tap into Windows (the OS) ‘window info’. Based on fiddling with that code, I’ve come up with a Python class that lets you query a few things:

  • The extents of the active Pygame window relative to the desktop: top, bottom, left, and right coordinates.
  • The extents of the active Pygame screen inside that window, relative to the desktop: top, bottom, left, and right coordinates.
  • The width of the window border edges, and the title bar.

Download the source here:

It comes with its own unit test: Executing from a shell will give you a floating resizable Pygame window, and a printout like this in the shell, showing its functionality:

Here’s the unit test code, showcasing its functionality:

# Unit Testing:
if __name__ == "__main__":
    """Unit Test, will print window, screen and border thicknesses to the shell."""

    pygame.init()

    # Must set this env var for PygameWindowInfo to work!
    os.environ['SDL_VIDEO_WINDOW_POS'] = "128,128"

    screen = pygame.display.set_mode((256, 256), pygame.RESIZABLE)
    pygame.display.set_caption("PygameWindowInfo v%s"%__version__)

    # Create our object:
    winInfo = PygameWindowInfo()

    # Main loop:
    looping = True
    while looping:
        # ***Must update our winInfo object during every loop*** to accurately
        #  track window and display screen position
        winInfo.update()

        screen.fill(pygame.Color("black"))

        events = pygame.event.get()
        for event in events:
            if event.type == pygame.QUIT:
                looping = False
            if event.type == pygame.VIDEORESIZE:
                WIDTH, HEIGHT = event.size
                screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.RESIZABLE)

        winPos = winInfo.getWindowPosition()
        screenPos = winInfo.getScreenPosition()

        # Print interesting data!
        print "Window -",
        for key in winPos.keys():
            print "%s: %s"%(key, winPos[key]),
        print " |  Screen -",
        for key in screenPos.keys():
            print "%s: %s"%(key, screenPos[key]),
        print " |  Title thickness: %s"%winInfo.titleThickness,
        print " |  Border thickness: %s"%winInfo.borderThickness,
        print "\n",

        pygame.display.flip()

    pygame.quit()

Enjoy!

How can I blur a PyGame Surface?

From over on my PyGame Wiki…

There doesn’t seem to be a built in way in PyGame to ‘blur’ a surface, but you can fake it bay scaling the surface twice: Scale it small once, then scale it back to normal size.

def blurSurf(surface, amt):
    """
    Blur the given surface by the given 'amount'.  Only values 1 and greater
    are valid.  Value 1 = no blur.
    """
    if amt < 1.0:
        raise ValueError("Arg 'amt' must be greater than 1.0, passed in value is %s"%amt)
    scale = 1.0/float(amt)
    surf_size = surface.get_size()
    scale_size = (int(surf_size[0]*scale), int(surf_size[1]*scale))
    surf = pygame.transform.smoothscale(surface, scale_size)
    surf = pygame.transform.smoothscale(surf, surf_size)
    return surf

You can also use pygame.transform.rotozoom() in place of smoothscale. However, they have different effects: smoothscale appears to blur (and push) the pixels ‘outward’ based on the center of the surface, while rotozoom blurs (and pushes) them outward based on the top left corner of the image (0,0), effectively making them translate down and to the right in the process.
http://www.pygame.org/docs/ref/transform.html#pygame.transform.smoothscale
http://www.pygame.org/docs/ref/transform.html#pygame.transform.rotozoom

Other methods I haven’t investigated would involve running a convolve routine on the surface pixels.  As is, I’m not sure how could the performance would be though.

I’d be interested to know what methods others have come up with to solve this problem.  My searching hasn’t come up with much.