Composite 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
Arty
Posts: 3
Joined: 2016-04-19T01:53:06-07:00
Authentication code: 1151

Composite image

Post by Arty »

I'm trying to create a simple sprite from 3 images using command line. I resize each image, then splice to move it to specific coordinates and composite to merge them.

This code works

Code: Select all

convert -background transparent \
    forum_link.png -resize '50x50!' -splice 50x0 -extent 200x100 \
    \( forum_read.png -resize '50x50!' -splice 150x0 \
        \( forum_page.png -resize '50x50!' -splice 100x0 \) -composite \
    \) -composite \
    test1.png
However this code doesn't:

Code: Select all

convert -background transparent \
    forum_link.png -resize '50x50!' -splice 500x0 -extent 600x500 \
    \( forum_read.png -resize '50x50!' -splice 500x50 \
        \( forum_page.png -resize '50x50!' -splice 500x100 \) -composite \
    \) -composite \
    test2.png
I don't understand why second code doesn't work. Its identical to first one, except for values for -extent and -splice. What am I doing wrong?

These are images I'm trying to join:

forum_read.png: Image
forum_link.png: Image
forum_page.png: Image

This is result of first command:
Image

This is result of second command:
Image
Notice missing icon_page.png
Arty
Posts: 3
Joined: 2016-04-19T01:53:06-07:00
Authentication code: 1151

Re: Composite image

Post by Arty »

After more testing it appears using vertical value above 50 for -splice for forum_page.png starts to cut off bottom part of image, like canvas height is only 100px.

Solved issue by adding -extent to each image to match it with canvas dimensions:

Code: Select all

convert -background transparent \
    forum_link.png -resize '50x50!' -splice 500x0 -extent 600x500 \
    \( forum_read.png -resize '50x50!' -splice 500x50 -extent 600x500 \
        \( forum_page.png -resize '50x50!' -splice 500x100 -extent 600x500 \) -composite \
    \) -composite \
    test2.png
But I don't understand why it was needed. Why was canvas for forum_page.png 100px tall?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Composite image

Post by snibgo »

When you composite two images, the result image will be the size of the first image.

In your second command, you composite the read and page images. When you composite, the sizes are 550x100 and 550x150. The result will be 550x100. But the page image is mostly transparent, with the icon in the bottom-right corner. So the icon doesn't appear in the output.
snibgo's IM pages: im.snibgo.com
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Composite image

Post by GeeMack »

Arty wrote:I don't understand why second code doesn't work. Its identical to first one, except for values for -extent and -splice. What am I doing wrong?
As snibgo explained, at the point the first composite was made, your "forum_page.png" was larger than "forum_read.png". You composited the larger image over the smaller one, so the result was the size of the smaller image.

There are many ways to approach any given problem with ImageMagick. You might consider a slightly different approach to the whole thing to make the code simpler to understand and troubleshoot. Consider something like this...

Code: Select all

convert -background transparent \
   forum_link.png forum_read.png forum_page.png -resize '50x50!' -append \
   -gravity northeast -extent 550x500 -gravity northwest -extent 600x500 test3a.png
That brings in all your images, resizes them to 50x50, and appends them vertically. At that point you're mostly done. Then with "-gravity" and "-extent" (or even "-splice") you can tweak the finished size of the canvas.

It could also be done by building a background canvas of your finished size, appending the three small images into one, and compositing that over the transparent canvas at the place you want them to be. Something like this...

Code: Select all

convert -size 600x500 xc:transparent \
   \( forum_link.png forum_read.png forum_page.png -resize '50x50!' -append \) \
   -gravity northeast -geometry +50+0 -composite test3b.png
That gets you away from those sort of nested composites, which was where you ran into an issue the first time.
Arty
Posts: 3
Joined: 2016-04-19T01:53:06-07:00
Authentication code: 1151

Re: Composite image

Post by Arty »

Thanks!
Post Reply