Specifying geometry in terms of output image.

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
User avatar
rossmcm
Posts: 28
Joined: 2012-07-26T06:10:38-07:00
Authentication code: 15
Location: Auckland, NZ

Specifying geometry in terms of output image.

Post by rossmcm »

I want to combine various images where I don't necessarily know their size, but I know what I size I want the output image to be, and I know what proportion of the output size I want any respective input image to occupy.

An example will (hopefully) make it clearer.

I have three images red.png, blue.png, green.png: Image Image Image

and I want to combine them to give this result: Image

The yellow borders are not part of the images, but are there to indicate the dimensions of each of the images (note that the output image does not extend all the way to the edges). Note also that the input image relative sizes are in differing proportions to their sizes in the output image.

So in this example, my requirements would be:
  • Output image size is to be 100x100
    Red.png is to be sized so that it is centred (i.e. so that the centre of the input image is positioned at +50, +50 in the output) and occupies 70 pixels (i.e. 15 pixels clear to the border)
    Blue.png is to be positioned so the centre is at +70, +70, and sized so that the circle is 43 pixels in diameter.
    Green.png is to be positioned so the centre is at +70, +45, and sized so that the circle is 35 pixels in diameter.
The important thing here is that everything is being specified in terms of the output image. I realise that it might be a tall order to be able to specify things in terms of where I want the centre of the image to be located, as opposed to the northeast corner. In my (brief) experiments to date, I have had difficulty with:
  • keeping the sizing of each of the input component layers from interacting with each other.
    forcing the output image to be a particular fixed size.
    dealing with arbitrary input image sizes
so I figure that the answer to this example will show me some of these things. Also of interest to be is how I might apply partial transparency to one layer (as shown in the green circle).
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Specifying geometry in terms of output image.

Post by fmw42 »

resize each image to the desired size, the use a series of composites with -gravity center -geometry +X+Y where X and Y are relative to the center of the output image. see http://www.imagemagick.org/Usage/layers/#convert

You can do the resize and composite all in one command if you do the resize of each image in parenthesis. see http://www.imagemagick.org/Usage/basics/#parenthesis
User avatar
rossmcm
Posts: 28
Joined: 2012-07-26T06:10:38-07:00
Authentication code: 15
Location: Auckland, NZ

Re: Specifying geometry in terms of output image.

Post by rossmcm »

Thanks, that got me most of the way. I ended up with this:

Code: Select all

convert ^
 -size 100x100 xc:none ^
 ^( red.png   -gravity center    -geometry 75x75+00+00 ^) -composite ^
 ^( blue.png  -gravity center    -geometry 45x45+20+20 ^) -composite ^
 ^( green.png -gravity center    -geometry 35x35+30+00 -alpha set -channel A -evaluate set 70%% ^) -composite ^
 output2.png
(excuse the carets - windows version of batch file continuation linebreak escape characters) but I still can't get partial transparency on the green.png working properly. I get an image like this:
Image

How do I stop the transparent background of green.png from punching out the layers underneath?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Specifying geometry in terms of output image.

Post by fmw42 »

Code: Select all

convert ^
 -size 100x100 xc:none ^
 ^( red.png   -gravity center    -geometry 75x75+00+00 ^) -composite ^
 ^( blue.png  -gravity center    -geometry 45x45+20+20 ^) -composite ^
 ^( green.png -gravity center    -geometry 35x35+30+00 -alpha set -channel A -evaluate set 70%% ^) -composite ^
 output2.png
In windows, you don't need to escape either parens ( ). Also see the +channel

try

convert ^
-size 100x100 xc:none ^
( red.png -gravity center -geometry 75x75+00+00 ) -composite ^
( blue.png -gravity center -geometry 45x45+20+20 ) -composite ^
( green.png -gravity center -geometry 35x35+30+00 -alpha set -channel A -evaluate set 70%% +channel ) -composite ^
output2.png

I can never remember if -channel A -evaluate set 70%% will be 70% transparent or 70% opaque. So if this does not work correctly, try the complement of 30%%.

Note: If you already have transparency, then using -alpha set will turn off existing transparency and set the whole image to 70%. Is that what you want?

If you just want to reduce the opaque part and not reset the transparent part, use

-alpha on -channel A -evaluate multiply 0.7 +channel
User avatar
rossmcm
Posts: 28
Joined: 2012-07-26T06:10:38-07:00
Authentication code: 15
Location: Auckland, NZ

Re: Specifying geometry in terms of output image.

Post by rossmcm »

Success! This command:

Code: Select all

convert ^
 -size 100x100 xc:none ^
 ^( red.png   -gravity center    -geometry 75x75+00+00 ^) -composite ^
 ^( blue.png  -gravity center    -geometry 45x45+20+20 ^) -composite ^
 ^( green.png -gravity center    -geometry 35x35+30+00 ^
    -alpha on -channel A -evaluate multiply 0.3 +channel ^) -composite ^
 output3.png
gives this result: Image which is what I was after.

It appears that the multiply factor sets the opacity, not the transparency, so a lower value makes the layer "fainter".

I got into the habit of escaping my "("s because you need to do that if you want to pass then in a parameter when calling another batch file.

Anyway, I appreciate the help. IM is one of those things I wish I had known about earlier in life. Sigh.
Post Reply