Page 1 of 1

magick script image list/sequence handling

Posted: 2018-09-28T00:13:39-07:00
by dirk1976
What possibilities are there to work with image sequences on the command line for "magick -script"?
Questions:
  1. change the image iterator index (like MagickWand.MagickSetIteratorIndex)
  2. get the current iterator index (like MagickWand.MagickGetIteratorIndex)
  3. write only the image on the current iterator index (like MagickWandImage.MagickWriteImage)
  4. how to find out if there is at least one image in the image sequence or count the images in the image sequence
My current solutions:
  1. "( -clone INDEX ... +delete )", but maybe there is another solution
  2. -print "%p", I don't know if this works (it is always 0), because the iterator index can't be set
  3. "( -clone INDEX -write FILE +delete )", but maybe there is another solution
  4. -print "%n", but if no image is in the image list then this value is still 1
    -print "%n file:%i\n", if there is no image left then the file name is empty
    identify is also not an option, because the program will exit with an error
Thanks

Re: magick script image list/sequence handling

Posted: 2018-09-28T04:17:05-07:00
by snibgo
At the command line, and in a magick script, there is no iterator index. At every stage in the command there is a list of images. If we are inside a parenthesis ( ) then there is a stack of lists.

Most operations, such as "-blur" and "-write", operate on all the images within the current list. Some operations ("-delete", "-insert", "-swap") modify the current list. "-clone" copies an image from the outer list and adds it to the end of the current list.

This means there is no direct method for, say, writing the Nth image in the list. We do it indirectly, by starting a new list, copying the second image from the outer list, writing all the images in the current list, emptying the current list, and closing it:

Code: Select all

( -clone 3 -write abc.png +delete )
(This is clumsy and error-prone. I like the G'MIC syntax, where operations can be applied to all images or just specified images.)

Also remember that IM operations are carried out in the order given. There are no flow-control statements for loops, conditionals etc.

"-print %n" works as expected for me, IM v7.0.7-28 on Windows 8.1:

Code: Select all

f:\web\im>%IMG7%magick -print %n xc: NULL:
0
f:\web\im>%IMG7%magick xc: +delete -print %n NULL:
0
f:\web\im>%IMG7%magick xc: -print %n NULL:
1
f:\web\im>%IMG7%magick xc: xc: -print %n NULL:
2

Re: magick script image list/sequence handling

Posted: 2018-09-28T04:27:40-07:00
by dirk1976
Thanks for your explanation.

I test "-print %n" with v7.0.7 and it works, but with v7.0.8 (Portable) it don't work. It is always 1. So maybe it's a bug in v7.0.8.

Re: magick script image list/sequence handling

Posted: 2018-09-28T04:48:45-07:00
by snibgo
Please report it to the bugs forum: viewforum.php?f=3&sid=f38adf323ccee0396ee5921236a62143

Re: magick script image list/sequence handling

Posted: 2018-09-28T04:53:49-07:00
by snibgo
Regarding %p: this is relevant when used in an operation that operate on all images in the list, such as "-write". Eg:

Code: Select all

f:\web\im>%IMG7%magick xc: xc: -format %p\n -write info: NULL:
0
1
"-print" only operates once.

Re: magick script image list/sequence handling

Posted: 2018-10-01T04:19:11-07:00
by dirk1976
In you last comment you set a format. My question is now how to reset the format so an identify or verbose identify command will work again? I want to use magick script to reuse the magick process. So the process initialization time can be saved.

Re: magick script image list/sequence handling

Posted: 2018-10-01T05:43:43-07:00
by snibgo
disk1976 wrote:...how to reset the format...
Use "+format" to reset to default, eg:

Code: Select all

f:\web\im>%IMG7%magick xc: xc: -format %p\n -write info: +format +write info: NULL:
0
1
xc:[0] XC 1x1 1x1+0+0 16-bit sRGB 0.000u 0:00.000
xc:[1] XC 1x1 1x1+0+0 16-bit sRGB 0.000u 0:00.015