PyGame - Take a screenshot of a certain part of the display
<p>How can I take a screenshot of a certain part of the pygame screen?</p>

<p>For example, this is my screen</p>

<p><a href="http://i.stack.imgur.com/VyEEY.png" rel="nofollow"><img src="http://i.stack.imgur.com/VyEEY.png" alt="screen"></a></p>

<p>and I want to take a screenshot of the area between the top-left dot, and the bottom-right dot (the coordinate are (20, 100), (480, 480), and the whole display is 500x50).</p>

<p>This is my best result so far (but it's still not good):</p>

<pre><code>def screenshot(obj, file_name, position, size):
    img = pygame.Surface(size_f)
    img.blit(obj, (0, 0), (position, size))
    pygame.image.save(img, file_name)
...
...
...
screenshot(game, "test1.png", (0, 100), (500, 400))
</code></pre>

<p><a href="http://i.stack.imgur.com/82uKP.png" rel="nofollow"><img src="http://i.stack.imgur.com/82uKP.png" alt="screen"></a></p>

<p>I added the black border to show the image border, because the image is white.</p>

<p>---------- EDIT ----------</p>

<p>with this function call</p>

<pre><code>screenshot(game, "test1.png", (20, 100), (480, 400))
</code></pre>

<p>i'm getting a better result - now both the top and the left sides are ok, (because the top-left is the point where the x and y starts) but still - both the right and the bottom sides aren't good</p>

<p>i.imgur.com/prH2WOB.png</p>

<hr>

<p>Your function has an error (size_f is not defined) but otherwise it's working. The function</p>

<pre><code>def screenshot(obj, file_name, position, size):
    img = pygame.Surface(size)
    img.blit(obj, (0, 0), (position, size))
    pygame.image.save(img, file_name)
</code></pre>

<p>takes the the topleft position and the size of the rectangle. You said yourself that the topleft red dot is at <code>(20, 100)</code>, so the <em>position</em> argument should be at <code>(20, 100)</code> (that's why your second attempt fixes the padding to the left and top).</p>

<p>The bottomright point is determined by the size of the rectangle. We're trying to get from <code>(20, 100)</code>to <code>(480, 480)</code>, so some quick math will give us the size:</p>

<pre><code>width = 480 - 20 = 460
height = 480 - 100 = 380
</code></pre>

<p>Your function should give the right result if you do like this:</p>

<pre><code>screenshot(game, "test1.png", (20, 100), (460, 380))
</code></pre>

<p>You could do this if you want to change the function to take two positions instead:</p>

<pre><code>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)
</code></pre>

<p>Here's an example demonstrating both functions:</p>

<pre><code>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()
</code></pre>

python python-3.x pygame