convert in_file -crop $GEOM -write output_file -> Error missing output_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
BrianP007
Posts: 49
Joined: 2013-12-13T09:54:14-07:00
Authentication code: 6789

convert in_file -crop $GEOM -write output_file -> Error missing output_file??

Post by BrianP007 »

Hi,

I need to make crops in identical places to a number of images WITHOUT destroying the originals. The -write option to convert takes a filename argument, "-write filename". This would seem to have zero ambiguity as written.

Convert a specified image by cropping it to a given geometry (size and starting XY) and write the output to a different filename. A complete logical process, a complete sentence, a complete, fully specified command. Just what is needed.

There is an error which seems to make no sense since the crop is done as specified (after hitting the F5 key to refresh windoz exlorer). I make it a point to check return codes and the $ERR string in PerlMagick after such operations.

The command line:
>"c:\Program Files\ImageMagick-6.8.9-Q16\convert.exe" tl-2014.0416-215799.p1.pristine.jpg -crop 2100x1300+5400+2900 -write 215799.zero.crop.jpg
convert.exe: missing an image filename `215799.p1.zero.crop.jpg' @ error/convert.c/ConvertImageCommand/3184.

The convert command line seems to follow the web documentation to the letter. The output image is just what is asked for. The error message appears to be completely out of line because the -write <<FILENAME>> is given, understood and used BY CONVERT as intended. Then it squawks about not having the exact filename it just read and used correctly.

Is it necessary to just keep a running of list of errors that are not really errors so they can be safely ignored? Or, am I missing a subtlety in the convert command?

Code: Select all

    $err = $CONVERT_COMMAND;
    if($err  &&  $err =~ m/missing an image filename .*$crop_name.*convert.c)  {
        if(-f $crop_name)  {
            # Ignore totally Bo-o-o-gus error. Crop done, file written.
        }
        else  {
            &FREAK_OUT_CROP_REALLY_FAILED_THIS_TIME();  #
       }
    }
Bizarre,
Brian


From http://www.imagemagick.org/script/comma ... .php#write
-write filename write an image sequence;
The image sequence preceding the -write filename option is written out, and processing continues with the same image in its current state if there are additional options ...


Version: ImageMagick 6.8.9-2 Q16 x64 2014-05-27
win7/64
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: convert in_file -crop $GEOM -write output_file -> Error missing output_file??

Post by snibgo »

BrianP007 wrote:Or, am I missing a subtlety in the convert command?
Yes.

The minimum convert syntax is:

Code: Select all

convert input-image output-image
You can put lots of extra stuff before or after the input-image. You can't put anything after the output-image.

One of the extra stuff you can use, if you want, is "-write filename".

So, you can probably see the problem now. You don't have an output-image. If you remove "-write", it should work.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: convert in_file -crop $GEOM -write output_file -> Error missing output_file??

Post by fmw42 »

You can use multiple -write filenames each in its own parenthesis with its own set of options and then just put null: for the final output.

windows syntax (I think):

Code: Select all

convert input ^
( -clone 0 <options> -write filename1 +delete ) ^
( -clone 0 <options> -write filename1 +delete ) ^
... etc ...
( -clone 0 <options> -write filenameN +delete ) ^
null:
The null: is needed to remove the input from memory (discard it). The +deletes get ride of each clone after writing the processed result to disk.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: convert in_file -crop $GEOM -write output_file -> Error missing output_file??

Post by snibgo »

Yes. So a different way of fixing Brian's command is by adding "NULL:" to the end, as the output-image:

Code: Select all

"c:\Program Files\ImageMagick-6.8.9-Q16\convert.exe" tl-2014.0416-215799.p1.pristine.jpg -crop 2100x1300+5400+2900 -write 215799.zero.crop.jpg NULL:
As a general rule, I do this only for cases like Fred's where the command isn't really creating a single output.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: convert in_file -crop $GEOM -write output_file -> Error missing output_file??

Post by fmw42 »

snibgo wrote:As a general rule, I do this only for cases like Fred's where the command isn't really creating a single output.
I would agree. I don't often use this technique either.
BrianP007
Posts: 49
Joined: 2013-12-13T09:54:14-07:00
Authentication code: 6789

Re: convert in_file -crop $GEOM -write output_file -> Error missing output_file??

Post by BrianP007 »

OK. I see the pattern. And it explains the error message.
convert INPUT_FILE [options ... ] OUTPUT_FILE <enter>
...
Option `-WRITE filename` may be used to write intermediate results as the command is executed in chunks!
Very handy.
The output_file was being absorbed by the option -write leaving the second and last MANDATORY arg empty. And, it did the right thing, produce the crop, with the information that it had rather than just die with a cryptic message (bad command or file name) because something wasn't perfect. Robust!

>> "The ImageMagick command line can be as simple as this. $ convert image.jpg image.png " is the link I missed! D'oh!

I just did not want to always dump STDERR > /dev/null as was tempting to do or write kludgy code for it.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: convert in_file -crop $GEOM -write output_file -> Error missing output_file??

Post by snibgo »

Yes, that's it.

Slightly confusingly, the inputs and and outputs don't have to be images or files. This is fine:

Code: Select all

convert xc: info:
It makes an image with a single white pixel, then writes text information about it.
snibgo's IM pages: im.snibgo.com
Post Reply