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

查看:44
本文介绍了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秒交换一次,而不闪烁.您可以根据需要进行更改.如果您由于某种原因不希望它在两者之间连续交替,您可以将延迟变得更大,然后将 SIGUSR1 发送给 feh ( 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天全站免登陆