简体   繁体   English

在文件夹中搜索最新图片,然后使用python将其另存为png文件

[英]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.

相关问题 Python 保存二进制图像:PNG 文件被 ASCII 转换损坏 - Python save binary image: PNG file corrupted by ASCII conversion 如何将自定义信息保存到 Python 中的 PNG 图像文件? - How do I save custom information to a PNG Image file in Python? Python代码在另一个文件夹中搜索一个文件夹(不是文件) - Python code to search a folder(not file) inside another folder (python)我想将抓取的图像保存在我的文件夹中。 例如,它看起来像“google.png” - (python) I want to save scraped image in my folder. For example, it looks like 'google.png' 使用python将灰度图像保存为4位png - Save grayscale image as 4 bit png with python 如何使用Django和Python下载文件并将其保存在上载文件夹中 - How download file and save it inside the upload folder using Django and Python 如何在Python中使用透明性将.EPS文件保存到PNG - How to save an .EPS file to PNG with transparency in Python 如何拍摄由 Python-tkinter 创建的正在运行的 GUI window 的快照并保存为图像文件(.jpeg/.png)? - How to take a snapshot of a running GUI window created by Python-tkinter and save as an image file (.jpeg/.png)? 如何使用 HTML 表单输入类型文件和烧瓶将图像保存到静态文件夹内的文件夹中 - How to save an image into a folder inside the static folder using HTML form input type file and flask 如何使用tkinter中的文件对话框和Python中的Pil将文件另存为已编辑图像(png) - How to 'save as' an edited image (png) using a file dialog in tkinter and Pil in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM