从 ImageMagick 的 Compare.exe 获取终端输出时出现问题(通过管道或 Python)
我对 python 相当陌生,但对其他语言有相当多的经验。我想知道是否有人可以帮忙。
问题 我试图合并两个图像的比较(使用 ImageMagickscompare.exe),然后根据输出做出决定。
我遇到了问题,因为我似乎无法将 Compare.exe 的输出提取到我自己的代码中。
在命令行运行我的命令,我得到了所需的差异度量:
C:\usr\local\bin\att>compare -metric AE -fuzz 2000 1.png 2.png diff.png
8772
C:\usr\local\bin\att>_
问题是,如果我尝试将其通过管道传输到文本文件:
C:\usr\local\bin\att>compare -metric AE -fuzz 2000 1.png 2.png diff.png > tmp.txt
8772
该度量仍然显示在控制台上,而不是写入文本文件。
我使用 python 取得的唯一成功是延迟输出,但我仍然无法将其捕获到变量中。
执行:
myOutput=subprocess.Popen("C:\\usr\\local\\bin\\att\\compare.exe -metric AE -fuzz 100 1.png 2.png mask.png", stdout=subprocess.PIPE)
不会在控制台上显示“8772”,直到我调用:
line = myOutput.stdout.readline()
将其写入控制台输出时,但我的变量将为 NULL。
任何人都可以帮忙解决这个问题,或者有人知道为什么会发生这种情况吗?
干杯,
内森。
I'm fairly new to python, but have a fair amount of experience with other languages. I wonder if anyone can help.
The Problem
I'm trying to incorporate the comparison of two images (using ImageMagicks compare.exe) and then make a decision based on the output.
I'm hitting problems, because I can't seem to pull the output from compare.exe into my own code.
Running my command at the command line, I get the required metric of difference:
C:\usr\local\bin\att>compare -metric AE -fuzz 2000 1.png 2.png diff.png
8772
C:\usr\local\bin\att>_
The problem is if I try and pipe this to a text file:
C:\usr\local\bin\att>compare -metric AE -fuzz 2000 1.png 2.png diff.png > tmp.txt
8772
The metric is still displayed at the console, and not written to the text file.
Following on the only success I've had using python is to delay the output, but I still can't capture it to a variable.
Doing:
myOutput=subprocess.Popen("C:\\usr\\local\\bin\\att\\compare.exe -metric AE -fuzz 100 1.png 2.png mask.png", stdout=subprocess.PIPE)
will not display '8772' to the console until I then call:
line = myOutput.stdout.readline()
when it will be written to console output, but my variable will be NULL.
Can anyone please help with this, or dose anyone have any clue why this is happening?
Cheers,
Nathan.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
比较工具在 stderr 上输出结果。当然,这完全没有意义,但要解决这个问题,您需要将 stderr 转发到文件(而不是 stdout)
您最好使用 Python ImageMagick 模块。如果发生错误,EXE 文件甚至不会返回非零值,因此您无法真正在批处理脚本中合理地使用它。
The compare tool outputs the result on stderr. Of course that totally does not make sense, but to work around it you need to forward stderr to a file (instead of stdout)
You would really be better off using the Python ImageMagick module. The EXE file does not even return a non-zero value if an error occurs, so you can't really use it reasonably in a batch script.
看起来你想要 PythonMagick。
编辑:
好的,根据 AndiDog 的回答,您的
Popen
调用应如下所示:或者,如果
stdout
还打印有用的信息,您可以这样做:不过,当您可以通过 Python 绑定直接使用 ImageMagick 时,为什么还要使用 Popen 呢?
Seems like you want PythonMagick.
EDIT:
Ok, based on AndiDog's answer, here's what your
Popen
call should look like:Or, if
stdout
also prints useful information, you could do this:Still, why use Popen when you can use ImageMagick directly through Python bindings?