简体   繁体   English

Python 中的 gphoto2 vs 命令行; 无法在 Python 中捕获预览

[英]gphoto2 in Python vs command line; not able to capture preview in Python

I've worked with gphoto2 (2.5.20, on a RPi) before, and have been able to do the basics from the command line:我以前使用过 gphoto2(2.5.20,在 RPi 上),并且能够从命令行完成基础操作:

/usr/bin/gphoto2 --capture-image-and-download --filename=zzz.jpg --force-overwrite /usr/bin/gphoto2 --capture-image-and-download --filename=zzz.jpg --force-overwrite

/usr/bin/gphoto2 --capture-preview --filename=zzz.jpg --force-overwrite /usr/bin/gphoto2 --capture-preview --filename=zzz.jpg --force-overwrite

The latter captures the preview image, which is a great deal smaller, and for some applications, is great;后者捕获的预览图像要小得多,并且对于某些应用程序来说很棒; you can essentially make a webcam out of a DSLR.您基本上可以用数码单反相机制作网络摄像头。

However, after installing the gphoto2 library for python, this (which should be the equivalent) fails:但是,在为 python 安装 gphoto2 库后,这(应该是等效的)失败:

import gphoto2 as gp
camera = gp.Camera()
camera.init()
file_path = camera.capture(gp.GP_OPERATION_CAPTURE_PREVIEW)

with the message "gphoto2.GPhoto2Error: [-6] Unsupported operation"带有消息“gphoto2.GPhoto2Error:[-6] 不支持的操作”

If you choose如果你选择

file_path = camera.capture(gp.GP_CAPTURE_IMAGE)

however, this works.但是,这行得通。 Any reasons why we might get a working preview from the command line, and not in the Python version?为什么我们可以从命令行获得工作预览,而不是在 Python 版本中?

gp.__version__ returns 2.2.2, and matches the latest release at https://github.com/jim-easterbrook/python-gphoto2 gp.__version__返回 2.2.2,并匹配最新版本https://github.com/jim-easterbrook/python-gphoto2

Have a look at the gphoto2 program source.看看 gphoto2 程序源。 You might be able to find out what sequence of libgphoto2 functions the --capture-preview command calls.您可能能够找出--capture-preview命令调用的 libgphoto2 函数序列。 You should then be able to reproduce that in Python.然后您应该能够在 Python 中重现它。

I drilled down into the libgphoto2 source.我深入研究了 libgphoto2 源代码。 The cameras I have fall under the PTP world;我拥有的相机属于 PTP 世界; I believe this is in camlibs/ptp2/library.c, where the camera_capture_preview function is located.我相信这是在 camlibs/ptp2/library.c 中,camera_capture_preview function 所在的位置。 The Python code fails with both a Nikon and Sony.尼康和索尼的 Python 代码均失败。 There do seem to be two things in the code which point to possible issues;代码中似乎确实有两件事指出了可能的问题; in the Nikon section, it will try 20 times to get the preview;在尼康部分,它会尝试20次以获得预览; in the Sony section, there are comments regarding timing.在索尼部分,有关于时间的评论。 However, the command line works, so the problem may not have been there.但是,命令行有效,因此问题可能不存在。

I looked at the gphoto2 implementation in Python as well.我还查看了 Python 中的 gphoto2 实现。 The good news is that the code at https://github.com/jim-easterbrook/python-gphoto2/blob/master/examples/preview-image.py works, so if anyone wants to do this in Python, they can replace the好消息是https://github.com/jim-easterbrook/python-gphoto2/blob/master/examples/preview-image.py的代码有效,所以如果有人想在 Python 中执行此操作,他们可以替换这

file_path = camera.capture(gp.GP_OPERATION_CAPTURE_PREVIEW) line file_path = camera.capture(gp.GP_OPERATION_CAPTURE_PREVIEW) 行

with the code there.与那里的代码。

Note that this worked on the rPi with gphoto2 (2.5.20);请注意,这适用于带有 gphoto2 (2.5.20) 的 rPi; on an Ubuntu 16.04 LTS system, this fails (gphoto2 is 2.5.9).在 Ubuntu 16.04 LTS 系统上,此操作失败(gphoto2 为 2.5.9)。

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

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