How to take screenshot of certain part of screen in Pygame
If you always want the screenshot to be of the same portion of the screen, you could use the subsurface
.
http://www.pygame.org/docs/ref/surface.html#pygame.Surface.subsurface
rect = pygame.Rect(25, 25, 100, 50)
sub = screen.subsurface(rect)
pygame.image.save(sub, "screenshot.jpg")
The subsurface
would work well in this scenario because any changes to the parent surface (screen
in this case) will be applied to the subsurface as well.
If you want to be able to specify an arbitrary portion of the screen to take a screenshot of (so, not the same rectangle every time) then it would probably be better to create a new surface, blit the desired portion of the screen to that surface, and then save it.
rect = pygame.Rect(25, 25, 100, 50)
screenshot = pygame.Surface(100, 50)
screenshot.blit(screen, area=rect)
pygame.image.save(screenshot, "screenshot.jpg")