6

Given these two images of size 512x512 with transparent background:

circle.png

enter image description here

inner.png

enter image description here

I'd like to overlay these two images, so that they appear centered and shrink the image size down to say 64x64. My initial attempt looks like:

convert -background none inner.png circle.png -layers flatten -resize 64x64 output.png

This produces:

enter image description here

From a design perspective, the inner image looks a bit too large relatively, so I'd like to scale it down a little bit. And despite being technically centered, I'd prefer to move it a little bit to the right, but without increasing the total width of the image. I think this requires to use -geometry. I have tried many combinations, but I never get the desired effect. For instance

convert -background none inner.png -geometry 256x256 circle.png -layers flatten -resize 64x64 output.png

produces

enter image description here

In a sense the inner image has become smaller relatively, but not in the way I expected.

Any ideas how this can be accomplished with imagemagick?

bluenote10
  • 225
  • 1
  • 2
  • 10
  • Is there any particular reason you wouldn't composite these in Pshop/Gimp first, then export at the required size as a single image? – Tetsujin Aug 28 '20 at 08:08
  • 1
    @Tetsujin I have to do that with many icons and want to automate the process. E.g. I may want to redesign the outer image later. Also I figured I should finally get a better understanding of imagemagick ;). – bluenote10 Aug 28 '20 at 08:12

1 Answers1

7

You may wish to try something like the following:

magick convert circle.png inner.png -gravity Center -geometry 256x256+30+5 -composite -resize 64x64 output.png

Where:

  • -gravity Center tells ImageMagick to center any "subimage" (inner.png) relative to the background image (circle.png).

  • -geometry 256x256+30+5 resizes inner.png to 256x256 then offsets its position 30 pixels horizontally to the right (+) and 5 pixels vertically down (+). This offset is considered to be relative to its current position (affected by ‑gravity, above).

  • -composite take the first image (circle.png) and overlays the second image (inner.png) according to the current -compose setting (which defaults to a value of Over). The location of the overlaid image (inner.png) is controlled by the -gravity and -geometry settings.

Results

ex. Final Composite Image (-resize 256x256 output.png)

Composite Play Button

Anaksunaman
  • 17,239
  • A perfect answer, thanks! What still surprises me is that swapping -gravity and -geometry behaves the same. – bluenote10 Aug 30 '20 at 07:28
  • 1
    Thanks so much! For me it worked when I removed the keyword magick in the beginning, so the command in my case was directly starting with convert. – Johannes Lemonde Dec 05 '20 at 18:24
  • @bluenote10 Your welcome. Some arguments are positional but many aren't. – Anaksunaman Dec 05 '20 at 23:27
  • @JohannesLemonde Not a problem. But to your comment, using the magick keyword depends on your installation of ImageMagick (newer installations i.e. v7+ are more likely to use it/have it available). – Anaksunaman Dec 05 '20 at 23:30