简体   繁体   English

树莓派相机-如何在预览窗口中绘制PIL图像?

[英]Raspberry pi camera - how do you draw a PIL Image to the preview window?

The current documentation for rasberry pi says to draw a PIL image onto the camera capture, you do the following: 当前有关rasberry pi的文档说,要在摄像机捕获的图像上绘制PIL图像,请执行以下操作:

import picamera
from PIL import Image
from time import sleep

with picamera.PiCamera() as camera:
    camera.resolution = (1280, 720)
    camera.framerate = 24
    camera.start_preview()

    # Load the arbitrarily sized image
    img = Image.open('overlay.png')
    # Create an image padded to the required size with
    # mode 'RGB'
    pad = Image.new('RGB', (
        ((img.size[0] + 31) // 32) * 32,
        ((img.size[1] + 15) // 16) * 16,
        ))
    # Paste the original image into the padded one
    pad.paste(img, (0, 0))

    # Add the overlay with the padded image as the source,
    # but the original image's dimensions
    o = camera.add_overlay(pad.tostring(), size=img.size)
    # By default, the overlay is in layer 0, beneath the
    # preview (which defaults to layer 2). Here we make
    # the new overlay semi-transparent, then move it above
    # the preview
    o.alpha = 128
    o.layer = 3

    # Wait indefinitely until the user terminates the script
    while True:
        sleep(1)

However, Image.tostring() throws an error with "tostring() has been removed. Please call tobytes() instead" 但是,Image.tostring()抛出错误,“ tostring()已被删除。请调用tobytes()代替”

What's the correct way to do this given that tostring is deprecated? 鉴于不建议使用tostring,什么是正确的方法?

The answer is in your question: 答案在您的问题中:

However, Image.tostring() throws an error with "tostring() has been removed. Please call tobytes() instead" 但是,Image.tostring()抛出错误,“ tostring()已被删除。请调用tobytes()代替”

Simply change: 只需更改:

o = camera.add_overlay(pad.tostring(), size=img.size)

to: 至:

o = camera.add_overlay(pad.tobytes(), size=img.size)

The picamera documentation has already been updated to address this: https://picamera.readthedocs.io/en/release-1.13/recipes1.html#overlaying-images-on-the-preview picamera文档已更新为解决此问题: https ://picamera.readthedocs.io/en/release-1.13/recipes1.html#overlaying-images-on-the-preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM