我做了一个屏幕放大程序,这样我就可以用我的视力看到屏幕,并做了这个。
import pyautogui
import pygame
import PIL
from PIL import Image
pygame.init()
LocationLeft = 50
LocationTop = 50
LocationWidth = 100
LocationHeight = 100
Magnification = 3
gameDisplay = pygame.display.set_mode((LocationWidth * Magnification , LocationHeight * Magnification ))
crashed = False
ImageFileName="ScreenLarger.png"
try:
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
x, y = pyautogui.position()
LocationLeft = x - 25
LocationTop = y - 25
im = pyautogui.screenshot(imageFilename=ImageFileName ,region=(LocationLeft,LocationTop, LocationWidth, LocationHeight))
img = Image.open(ImageFileName)
img = img.resize((LocationWidth * Magnification, LocationHeight * Magnification))
img.save(ImageFileName)
theimg = pygame.image.load(ImageFileName)
gameDisplay.blit(theimg,(0,0))
pygame.display.update()
except KeyboardInterrupt:
print('\n')它工作得很好,你可以使用它,问题是它与硬盘每次迭代4次,我认为这不是最好的实践,因为没有固态驱动器的话,它会增加驱动器的磨损和损坏。那么,我如何将图像保存在它所属的ram中?
发布于 2019-08-03 19:50:56
那你为什么要把它保存到文件里呢?
只有当您将文件名传递到应该保存的文件名时,pyautogui.screenshot才会保存到文件中,否则,它将返回PIL.Image。只是不要要求它将其保存到文件中。
派克有一个函数pygame.image.fromstring(string, size, format, flipped=False) -> Surface。
这样,您就可以获取屏幕截图,并将其转换为吡咯面:
screenshot = pyautogui.screenshot(region=(LocationLeft,LocationTop, LocationWidth, LocationHeight))
image = pygame.image.fromstring(screenshot.tobytes(), screenshot.size, screenshot.mode)然后直接将它放到屏幕上,而不需要将其保存到文件中。
.tobytes()返回图像的原始字节,这就是pygame所指的"string",而bytes是一个全局存在于python库中的函数,因此,在处理二进制数据时,单词“字节”不能真正用作变量名而不隐藏该函数(仅在Python中)-- string是指bytes。
.size返回图像的维数,这也是吡游函数所期望的。
.mode返回图像的格式,游戏还需要从原始字节重构真实的图像。
如果您不需要翻转图像(这是您要搞清楚的),您应该使用pygame.image.frombuffer而不是pygame.image.fromstring,这将更快,因为它不会复制任何数据,并将使用PIL图像字节直接。
https://stackoverflow.com/questions/57341567
复制相似问题