【问题标题】:Capturing game screenshots for use by a Python script捕获游戏截图以供 Python 脚本使用
【发布时间】:2018-10-23 06:11:47
【问题描述】:

我需要为 Python 脚本(一些机器学习、AI 与视频游戏)截取游戏截图,并且我被引导到 FRAPS。 FRAPS和其他类似的软件可以随心所欲地截取屏幕截图——但据我所知,我需要知道要命名的文件是什么,更重要的是,我需要将其与其他屏幕截图区分开来,或者删除这些屏幕截图一旦我完成了他们。 FRAPS 的命名系统(以及 Bandicam 的)要求我知道拍摄时间,精确到千分之一秒(据我所知)。该程序将在动态的、时间敏感的情况下运行,所以我只想有一个设置命名方案,就像我在 Greenshot 中可以做的那样(也就是说,除非很容易找到正在保存的文件,否则我不感兴趣在游戏期间的卷积文件搜索中,否则我可能会对此持开放态度。拍摄 FRAPS 游戏视频已经滞后到足以破坏游戏中脚本同步的功能,并出现随机错误)。我既关心游戏期间的一般系统性能,也关心算法响应能力。毕竟,我已经知道下一步是在屏幕上找到图像或其中的一部分,这将很费力。

我会对任何可以将图像文件(我一直计划使用 .png,但任何无损文件类型都可以)放入程序目录的软件、模块或代码感到满意,作为图片。将它保存到剪贴板也可以,因为它可以从那里保存和操作。无论如何,我对大多数这些屏幕截图都没有长期使用。如果它可以只截取屏幕的一部分,那也更好。

我怀疑这是否重要,但这里有一些关于我的程序外观的精简伪代码。我真的不需要任何故障排除。

    import os,sys,subprocess,mss,mss.tools,time as sleepy,keyboard,sys,pyscreenshot as Grabby
    from random import randint as spinny
    from PIL import Image
    from pyautogui import * # Just to help with what I've been using, a lot of these aren't currently in use
    class nameOfGame:
        magicNumbers = {'whereStuffIs': (500,500),'generalDelay':0.05,'Hotkey':'f10','fileName':'lmno.png'}
        def __init__(self,magicNumbers):
            os.startfile(gameLocation)

        def Play(self):
            if(self.goAhead()):

moveTo(self.magicNumbers['whereStuffIs'])                
click(self.magicNumbers['whereStuffIs']) #How do I format this correctly on StackOverflow?

        def ScreenShot(self):
            if(softWareScreenSshot):# Outside software is getting the screenshot
                press(self.magicNumbers['Hotkey'])
            else: # Python module or other method...?
                PseudoModule.takeAScreenshot()
            load(self.magicNumbers['fileName'])

        def goAhead(self):
            return True # This is a placeholder for AI.

人们通常如何获得这种人工智能?如果可能的话,我不想为此使用任何更高级别的编码,因为我主要将此作为个人挑战。到目前为止,我已经学到了很多东西,我不想让程序为我做所有事情,只是截图。不过,为此,如果有办法解决这个问题,我不介意把脚本弄得一团糟。但是,从我所读到的内容来看,我认为我需要在游戏运行时进入游戏来执行此操作,这可能会也可能不会实用。提前致谢!

【问题讨论】:

    标签: python python-3.x artificial-intelligence screenshot


    【解决方案1】:

    我看到这个问题已经很老了,但仍然没有答案。 您可以使用 python 库mss 来抓取屏幕。我可以看到你已经导入了它,但没有使用它。

    In their documentation is already a sample:

    import time
    import cv2
    import mss
    import numpy
    
    with mss.mss() as sct:
      # Part of the screen to capture
      monitor = {"top": 40, "left": 0, "width": 800, "height": 640}
    
      while "Screen capturing":
          last_time = time.time()
          # Get raw pixels from the screen, save it to a Numpy array
          img = numpy.array(sct.grab(monitor))
    
          # Display the picture
          cv2.imshow("OpenCV/Numpy normal", img)
    
          # Display the picture in grayscale
          # cv2.imshow('OpenCV/Numpy grayscale',
          #            cv2.cvtColor(img, cv2.COLOR_BGRA2GRAY))
    
          print("fps: {}".format(1 / (time.time() - last_time)))
    
          # Press "q" to quit
          if cv2.waitKey(25) & 0xFF == ord("q"):
              cv2.destroyAllWindows()
              break
    

    如我所见,您还找到了PyAutoGui。它有一个locate function 来查找屏幕上的区域,但是速度很慢。他们写在文档中

    在 1920 x 1080 屏幕上,定位函数调用大约需要 1 或 2 秒。

    我认为将屏幕截图缩小到感兴趣的区域可以加快速度。

    如果您需要更多信息,那里有很多教程。例如,这是using Python to play GTA V 的 git 存储库。请参阅相关 youtube 视频和文章的链接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-04
      • 2014-11-25
      • 1970-01-01
      • 2015-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多