创建一个小巧的 RAMdisk,它既美观又快速,可避免 SD 卡磨损。将图像写入其中并使用feh 或类似名称显示:
sudo mkdir -p /media/ramdisk
sudo mount -t tmpfs -o size=4M tmpfs /media/ramdisk
df /media/ramdisk
Filesystem 1K-blocks Used Available Use% Mounted on
tmpfs 4096 0 4096 0% /media/ramdisk
然后启动一个 Python 脚本来更新您的图像。
初始脚本可能如下所示:
#!/bin/bash
# Create a couple of useful variables
MNTPNT="/media/ramdisk"
IMGNAM="$MNTPNT/image.ppm"
# Make ramdisk and mount
sudo mkdir -p /media/ramdisk 2> /dev/null
sudo mount -t tmpfs -o size=4M tmpfs /media/ramdisk 2> /dev/null
# Create initial empty image to display with ImageMagick or any other means
convert -size 800x600 xc:black -depth 8 "$IMGNAM"
# Get 'feh' started in "Slideshow mode" in the background
feh --title "Monitor" -D 0.5 "$IMGNAM" "$IMGNAM" &
# Now start Python script with image name
./monitor.py "$IMGNAM"
那么 Python 脚本 monitor.py 可能如下所示:
#!/usr/bin/python3
import os, sys, time
from PIL import Image, ImageDraw
from random import randint
# Pick up parameters
filename = sys.argv[1]
# Create initial image and drawing handle
w, h = 800, 600
im = Image.new("RGB",(w,h),color=(0,0,0))
draw = ImageDraw.Draw(im)
# Create name of temp image in same directory
tmpnam = os.path.join(os.path.dirname(filename), "tmp.ppm")
# Now loop, updating the image 100 times
for i in range(100):
# Select random top-left and bottom-right corners for image
x0 = randint(0,w)
y0 = randint(0,h)
x1 = x0 + randint(10,250)
y1 = y0 + randint(10,250)
# Select a random colour and draw a rectangle
fill = (randint(0,255), randint(0,255), randint(0,255))
draw.rectangle([(x0,y0),(x1,y1)], fill=fill)
# Save image to a temporary file, then rename in one immutable operation...
# ... so that 'feh' cannot read a half-saved image
im.save(tmpnam)
os.rename(tmpnam,filename)
time.sleep(0.3)
本质上,您的应用程序所要做的就是:
while true:
... generate new image ...
im.save(tmpnam)
os.rename(tmpnam,filename)
如果您从feh 命令中删除--title "monitor",您将看到它只是在遍历包含2 个图像的列表,而这两个图像恰好是同一个图像。图像每 0.5 秒交换一次,不闪烁。如果你愿意,你可以改变它。如果由于某种原因您不希望它在两者之间连续交替,您可以使延迟更大,并发送SIGUSR1 到feh (os.kill(pid, signal.SIGUSR1)) 以使其在更新图像时切换。