Selecting piped images

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
AlicanC
Posts: 9
Joined: 2015-10-22T01:43:05-07:00
Authentication code: 1151

Selecting piped images

Post by AlicanC »

I wrote a program that does this and it works perfectly:

Code: Select all

{ convert rose: miff:-; convert rose: -colorspace gray miff:-; } \
  | convert miff:- +append result.png
Now I have a line like this which I have to use it with piped images:

Code: Select all

convert one.png \( two.png +level 50x100% -blur '0x5' \) -compose multiply -composite result.png
I have tried these two and both failed:

Code: Select all

{ convert rose: miff:-; convert rose: -colorspace gray miff:-; } \
  | convert miff:- \( miff:- +level 50x100% -blur '0x5' \) -compose multiply -composite result.png

convert: improper image header `/var/tmp/magick-71624xtFCjx32DkrZ' @ error/miff.c/ReadMIFFImage/510.

{ convert rose: miff:-; convert rose: -colorspace gray miff:-; } \
  | convert miff:-[0] \( miff:-[1] +level 50x100% -blur '0x5' \) -compose multiply -composite result.png

convert: improper image header `/var/tmp/magick-716281IPGsqH7SEAO' @ error/miff.c/ReadMIFFImage/510.
convert: no images defined `result.png' @ error/convert.c/ConvertImageCommand/3212.
A workaround is this, but it looks ugly:

Code: Select all

{ convert rose: miff:-; convert rose: -colorspace gray miff:-; } \
  | convert miff:- \( -clone 1 +level 50x100% -blur '0x5' \) -delete 1 -compose multiply -composite result.png
Is there a better way to achieve this?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Selecting piped images

Post by snibgo »

It may look ugly but it is correct.

"convert miff:-" reads all the images that are available from stdin. There are no more images for another "miff:-" to read.

An alternative is to do the level and blur in the convert before the pipe.
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: Selecting piped images

Post by fmw42 »

Does this do what you want?

Code: Select all

(
convert rose: \( -clone 0 -colorspace gray \) miff:- 
) | convert - \( -clone 0 \) \( -clone 1 +level 50x100% -blur '0x5' \) \
-delete 0,1 -compose multiply -composite result.png   
or simpler

Code: Select all

convert rose: \( -clone 0 -colorspace gray +level 50x100% -blur '0x5' \) \
-compose multiply -composite result.png
Or am I misunderstanding what you desired to do? Why must it have piped input?
AlicanC
Posts: 9
Joined: 2015-10-22T01:43:05-07:00
Authentication code: 1151

Re: Selecting piped images

Post by AlicanC »

fmw42 wrote:Or am I misunderstanding what you desired to do? Why must it have piped input?
I am actually implementing your "spot" script on JavaScript. Unlike in your scripts, I can't afford to write files in each step so I keep MIFFs in memory.

Take this command from the "spot" script for an example:

Code: Select all

convert \( $tmpA1 -define distort:viewport=${www}x${hhh}+0+0 -virtual-pixel mirror -distort SRT 0 \
	-scale ${xmin}x${ymin}! -scale ${www}x${hhh}! -crop ${ww}x${hh}+0+0 +repage \) \
	\( $tmpA2 -write mpr:tile +delete -size ${ww}x${hh}! tile:mpr:tile \) \
	-alpha off -compose copy_opacity -composite -compose over \
	-background $bgcolor -flatten  \
	"$outfile"
Your script reads images from the file system, but my program can't do that because the images are actually in memory.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Selecting piped images

Post by fmw42 »

Can't you just fold the processing for $tmpA2 into a parentheses section where you have $tmpA2. But what does this have to do with piping? You should avoid piping and just fold all the commands together into one big command line.
AlicanC
Posts: 9
Joined: 2015-10-22T01:43:05-07:00
Authentication code: 1151

Re: Selecting piped images

Post by AlicanC »

fmw42 wrote:Can't you just fold the processing for $tmpA2 into a parentheses section where you have $tmpA2. But what does this have to do with piping? You should avoid piping and just fold all the commands together into one big command line.
I could, but I didn't since you didn't.

What you do in your scripts is:

Code: Select all

- Read input, write temp.
- If some parameter given, read temp, do something, write temp.
- If some parameter given, read temp, do something, write temp.
- If some parameter given, read temp, do something, write temp.
...
- Read temp, write output.
and not:

Code: Select all

- Start generating a big command line.
- If some parameter given, add "do something" to command line.
- If some parameter given, add "do something" to command line.
- If some parameter given, add "do something" to command line.
...
- Read input, run the big command line with multiple "do something"s, write output.
I could follow the second method when I port your scripts to JavaScript, but I'm not an ImageMagick god and I would probably waste a lot of time trying to debug my ports. That's why I try to make my ports as close to the originals as possible. Though I might have to do this since the scripts are also ridiculously slow when it comes to applying them to 100-200 frame MIFFs.

Maybe the real solution is IM7 scripts, but I haven't found any documentation on them and have no idea what they are capable of.
Last edited by AlicanC on 2015-11-11T07:31:21-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: Selecting piped images

Post by fmw42 »

I save temps because I often have multiple options and conditionals before the next step. But if you know exactly what you want, then it is better to just fold them together into one larger command line. Or if conditionals, then repeat the code for each conditional and avoid the temps.
Post Reply