I have a set of images, and I can use the Imagemagick montage
command on them to produce a montage image file with transparent background (let's call this fgimg
). Now I have another existing image (let's call this bgimg
) that I'd like to use (after some special processing with the convert
command) as the background for fgimg
, which can be achieved within the same convert
command. At this point it seems trivial to avoid writing the temporary fgimg
to disk, simply by piping the standard output of montage
to the standard input of convert
.
My problem is that the special processing I'm applying to bgimg
will require some knowledge of the image properties of fgimg
(e.g., resizing bgimg
to have the same size as fgimg
), which I don't know in advance. How can this information be retrieved and used in the convert
command?
Note: I'm using Imagemagick version 6.9.7-4 on Linux.
I'll include some commands below to further illustrate the problem in detail.
The following command produces the montage image fgimg
from a set of input images. The output is in the 'special' miff
format (which seems best for temporary output to be worked on later), and has transparent background so that the actual background can be applied later. Most of the other options here are not important, but the point is that the output size (dimensions) cannot be determined in advance.
montage input_*.jpg -tile 5x -border 2 -geometry '200x200>+20+20' \
-gravity center -set label '%f\n%G' -background none -fill white \
-title 'Sample Title' miff:fgimg
Next, I have another input image bgimg.jpg
. I want to perform some processing on it before using it as background to fgimg
. The processing can be quite complex in general, but in this example, I want to:
- resize
bgimg.jpg
to fit inside the dimensions offgimg
without any cropping; - apply a fade-to-black effect around the edges;
- make it the same size as
fgimg
, with a black background; - combine this with
fgimg
to produce the final output.
Notice that I need the size of fgimg
in two places. I can first extract this into a shell variable:
size=$(identify -format '%G' miff:fgimg)
Then I can do all the steps above in one convert
command (note that $size
is used twice):
convert "bgimg.jpg[$size]" -gravity center \
\( +clone -fill white -colorize 100% -bordercolor black \
-shave 20 -border 20 -blur 0x20 \) -compose multiply -composite \
-background black -compose copy -extent $size \
miff:fgimg -compose over -composite final_out.jpg
Now here is the problem: I want to avoid writing the temporary file fgimg
to disk.
I could replace miff:fgimg
with miff:-
in both the montage
and convert
commands and then just pipe one to the other: montage ... | convert ...
. But how do I deal with the $size
?
I tried to use file descriptors (miff:fd:3
) but this does not seem to work, which is confirmed by the comments to this question.
Is there a way to do this (in Imagemagick v6) without creating a temporary file?