简体   繁体   English

python在ram中保存图像而不保存到硬盘?

[英]python keeping a image in ram not saving to hard drive?

I made a screen magnification program, so I can see the Screen with my eyesight and have made this. 我做了一个屏幕放大程序,所以我可以用我的视力看到屏幕,并做了这个。

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')

It works great and you can use it, the problem is it interacts with the hard drive 4 times each iteration, I don't think this is best practices because those without a solid state drive it will increase wear and damage to the drive. 它运行良好,您可以使用它,问题是它每次迭代与硬盘驱动器交互4次,我不认为这是最佳实践,因为没有固态驱动器的那些会增加驱动器的磨损和损坏。 so how do I keep the image in ram where it belongs? 那么如何将图像保存在ram所属的位置?

Why do you save it to a file then? 为什么要将它保存到文件中呢?

pyautogui.screenshot saves to a file only if you pass a filename where it should be saved, otherwise, it returns PIL.Image . pyautogui.screenshot仅在您传递应保存的文件名时保存到文件,否则返回PIL.Image Simply do not ask it to save it to a file. 只是不要求它将其保存到文件中。

Pygame has a function pygame.image.fromstring(string, size, format, flipped=False) -> Surface . Pygame有一个函数pygame.image.fromstring(string, size, format, flipped=False) -> Surface

This way, you can take a screenshot and convert it to pygame surface: 这样,您可以截取屏幕截图并将其转换为pygame表面:

screenshot = pyautogui.screenshot(region=(LocationLeft,LocationTop, LocationWidth, LocationHeight))
image = pygame.image.fromstring(screenshot.tobytes(), screenshot.size, screenshot.mode)

Then blit it directly to screen without the need to save it to a file. 然后直接将其blit到屏幕,而无需将其保存到文件中。

.tobytes() returns raw bytes of an image, this is what pygame refers to as "string", while not same thing, bytes is a function that exists in python library, globally, and thus word "bytes" cannot really be used as a variable name without shadowing that function, more often than not (only in Python!), when dealing with binary data - string means bytes . .tobytes()返回一个图像的原始字节,这就是pygame所说的“字符串”,而不是同一个东西, bytes是一个存在于python库中的函数,全局,因此单词“bytes”不能真正用作在处理二进制数据时,变量名称没有遮蔽该函数(通常只在Python中) - string 表示 bytes

.size returns dimensions of an image, which is what pygame function expects. .size返回图像的尺寸,这是pygame函数所期望的。

.mode returns format of an image, which pygame also needs to reconstruct real image from raw bytes. .mode返回图像的格式,pygame还需要从原始字节重建真实图像。

In case you do not need to flip the image (that's for you to figure out), you should use pygame.image.frombuffer instead of pygame.image.fromstring , that will be a lot faster as it won't copy any data and will use PIL image bytes directly. 如果您不需要翻转图像(这是为了让您弄清楚),您应该使用pygame.image.frombuffer而不是pygame.image.fromstring ,这将更快,因为它不会复制任何数据和将直接使用PIL图像字节。

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

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