繁体   English   中英

如何截取整个显示器 pygame

[英]How to take screenshot of entire display pygame

我正在创建一个画家软件,并希望用户在完成创建后能够保存他们的图像,所以我尝试pygame.image.save(pygame.display.get_surface(), "/home/user/screenshot.png") 我快速绘图并按下我设置的保存图像的键。 我看图片,它只保存了空白显示表面,而不是实际绘图的pygame.draw.rect() s。 我查看了以下链接: 如何捕获 pygame 屏幕? https://gamedev.stackexchange.com/questions/118372/how-can-i-take-a-screenshot-of-a-certain-part-of-the-screen-in-pygame 可以 python 获取屏幕截图一个特定的 window? 以及更多。 我将如何截取整个显示屏的屏幕截图以及绘图? 这是我的主循环:

running = True
while running:
    updateWindow() # Updates window
    clearWindow() # Clears window
    checkEvents() # Checks events
    redrawItems() # Redraws your drawing
    pygame.event.pump()
pygame.display.quit()
pygame.quit()

在显示表面上尝试pygame.Surface.copy 请参阅此处的文档。

因此,如果display是您的屏幕,那么:

screencopy = display.copy()

应该在screencopy中为您提供显示图像的副本。 请记住,由于双缓冲,如果您当时执行了display.update() ,它将为您提供屏幕上看到的内容的副本,这可能与您完成未完成的操作时屏幕上显示的内容不同尚未被update()推送到屏幕上。

您可以使用 pygame.image.save(Surface, filename) 来执行此操作,您可以在此处阅读更多信息

下面是一个简单的 function,它将显示的一部分保存为图像。

def Capture(display,name,pos,size): # (pygame Surface, String, tuple, tuple)
    image = pygame.Surface(size)  # Create image surface
    image.blit(display,(0,0),(pos,size))  # Blit portion of the display to the image
    pygame.image.save(image,name)  # Save the image to the disk**

这个 function 所做的是创建一个名为 image 的 pygame 表面。 然后区域 (pos,size) 在其原点被 blitted 到图像。 最后,将调用 pygame.image.save(Surface, filename) 并将图像保存到磁盘。

例如,如果我们想在显示器的 50x50 位置保存一个名为“Capture.png”的 100x100 图像,名称将等于“Capture.png”,位置等于 (50,50),大小等于 (100,100) ,并且 function 调用看起来像这样:

Capture(display,"Capture.png",(50,50),(100,100))

暂无
暂无

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

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