Raspberrypi python从内存显示图像

2024-04-18 18:32:11 发布

您现在位置:Python中文网/ 问答频道 /正文

任务如下:从Picamera的外部事件(GPIO)捕获帧并显示在屏幕上。在

使用方法:

  1. 皮尔图像.show()-制作临时文件并使用外部查看器,我需要内存。

  2. 开放式CV简历imshow()-在多个顺序事件后冻结带有图像的窗口。我玩了很久它还是冻着的。

  3. 带有图像的Gdk窗口在几个事件后也会冻结,但如果GLib计时器事件调用更新,则不会冻结,但不会冻结GPIO的处理程序。

你能提出什么方法来完成这项任务吗?在


Tags: 方法内存图像处理程序gpio屏幕顺序show
1条回答
网友
1楼 · 发布于 2024-04-18 18:32:11

创建一个小的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脚本来更新图像。在


初始脚本可能如下所示:

^{pr2}$

那么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)

enter image description here

本质上,您的应用程序所要做的就是:

 while true:
   ... generate new image ...
   im.save(tmpnam)
   os.rename(tmpnam,filename)

如果从feh命令中删除 title "monitor",您将看到它只是在迭代一个由两个图像组成的列表,而这两个图像恰好是同一个图像。图像每隔0.5秒交换一次,不闪烁。如果你愿意,你可以改变。如果由于某种原因您不想让它在这两者之间不断交替,那么您可以使延迟大得多,并发送一个SIGUSR1fehos.kill(pid, signal.SIGUSR1)),使其在更新图像时进行切换。在

相关问题 更多 >