[英]search for latest image inside a folder, and save it as a png file with python
I am new to python. 我是python的新手。 I want to do some image analysis using the python on Raspberry Pi.
我想在Raspberry Pi上使用python做一些图像分析。
I am streaming images using motion to a folder. 我正在使用运动将图像流传输到文件夹。 In that folder, I want to find the latest image at any point of time.
在该文件夹中,我想随时查找最新图像。 Then I would like to apply my image analysis command on that one image and export the results to a text file.
然后,我想在那张图像上应用我的图像分析命令,并将结果导出到文本文件。 I have achieved most parts of it.
我已经实现了大部分。 But struck at one small bit.
但是只有一点点。
import subprocess
import os
import glob
newest = max(glob.iglob('*.[p][n][g]'), key=os.path.getctime)
print(newest)
cmd = "sudo ./deepbelief lena.png > try5.txt"
proc = subprocess.Popen(cmd, shell=True)
In the above deepbelief is my image analysis program. 以上是我的图像分析程序。 Now the problem is how can I feed my newest image as input to the command ./deepbelief.
现在的问题是,如何将最新的图像作为./deepbelief命令的输入。
Also, if possible, can I save newest image as an png file for later use? 另外,如果可能,我可以将最新图像另存为png文件以供以后使用吗?
Many thanks in advance! 提前谢谢了!
You can use string formatting: 您可以使用字符串格式:
cmd = "sudo ./deepbelief {} > try5.txt".format(newest)
The {}
will be whatever the value of newest is, so for example if newest is foo.png, you're command will be "sudo ./deepbelief foo.png > try5.txt"
and so on.. {}
将是newest的值,例如,如果newest是foo.png,则您的命令将是"sudo ./deepbelief foo.png > try5.txt"
,依此类推。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.