Python3 PIL 创建图像时出错:无法识别图像文件

问题描述 投票:0回答:1

代码

from io import StringIO
import subprocess
import os
import time
from datetime import datetime
from PIL import Image
import subprocess


def captureTestImage(settings, width, height):
    command = "libcamera-still --width {} --height {} --autofocus-on-capture 1 -q 75 --tuning-file /usr/share/libcamera/ipa/rpi/vc4/imx708_wide.json -o DAY-AF1-q75--tuningfile-imx708_wide-06-21-2024-16-15-20.jpg".format(width, height)
    subprocess.run(command[0],shell=True,executable="/usr/bin/bash")
    imageData = StringIO()
    imageData.write(subprocess.check_output(command, shell=True))
    imageData.seek(0)
    im = Image.open(imageData)
    buffer = im.load()
    imageData.close()
    return im, buffer

实际接收错误:

Still capture image received

    im = Image.open(imageData)
  File "/home/user/.local/lib/python3.9/site-packages/PIL/Image.py", line 3339, in open
    raise UnidentifiedImageError(msg)
PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7f9df4db80>

这里出了什么问题?

这是创建一个实际的图像文件,但脚本返回此错误 - 为什么?我该如何修复?谢谢你

python-3.x python-imaging-library
1个回答
0
投票

我不明白你为什么把事情搞得这么复杂。我认为下面的代码应该足以满足您想要做的事情 - 如果没有,请说出它缺少哪些功能,以便我可以更好地提供帮助:

#!/usr/bin/env python3

import subprocess as sp

# Define parameters for libcamera command
width, height = 640, 480
filename = "image.jpg"

# Flesh out libcamera command
cmd = [
   '/usr/bin/libcamera-still',
   '--width', f'{width}',
   '--height', f'{height}',
   '--autofocus-on-capture', '1',
   '-q', '75',
   '--tuning-file', '/usr/share/libcamera/ipa/rpi/vc4/imx708_wide.json',
   '-o', filename
   ] 

# Run libcamera command to create image.jpg
sp.run(cmd)

# Open image.jpg with Pillow
im = Image.open(filename)
© www.soinside.com 2019 - 2024. All rights reserved.