How to write command line output to text file?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
fatejd
Posts: 2
Joined: 2011-02-01T12:41:13-07:00
Authentication code: 8675308

How to write command line output to text file?

Post by fatejd »

Hi -

I'm somewhat new to command line scripting (Windows XP), and am now stumped about how to get the results of an IM command and write it to a text file. I'm comparing images and would like to get the output (number of pixels that are different) and write it to a text file. Or, better yet, get the result in the script and assign it to a variable for further manipulation.

Here is what I have for the IM part of my script:

Code: Select all

compare -metric AE %currTest%.png %fileName%.png %fileName%_DIFF.png>>results.txt
The variables are file names and directories and some input test numbers. This command writes nothing to the results.txt file though. The output is written to the screen:

Code: Select all

C:\Screenshots>"c:\Documents and Settings\xxxxxxxx\Desktop\test_script.bat" TEST1
1480
Any ideas?

Thanks
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: How to write command line output to text file?

Post by el_supremo »

The output from the compare command is sent to the error output file descriptor 2. So use this command:

Code: Select all

compare -metric AE %currTest%.png %fileName%.png %fileName%_DIFF.png 2>results.txt
Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
fatejd
Posts: 2
Joined: 2011-02-01T12:41:13-07:00
Authentication code: 8675308

Re: How to write command line output to text file?

Post by fatejd »

Thanks for the reply.

Of course, shortly after I posted this I figured out a way to do this, with a variable as well:

Code: Select all

FOR /F %%A in ('compare -metric AE %currTest%.png %fileName%.png %fileName%_DIFF.png 2^>^&1') DO set pixelDiff=%%A
lizac
Posts: 15
Joined: 2011-03-25T19:39:37-07:00
Authentication code: 6789
Location: Philippines

Re: How to write command line output to text file?

Post by lizac »

Hi! Your code is what i needed... Do you know how to convert it in vbscript?

I would actually need the AE value to test if it's equal to zero or not. I'm having a hard time looking for resources where I need to pass the value of batch result to use in my vbscript. Once the value is tested, if it's equal to zero then the script will exit, otherwise it will send an email to me. Thank you in advance.
User avatar
whugemann
Posts: 289
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: How to write command line output to text file?

Post by whugemann »

lizac wrote:Hi! Your code is what i needed... Do you know how to convert it in vbscript?
See http://www.imagemagick.org/Usage/windows/#vb_example how you cann read the result of an operation into a VBScript.

Wolfgang Hugemann
Wolfgang Hugemann
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to write command line output to text file?

Post by anthony »

Example do not detail the handling of standard error output in windows. If there even is such a concept in BAT scripts.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
whugemann
Posts: 289
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: How to write command line output to text file?

Post by whugemann »

anthony wrote:Example do not detail the handling of standard error output in windows. If there even is such a concept in BAT scripts.
The example demonstrates how you read stdout of a DOS command into VBScript. You can then perform the testing of the value within VBScript and act upon the result. So I think that this would do what what is asked for over here. But I will check whether there is a possibility to read stderr back into VBScript.

Wolfgang
Wolfgang Hugemann
pctechtv
Posts: 22
Joined: 2016-10-09T19:47:59-07:00
Authentication code: 1151

Re: How to write command line output to text file?

Post by pctechtv »

Where do you find the output results.txt file after running command? The command is working. I get my jpg output, but I don't have any results file afterwards. My command looks like this

Code: Select all

magick -density 300 "C:\CTestAC1\Pointer.pdf"[0] -crop %[w]x%[fx:w*0.5625]+0+0 "C:\CTestAC1\Pointer.jpg" 2>results.txt
Thanks
pctechtv
Posts: 22
Joined: 2016-10-09T19:47:59-07:00
Authentication code: 1151

Re: How to write command line output to text file?

Post by pctechtv »

OK I see the example is only for the compare command. The title for the post attracted me because I need the command line output in a text file. How can this be accomplished? All I really need is a file-size and name report output in a text file. Thanks
pctechtv
Posts: 22
Joined: 2016-10-09T19:47:59-07:00
Authentication code: 1151

Re: How to write command line output to text file?

Post by pctechtv »

Thanks
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to write command line output to text file?

Post by fmw42 »

Please do not add a post to a very old topic. Also always provide your IM version and platform. Please read viewtopic.php?f=1&t=9620

When you crop an image, it makes new images. You say you want a text file.

Code: Select all

 But what information should be in the text file?
Raster data from the crop new images would not normally be put into a text file.

Images can be written to text, but you would then replace the output jpg with a textfile.txt. But that will be a huge amount of text, one line of text information for every pixel.
pctechtv
Posts: 22
Joined: 2016-10-09T19:47:59-07:00
Authentication code: 1151

Re: How to write command line output to text file?

Post by pctechtv »

Code: Select all

version: ImageMagick-7.0.3-Q16
I just need the jpg file name and the file size in a text file. I don't need any conversion of the jpg output to text. I want this in same directory that my jpg is output to. I want it generated with the same command that creates the jpg output. That is why I thought the command above with the pipe output was what I am looking for. When my command is finished, I want to a jpg of my PDF, and text file reporting the name and file size (kb) of the jpg. Thanks
Last edited by pctechtv on 2016-10-29T10:28:25-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to write command line output to text file?

Post by fmw42 »

There is no output image, yet, to get its name, so this would only get the output size

Code: Select all

magick logo: -crop 500x300+100+100 -format "%w %h" -write info:tmp.txt logo_crop.jpg
So the text file will only have
500 300

If you want the output name, then you need a second command

Code: Select all

magick logo: -crop 500x300+100+100 logo_crop.jpg
magick logo_crop.jpg -format "%f %w %h" info: > tmp.txt
Here the textfile will have
logo_crop.jpg 500 300
pctechtv
Posts: 22
Joined: 2016-10-09T19:47:59-07:00
Authentication code: 1151

Re: How to write command line output to text file?

Post by pctechtv »

Yes... no file so I need second command after! I get it. thank you. The two commands will be fine, that is exactly what I am looking for. Thanks
Post Reply