Search code examples
pythonpython-3.xpygamescreenshotscreen-capture

How to take screenshot of entire display pygame


I am creating a painter software and want the user to be able to save their image after they have finished creating it, so I tried pygame.image.save(pygame.display.get_surface(), "/home/user/screenshot.png"). I made a quick drawing and pressed the key I set which would save the image. I look at the image, and it's only saved the blank display surface, not the pygame.draw.rect()s of the actual drawing. I looked at the following links: How to capture pygame screen? https://gamedev.stackexchange.com/questions/118372/how-can-i-take-a-screenshot-of-a-certain-part-of-the-screen-in-pygame Can python get the screen shot of a specific window? and much more. How would I take a screenshot of the entire display screen along with the drawing? This is my mainloop:

running = True
while running:
    updateWindow() # Updates window
    clearWindow() # Clears window
    checkEvents() # Checks events
    redrawItems() # Redraws your drawing
    pygame.event.pump()
pygame.display.quit()
pygame.quit()

Solution

  • Try pygame.Surface.copy on the display surface. See docs here.

    So if display is your screen, then:

    screencopy = display.copy()
    

    should get you a copy of the display image in screencopy. Remember because of double buffering it will give you a copy of what you would see on the screen if you did an display.update() right then, which might be different that what is showing on the screen if you have done things that have not yet been pushed to the screen yet by an update().