I need to for my pixel art tool that I'm making to help myself make games.

  • 1
    \$\begingroup\$ Do you mean the a certain part of the entire screen, or just a portion of the window in which your PyGame game is running? \$\endgroup\$
    – user1430
    Mar 17 '16 at 1:40
  • 1
    \$\begingroup\$ A portion of what is being displayed. eg: starting at 0,0 and ending at 100,100 when the screen size is 500x500. \$\endgroup\$ Mar 17 '16 at 1:43
  • \$\begingroup\$ Do you mean like the rectangular marquee tool in painting programs? Using the term "screenshot" is throwing people off, because it's a term reserved for capturing arbitrary areas of the complete display, whether it's in the same program or not. \$\endgroup\$ Mar 17 '16 at 4:15

You can do this using pygame.image.save(Surface, filename), which you can read more about here.

Below is a simple function that will save a portion of the display as an image.

def Capture(display,name,pos,size): # (pygame Surface, String, tuple, tuple)
    image = pygame.Surface(size)  # Create image surface
    image.blit(display,(0,0),(pos,size))  # Blit portion of the display to the image
    pygame.image.save(image,name)  # Save the image to the disk

What this function does is created a pygame surface named image. Then the area (pos,size) is blitted to image at its origin. Finally, pygame.image.save(Surface, filename) will be called and save image to the disk.

For example, if we want to save a 100x100 image named "Capture.png" at the pos 50x50 on the display, name would equal "Capture.png", pos would equal (50,50), and size would equal (100,100), and the function call would look as such:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.