如何在Pygame中获取屏幕某些部分的屏幕截图 [英] How to take screenshot of certain part of screen in Pygame
问题描述
有没有办法让我的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.image.save(screen,"screenshot.jpg")
但这将包括图像中的整个屏幕。
But that will include the entire screen in the image.
有没有办法让我对pygame窗口的右半部分截屏?
Is there a way I can take a screenshot of the right half of my pygame window?
也许通过某种方式更改其包含的区域?我已经在Google上进行了搜索,但找不到任何我在想的东西,也许我可以使用PIL对其进行裁剪,但这似乎需要大量工作。
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.
如果不可能,谁能告诉我最简单的方法来裁剪整个屏幕的图片?
If it's not possible, can anyone tell me the easiest way for me to crop the picture of the whole screen?
推荐答案
如果您始终希望屏幕截图位于屏幕的同一部分,则可以使用 subsurface
。
http://www.pygame.org/docs /ref/surface.html#pygame.Surface.subsurface
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")
这篇关于如何在Pygame中获取屏幕某些部分的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!