简体   繁体   English

如何在 Pygame 中截取屏幕的某些部分

[英]How to take screenshot of certain part of screen in Pygame

Is there a way I can take a screenshot of the right half of my pygame window?有没有办法截取我的 pygame 窗口右半部分的屏幕截图?

I'm making a game using pygame and I need to take a snapshot of the screen but not the whole screen, just the right half.我正在使用 pygame 制作游戏,我需要拍摄屏幕快照而不是整个屏幕,只是右半部分。

I know of:我知道:

pygame.image.save(screen,"screenshot.jpg")

But that will include the entire screen in the image.但这将包括图像中的整个屏幕。

Is there a way I can take a screenshot of the right half of my pygame window?有没有办法截取我的 pygame 窗口右半部分的屏幕截图?

Maybe by changing the area that it includes somehow?也许通过以某种方式改变它包含的区域? I've googled it but couldn't find anything I was thinking maybe I could use PIL to crop it, but that seems like a lot of additional work.我用谷歌搜索了它但找不到任何我想也许我可以使用 PIL 来裁剪它的东西,但这似乎是很多额外的工作。

If it's not possible, can anyone tell me the easiest way for me to crop the picture of the whole screen?如果不可能,谁能告诉我裁剪整个屏幕图片的最简单方法?

If you always want the screenshot to be of the same portion of the screen, you could use the subsurface .如果您始终希望屏幕截图位于屏幕的同一部分,则可以使用subsurface http://www.pygame.org/docs/ref/surface.html#pygame.Surface.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.在这种情况下, subsurface会很好subsurface工作,因为对父表面(在这种情况下为screen )的任何更改也将应用于次表面。

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.如果您希望能够指定屏幕的任意部分来截取屏幕截图(因此,每次都不是相同的矩形),那么最好创建一个新表面,将屏幕的所需部分 blit 到该部分面,然后保存。

rect = pygame.Rect(25, 25, 100, 50)
screenshot = pygame.Surface(100, 50)
screenshot.blit(screen, area=rect)
pygame.image.save(screenshot, "screenshot.jpg")

This didn't exactly work on my system with Python 3.7.4.这在我使用 Python 3.7.4 的系统上并不完全适用。 Here is a version which worked:这是一个有效的版本:

rect = pygame.Rect(25, 25, 100, 50)
sub = screen.subsurface(rect)
screenshot = pygame.Surface((100, 50))
screenshot.blit(sub, (0,0))
pygame.image.save(screenshot, "screenshot.jpg")
import pygame
import sys


screen = pygame.display.set_mode((400, 500))
clock = pygame.time.Clock()


def grab(x, y, w, h):
    "Grab a part of the screen"
    # get the dimension of the surface
    rect = pygame.Rect(x, y, w, h)
    # copy the part of the screen
    sub = screen.subsurface(rect)
    # create another surface with dimensions
    # This is done to unlock the screen surface
    screenshot = pygame.Surface((w, h))
    screenshot.blit(sub, (0, 0))
    return screenshot


def blit(part, x, y):
    screen.blit(part, (x, y))


def quit():
    pygame.quit()
    sys.exit()


def start():
    # shows half the screen
    blit(back, 0, 0)
    # and the other half copied
    sub = grab(50, 0, 75, 250)
    blit(sub, 200, 0)
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    quit()
        pygame.display.update()
        clock.tick(60)


back = pygame.image.load("img\\back.png")

start()

I would do something like:我会做这样的事情:

example = pygame.Surface(screen.get_width()/2, 0)

Then later on when you want to take the screenshot do:稍后当您想要截取屏幕截图时,请执行以下操作:

pygame.image.save(example, "example.jpg")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM