Problem getting terminal output from ImageMagick's compare.exe ( Either by pipe or Python )
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 h开发者_JAVA技巧appening?
Cheers,
Nathan.
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)
compare -metric AE -fuzz 2000 1.png 2.png diff.png 2> tmp.txt
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.
Seems like you want PythonMagick.
EDIT:
Ok, based on AndiDog's answer, here's what your Popen
call should look like:
myOutput=subprocess.Popen("C:\\usr\\local\\bin\\att\\compare.exe -metric AE -fuzz 100 1.png 2.png mask.png", stderr=subprocess.PIPE)
Or, if stdout
also prints useful information, you could do this:
myOutput=subprocess.Popen("C:\\usr\\local\\bin\\att\\compare.exe -metric AE -fuzz 100 1.png 2.png mask.png", stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
Still, why use Popen when you can use ImageMagick directly through Python bindings?
精彩评论