2

I have three images, a.jpg, b.jpg, c.jpg。

I want to concatenate them so they look like the follow:

enter image description here

I want to do this using one command. No tmp files generated. How can I do this using IM/GM.

3 Answers 3

5

Another way in ImageMagick is to use smush rather then append. Smush allows offsets.

Create images:

convert -size 250x250 xc:green green.png
convert -size 250x250 xc:black black.png
convert -size 250x510 xc:red red.png

Now combine them:

convert -background white red.png \
\( green.png black.png -smush 10 \) \
+smush 10 \
result.png

enter image description here

2
  • Nice solution - note though that GrapicsMagick lacks smush operator AFAIK. Jan 5, 2018 at 7:22
  • Yes, that is because GraphicsMagick was an early offshoot of ImageMagick and the former has remain mostly static, while the latter has continued to develop.
    – fmw42
    Jan 5, 2018 at 8:20
2

Assuming the images are all the correct sizes, this is probably easiest:

convert -size 10x10 green.png xc:white black.png -append xc:white red.png -reverse +append result.png

enter image description here

That says... "make the size of the little spacers 10x10. Load the green image, then make a white spacer, then load the black image and append them together vertically. Make another white spacer. Load the red image. Reverse the columns of images so the most recently added red column is at the left instead of the right, append the images side-by-side."

I did it this way round (starting with the right side) because GraphicsMagick doesn't offer parentheses.


If the images are not already suitably sized, you would be looking at something more like this - still a single command:

convert -size 10x10                 \
    \( green.png -resize somehow \) \
    xc:white                        \
    \( black.png -resize somehow \) \
    -append                         \
    xc:white                        \
    \( red.png -resize somehow   \) \
    -reverse +append result.png
7
  • I had not heard of -reverse before @Mark Setchell
    – Bonzo
    Jan 3, 2018 at 8:33
  • @Bonzo It's pretty handy when you need to build things in the wrong order, or you want to append images right-to-left or bottom-to-top when the +append and -append naturally go left-to-right and top-to-bottom! If you see what I mean ;-) You can also achieve the same effect using -swap multiple times. Jan 3, 2018 at 8:37
  • Thinks for you answer. But if all images size are same. How to do?
    – XGHeaven
    Jan 3, 2018 at 9:57
  • That's what the second part shows you. Change the part that says red.png -resize somehow to red.png -resize WxH\! where W is the width you want and H is the height you want - presumably the sum of the heights of the green and black plus the 10px spacer. Jan 3, 2018 at 11:52
  • Thanks. And one more question. What's meaning of xc:white?
    – XGHeaven
    Jan 4, 2018 at 1:37
2

One other way to do this in ImageMagick is just to composite the 3 images to the proper corners of a white background image.

Create images:

convert -size 250x250 xc:green green.png
convert -size 250x250 xc:black black.png
convert -size 250x510 xc:red red.png

Process:

convert -size 510x510 xc:white \
red.png -gravity northwest -composite \
green.png -gravity northeast -composite \
black.png -gravity southeast -composite \
result.png

enter image description here

1
  • ImageMagick is such a powerful tool - it allows you to tackle a problem from so many different angles - I love it. Nice approach! Jan 4, 2018 at 7:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.