Raspberrypi python 从内存中显示图像 [英] Raspberrypi python display image from memory

查看:19
本文介绍了Raspberrypi python 从内存中显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

the task is folowing: capture the frame from Picamera on external event (GPIO) and display it on the screen.

使用的方法:

  1. PIL image.show() - 制作临时文件并使用外部查看器,我需要从内存中获取.

  1. PIL image.show() - make temporary file and use external viewer, I need from memory.

Opencv cv.imshow() - 在几个序列事件后冻结带有图像的窗口.我玩过延迟它仍然冻结.

Opencv cv.imshow() - freeze window with image after several sequenced events. I have played with delays it still freeze.

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

UPD: Gdk Window with image also freeze after several events but it is not freeze if GLib timer event call update, but not GPIO's handler.

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

Could you suggest any method to complete this task ?

推荐答案

创建一个小巧的 RAMdisk,它既美观又快速,并且不会磨损您的 SD 卡.将图像写入该图像并使用 feh 或类似内容显示:

Create a small RAMdisk which is nice and fast and avoids wear on your SD card. Write the image to that and display it with feh or similar:

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 脚本更新您的图像.

Then start a Python script updating your images.

初始脚本可能如下所示:

The initial script might look like this:

#!/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 可能如下所示:

Then the Python script monitor.py might look like this:

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

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

In essence, all your application has to do is:

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

如果您从 feh 命令中删除 --title "monitor",您将看到它只是遍历 2 个图像的列表,两者都发生在是同一个形象.图像每 0.5 秒交换一次,不闪烁.如果你愿意,你可以改变它.如果由于某种原因你不希望它在两者之间连续交替,你可以使延迟更大,然后发送一个 SIGUSR1feh (os.kill(pid, signal.SIGUSR1)) 使其在更新图像时切换.

If you remove the --title "monitor" from the feh command, you will see it is just iterating through a list of 2 images, that both happen to be the same image. The images are swapped every 0.5 seconds, without flashing. You can change that if you want. If you don't want it continuously alternate between the two for some reason you can make the delay much bigger, and send a SIGUSR1 to feh (os.kill(pid, signal.SIGUSR1)) to make it switch whenever you update the image.

这篇关于Raspberrypi python 从内存中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆