piping more than once

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
fragileandys
Posts: 11
Joined: 2012-02-07T10:53:21-07:00
Authentication code: 8675308

piping more than once

Post by fragileandys »

I am dynamically creating a watermark image with 'convert' then piping it into a 'composite' command to watermark the image without having to save the watermark image:

Code: Select all

convert -gravity center -size 90x60 label:"username" miff:- | composite -dissolve 90 -tile - image.jpg jpg:-
After the composite command, I would like to pipe again but use the firstly created watermark in the 3rd command ( after the 2nd pipe)
so something like:

Code: Select all

create watermark | use watermark to create an image | use both the watermark and image again
is this possible? I read the bit on fd:5 file descriptor's and tried using it similar to a variable but I'm assuming I wasn't using it right because it didn't work.

any help would be greatly appreciated! thanks
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: piping more than once

Post by anthony »

A multi-command pipeline is easy with UNIX.

Code: Select all

   convert .... miff:- | composite - ... miff:- | convert - ..... result.png
However convert does have a -composite operator (image order reversed from "composite" command), so you can do it all in one command.
http://www.imagemagick.org/Usage/compose/#compose

The -watermark option in "composite" is a -compose modulate method. I have no idea why it is called "modulate" or where the compose methods definition came from, but that is what it is.

Code: Select all

    convert \( ....generate background.... \) \
             \( ....generate overlay.... \) \
             -compose modulate -geometry +100+100  -composite \
             result.png
For more detailed example of "watermark" or "modulate" composition.
http://www.imagemagick.org/Usage/compose/#watermark



If on the other hand you mean mutli-image pipelines. That also is no problem. But only if the image file format is a 'streaming' image file format. MIFF, and PbmPlus (NetPbm), are streaming formats.

Code: Select all

  convert image1.png  image2.png image3.png miff:- | convert miff:- ..... result.png
OR even loop over a whole directory of images, processing each before writing into the pipeline.

Code: Select all

  for image in *.png; do
       convert "$image" ... miff:- 
  done | convert miff:- ..... result.png
See MIFF Pipeline Streaming
http://www.imagemagick.org/Usage/file/#miff_stream
also see Layered Image Examples, Programmed Images, for more about this technique
http://www.imagemagick.org/Usage/layers/#layer_prog
The key is that you can continuiously
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: piping more than once

Post by anthony »

fragileandys wrote:I read the bit on fd:5 file descriptor's and tried using it similar to a variable but I'm assuming I wasn't using it right because it didn't work.

any help would be greatly appreciated! thanks
The use of file descriptors can be extremely tricky. And that is just is shell handling. Perhaps a small example of exactly what you did to setup and use fd:5 would help.


IMv6 does not help a lot when reading images from a file descriptor (or pipeline), as it can only read ALL the images in a pipeline, and then IM will closes that pipeline, making it useless for any other use of that pipe. Named pipes however may work better as that have very specific open-close handling.

I am wanting IMv7 (in development) coders (those that deal with streaming images) to be able to read just ONE image from a 'file stream' but then leave 'file streams' open so that another image (or even something else) can be read from that stream. That is add a "read one image and do not close", operator.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
fragileandys
Posts: 11
Joined: 2012-02-07T10:53:21-07:00
Authentication code: 8675308

Re: piping more than once

Post by fragileandys »

Well, what I'm trying to do is use the watermark created in the 1st command and the watermarked image created in the 2nd command to then in a 3rd piped command use the -stegano function to hide the watermark in the watermarked image.

so I would like to do:

Code: Select all

 convert -gravity center -size 90x60 label:"username" miff:- | composite -dissolve 90 -tile - image.jpg jpg:- | composite -stegano +10+10 {watermark} {watermaked_image} out.jpg
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: piping more than once

Post by anthony »

SO you are 'marking' the image twice: once with composition (visible), and one with stegano (invisible).

WARNING: a stegano image can not be saved using JPEG as JPEG is a lossy format. That is stegano saves in the lowest color bits of the image, but JPEG wipes out those bits with its lossy compression!

here is my - all in one - version using 'logo:' as the image to be processed.

Code: Select all

convert   logo: \
              \( -background white label:"username" -rotate -30 -write mpr:tile \) \
              \( -clone 0 -tile mpr:tile -draw 'color 0,0 reset' \) \
              \( -clone 0,2  -define compose:args=30x100 -compose dissolve -composite \) \
              -delete 0,1 miff:- |\
    composite miff:-  -stegano +15+2 result.png
Note the miff: pipeline has two images in it, in the order: wartermark image , background image

I was supprised that -stegano was only available in "composite" command -- I thought it was available in "convert" but it seems that was not the case! I'll have to see about rolling it into "convert" as part of the IMv7 Shell API project (in alpha development).

For information the above style of image processing see
IM examples, Basics, Parenthesis
http://www.imagemagick.org/Usage/basics/#parenthesis
and Complex Image Processing and Debugging
http://www.imagemagick.org/Usage/basics/#complex

For in-memory image tiling (over another image) see
Canvas Creation, Tiling with an Image already In Memory
http://www.imagemagick.org/Usage/canvas/#tile_memory

And Dissolve Composition
http://www.imagemagick.org/Usage/compose/#dissolve
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
fragileandys
Posts: 11
Joined: 2012-02-07T10:53:21-07:00
Authentication code: 8675308

Re: piping more than once

Post by fragileandys »

Anthony, thank you very much. And thanks for the links, I had never seen the part on parenthesis.
Post Reply