Your function has an error (size_f is not defined) but otherwise it's working. The function
def screenshot(obj, file_name, position, size):
img = pygame.Surface(size)
img.blit(obj, (0, 0), (position, size))
pygame.image.save(img, file_name)
takes the the topleft position and the size of the rectangle. You said yourself that the topleft red dot is at (20, 100)
, so the position argument should be at (20, 100)
(that's why your second attempt fixes the padding to the left and top).
The bottomright point is determined by the size of the rectangle. We're trying to get from (20, 100)
to (480, 480)
, so some quick math will give us the size:
width = 480 - 20 = 460
height = 480 - 100 = 380
Your function should give the right result if you do like this:
screenshot(game, "test1.png", (20, 100), (460, 380))
You could do this if you want to change the function to take two positions instead:
def screenshot(obj, file_name, topleft, bottomright):
size = bottomright[0] - topleft[0], bottomright[1] - topleft[1]
img = pygame.Surface(size)
img.blit(obj, (0, 0), (topleft, size))
pygame.image.save(img, file_name)
Here's an example demonstrating both functions:
import pygame
pygame.init()
screen = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()
def screenshot(obj, file_name, position, size):
img = pygame.Surface(size)
img.blit(obj, (0, 0), (position, size))
pygame.image.save(img, file_name)
def screenshot2(obj, file_name, topleft, bottomright):
size = bottomright[0] - topleft[0], bottomright[1] - topleft[1]
img = pygame.Surface(size)
img.blit(obj, (0, 0), (topleft, size))
pygame.image.save(img, file_name)
a = pygame.Surface((10, 10))
a.fill((255, 0, 0))
while 1:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_s:
screenshot(screen, "test1.png", (20, 100), (460, 380))
screenshot2(screen, "test2.png", (20, 100), (480, 480))
print("PRINTED")
elif event.type == pygame.QUIT:
quit()
screen.blit(a, (20, 100))
screen.blit(a, (480, 480))
pygame.display.update()