Page 1 of 1
Composite image
Posted: 2016-04-19T02:03:03-07:00
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:
forum_link.png:
forum_page.png:
This is result of first command:
This is result of second command:
Notice missing icon_page.png
Re: Composite image
Posted: 2016-04-19T02:50:45-07:00
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?
Re: Composite image
Posted: 2016-04-19T06:01:54-07:00
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.
Re: Composite image
Posted: 2016-04-19T06:28:52-07:00
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.
Re: Composite image
Posted: 2016-04-19T07:33:47-07:00
by Arty
Thanks!