枕头 image.show() 实例

Pillow image.show() instance

    def screen_shot():
       pass
    While 1:
       img= screen_shot()
       #getting image in temp
       img.show()
       sleep(1)

您好, 此代码显示 window 上的所有屏幕截图。我想使用 pillow image.show() 像屏幕录像机一样整理屏幕截图。我将如何创建稳定的 window 来显示当前的屏幕截图图像,当出现新的屏幕截图图像时它会删除旧的?我在 Tkinter 中用更新标签做了它,但它太慢了而且有延迟。

由于您似乎不喜欢我使用 and with 的解决方案,我想我会给您写第三个 - 这次使用 feh

#!/usr/bin/env python3

import pexpect
import random
import time
from PIL import Image

# I had to ping-pong between two images to get it smooth and error-free
filenames = [ '/tmp/PIL-0.jpg', '/tmp/PIL-1.jpg']
width, height = 640, 480
im = Image.new('RGB', (width,height), (255,0,0))
im.save(filenames[0])
im.save(filenames[1])

index = 1
viewer = pexpect.spawn(f'feh --title "Image Viewer" -- {filenames[0]} {filenames[1]}')
for i in range(100):
    # Choose a random colour
    colour = tuple(random.sample(range(0, 256), 3))
    print(f'Frame: {i}, colour: {colour}')
    # Fill image with random colour
    im.paste(colour, [0,0,width,height])
    im.save(filenames[index])
    # Tell feh to display next (other) image
    viewer.send('n')
    index = 1 - index

# Tell feh to exit
viewer.send('q')

它在我的 Mac.

上以 640x480 的图像进行大约 20fps 的帧速率