I need to for my pixel art tool that I'm making to help myself make games.
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: