Page 1 of 2
Output file only when finished?
Posted: 2008-12-29T23:32:39-07:00
by rich95
Hello there;
I'm having trouble trying to get convert.exe to perform a specific function.
I'm having it convert one file to another in a dos shell, let's say file1.jpg to file2.tga.
Now, when I send this to the command prompt, it initiates the process and finishes it. In most cases, if the file is small, it finishes quickly and the next function in my script successfully runs (an image loader that loads the converted image).
However, sometimes there is a rather large file and the conversion takes much longer to complete and this messes things up because the image loader tries to load a file that has not yet been successfully converted, resulting in an error.
All this would be easily resolved if I could check if the converted file exists only when it's done converting and only load it then. However, convert.exe spits out a file and continues to write to it during the conversion.
My question is:
Is it possible to have convert.exe only output the converted file only once it's been converted?
This is all happening on Windows XP through the command prompt.
Any suggestions would be welcome.
Thanks,
-Richard
Re: Output file only when finished?
Posted: 2008-12-29T23:51:10-07:00
by anthony
IM converts the file as it is outputing it. That is the way it works. It can take time to write any file, the larger it is the longer it takes. It is a matter of buffer size. A small file (less than 4k) is near instant in writing.
Normally programs and scripts waits for the convert command to exist before proceeding. If it is backgrounded it waits for the 'child signal' to inform the parent process that it has completed. However as you are not doing this 'usual' practice, I assume this is not convenient, probably as the language is limited in some way. It is posible in shell, perl, and even PHP, but not always simple.
However can I suggest an alternative, that may be simplier.
-write the file you want, but then clear all the images, and have IM output a very small image in another file. The calling script can look for the existence of the second smaller file and when it appears it will know that IM has finished writing the first file. The script can then clean up the second 'flag' file, and do what it wants with the first.
Something like...
Code: Select all
convert image.jpg -write converted_image.tga -delete 0--1 -size 1x1 xc: flag.gif
Re: Output file only when finished?
Posted: 2008-12-30T01:20:42-07:00
by rich95
Hi Anthony;
Thanks for your reply and this should certainly work. If you'll bear with me, I'm trying to understand exactly what we're doing here.
The -write function outputs a sequence of files and it looks like your command will output two files, converted_image.tga and flag.gif which is a 1x1 pixel image of image.jpg, correct?
What I don't get is the -delete flag. That would delete my main image before I could grab it. I have access to deleting outside of imagemagick so that would be no problem. Also, if it refers to deleting the source image, I wouldn't want to do that either.
So having said that, if I want to output two files, the main converted one and a 1x1 pixel flag.gif, created only after the main one is converted, would my code be:
Code: Select all
convert image.jpg -write converted_image.tga -size 1x1 xc: flag.gif
?
This would leave me with the final large converted_image.tga and the newly-created flag.gif. When flag.gif exists, it means the first file has been successfully finished. I can then grab it, delete it once it's grabbed, and delete the flag as well using my own script. Is that correct? Do I have the above code properly written?
Thanks,
-Richard
Re: Output file only when finished?
Posted: 2008-12-31T13:23:00-07:00
by palpacino
Wow. Ironic.
I, too, have been looking into this today and trying to determine a good method for waiting until IM is done before continuing on with my code.
A few weeks ago it seems I saw a way for IM to output its progress or finished progress to a log file, but I cannot find that info again on the net (if I, indeed, am remembering correctly).
Perhaps between you and I we can figure out a method of doing that as opposed to the writing out of a small file as mentioned? It might be half a dozen here and six the other way, though.
I started down the path this morning of monitoring (in my case) the convert.exe process running, but I found some issues there with the way I am shelling it with cmd.exe (winxp).
Just a thought. I'll post back here if I find out anything else.
Re: Output file only when finished?
Posted: 2008-12-31T13:38:47-07:00
by palpacino
The only technical thing I can think of that I am seeing as I am doing it the way Anthony suggested above (thank you by the way Anthony) is that it still MIGHT MIGHT have a very small delay in writing the small flag file. In my case, I am wanting to copy the output file elsewhere and clean up the junk including the flag file right after that. I'm seeing it, on my mediocre system, lagging every once in awhile randomly and when it tries to delete that flag file pretty much right after it sees it it will bomb out because the flag file itself is in use by IM for that brief moment in time.
Re: Output file only when finished?
Posted: 2009-01-01T02:25:03-07:00
by anthony
rich95 wrote:TThe -write function outputs a sequence of files and it looks like your command will output two files, converted_image.tga and flag.gif which is a 1x1 pixel image of image.jpg, correct?
Yes exactly
What I don't get is the -delete flag. That would delete my main image before I could grab it. I have access to deleting outside of imagemagick so that would be no problem. Also, if it refers to deleting the source image, I wouldn't want to do that either.
It is set to delete ALL images from memory at the point, after it has written the image. It is important as you only want to write the small image (for speed) at the end to say...
I am finished.
So having said that, if I want to output two files, the main converted one and a 1x1 pixel flag.gif, created only after the main one is converted, would my code be:
Code: Select all
convert image.jpg -write converted_image.tga -size 1x1 xc: flag.gif
?
that will add a second image into memory, so the 'flag.gif' will be two file animation. one BIG one and a small one. You need to delete the big one after writing so the small one is written FAST at the end. Rememeber -write has written it out already at that point.
IM should do operations ion the order given.
This would leave me with the final large converted_image.tga and the newly-created flag.gif. When flag.gif exists, it means the first file has been successfully finished. I can then grab it, delete it once it's grabbed, and delete the flag as well using my own script. Is that correct? Do I have the above code properly written?
Thanks,
-Richard[/quote]
Re: Output file only when finished?
Posted: 2009-01-01T02:33:07-07:00
by anthony
palpacino wrote:I, too, have been looking into this today and trying to determine a good method for waiting until IM is done before continuing on with my code.
As I mentioned the proper way programmically is to set you a signal handler in the calling parent that will get called when a child exits. The parent then 'waits' on the child and flags that it has finished, for the main loop to pick up on and continue. THAT is the PROPER way, and the FASTEST way.
When IM is finished it exits, and and exiting process signals its parent it is finished so the parent can 'wait' on it and acnologe it is finished. Otherwise the process just hands around as a 'zombie' until the parent dies. It is the UNIX way of process handling.. Parents out live there children and acknologe there deaths with a 'system wait'.
I suggest you get a basic programming under UNIX book.
Here is a favorite quote of mine on this...
The life of processes in the UNIX environment is weird. Parent
processes normally out live their children, and in fact processes who
die become zombies until either their parents recognise they are in
fact dead, or they themselves die. -- Treaty on UNIX Process Handling
Re: Output file only when finished?
Posted: 2009-01-06T16:20:35-07:00
by palpacino
I am using the following:
Code: Select all
convert -adjoin -compress Group4 -define quantum:polarity=min-is-white c:\a.tif c:\b.tif -write c:\output.tif -delete 0--1 -size 1x1 xc: C:\flag.tif
...And that is outputting two files (output.tif and flag.tif) just fine. The output.tif file is a big one and the flag.tif is very small just as expected.
This worked!
I really do appreciate you Anthony. Thanks for your input as it is greatly valued. I did not mean to sound like I was second guessing you in the previous message.
Re: Output file only when finished?
Posted: 2009-01-06T18:42:24-07:00
by anthony
You are welcome. You just mis understood what I was getting IM to do.
However I would seriously recommend that look at correct UNIX process handling, and specifically handling background (forked) child processes.
When IM is finished the process will exit which will report back to the parent, even if the parent is doing other things. A signal handler will let the parent note the child is finished, so the main look can handle it when it is ready to do so.
If you are not backgrounding the process 9to do other things in the mean time) a simple wait can be used to have the parent 'hang' until the child exists. That is what normall happens when you run commands say from the command line!
Even shell scripts can do this child handling (bourne shell, not csh deritives that is)
Re: Output file only when finished?
Posted: 2009-01-08T09:22:11-07:00
by palpacino
But Anthony unless I am misunderstanding you once again I do not think I would need to know or learn anything about UNIX since I am doing this in windows XP... as was the original poster in this thread. I am a UNIX virgin. lol.
Unless you just mean it would be a good idea to learn about computing and processes and forked processes, etc in general?
As it turns out, I am still seeing my computer grabbing the output file too quickly even though the program is looping and waiting for the flag file. It only tries to copy the output file once it sees the flag file. It works 90+% of the time, but sometimes it grabs and copies the output file (I think) before its actually done and the file won't open in my case.
Does anyone know why this code:
Code: Select all
convert -adjoin -compress Group4 -define quantum:polarity=min-is-white c:\a.tif c:\b.tif -write c:\output.tif -delete 0--1 -size 1x1 xc: C:\flag.tif
might take a long time to run sometimes? If I have a 13 page tif file and I want to add 20 pages to the beginning of it from another tif image (join them), it will take a really long time and thats usually when my app screws up. Not sure why that command would take so long even though its fairly long.... maybe the compression?
What I am doing is in VB. It shells that command up above in DOS. It then kicks off a loop to just wait for the flag file. When it sees it, it then copies the output.tif file out to a network path. I originally had IM just writing its file out to the network path, but it was going too slow due to the constant writing and latency. So, now I want to have it just do everything locally, then as a last step copy its file out to the network...
Re: Output file only when finished?
Posted: 2009-01-08T17:53:17-07:00
by anthony
Sorry. I am not a Windows user let alone a programmer.
I think it is a specific window thing about lot letting an application open a file will another application still has it open for write, even if it is finished writing. Prehaps it its something to do with the window OS buffering the data.
It may be you will need to find a windows programming book or user to help for this.
Re: Output file only when finished?
Posted: 2009-01-08T18:57:41-07:00
by palpacino
Thank you Anthony.
Re: Output file only when finished?
Posted: 2009-01-12T04:39:31-07:00
by strawberryfield
I have the same problem in a javascript program using com interface...
My opinion is that a -wait or somewhat similar flag that impose to convert to not return to the caller until finished will be very useful (and simple to use) in these cases.
Re: Output file only when finished?
Posted: 2009-01-12T22:12:55-07:00
by anthony
The thing is imagemagick has finsihed, as far as it is concerned. Everything has been written out and all is done.
That is happening is that while IM has finished the OS and disk drives and whatever else is involved hasn't finished. This is not a problem with IM itself.
Re: Output file only when finished?
Posted: 2009-01-13T13:23:49-07:00
by palpacino
Anthony is right.
And it stinks because you have to do other measures to make sure the file has been written.