Pillow image.show() instance

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

Hi , this code show all screenshots on window. I want to use pillow image.show() to organize screenshots like screen recorder. How I will create stable window to show current screenshot image and when new screenshot image comes it delete old one? I made it with update label in Tkinter but it is so slow and have delay.

1 Answers

As you appear to dislike my solutions with OpenCV and with pygame, I thought I'd write you a third one - with feh this time:

#!/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')

It does around 20fps frame rate with 640x480 images on my Mac.

Related