简体   繁体   中英

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. so how do I keep the image in ram where it belongs?

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 . Simply do not ask it to save it to a file.

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

This way, you can take a screenshot and convert it to pygame surface:

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.

.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 .

.size returns dimensions of an image, which is what pygame function expects.

.mode returns format of an image, which pygame also needs to reconstruct real image from raw bytes.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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