-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
1736 lines (1717 loc) · 148 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="generator" content="HTML Tidy for HTML5 for Linux version 5.8.0">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="../assets/usage.css">
<title>Layering -- ImageMagick Examples</title>
<link rel="icon" href="../img_www/favicon.ico" type="image/x-icon">
<link rel="shortcut" href="../img_www/favicon.ico" type="image/x-icon">
<link rel="canonical" href="https://imagemagick.org/Usage/layers/">
</head>
<body>
<main class="container">
<div class="magick-template">
<div class="magick-header">
<h1>ImageMagick Examples --<br>
<img src="../img_www/space.gif" width="50" height="1"> Multi-Image Layers</h1>
<div>
<dl>
<dt><b>Index</b></dt>
<dt>
<a href="../"><img src="../img_www/granitesm_left.gif" border="0" width="15" height="15"> ImageMagick Examples Preface and Index</a>
</dt>
<dd>
<a href="#intro"><img src="../img_www/granitesm_right.gif" border="0" width="15" height="15"> Layers Introduction</a>
</dd>
<dd>
<a href="#append"><img src="../img_www/granitesm_right.gif" border="0" width="15" height="15"> Appending Images</a> (-append)
<ul>
<li>
<a href="#append_array">Append an Array of Images</a>
</li>
<li>
<a href="#append_overlap">Append with Overlap</a>
</li>
<li>
<a href="#smush">Smushing Append</a> (-smush)
</li>
</ul>
</dd>
<dd>
<a href="#composition"><img src="../img_www/granitesm_right.gif" border="0" width="15" height="15"> Composition of Multiple Pairs of Images</a>
<ul>
<li>
<a href="#composite">Using Composite Command</a> (composite, -geometry)
</li>
<li>
<a href="#convert">Composite Operator of Convert</a> (-composite, -geometry)
</li>
<li>
<a href="#draw">Draw Multiple Images</a> (-draw 'image ..')
</li>
</ul>
</dd>
<dd>
<a href="#layers"><img src="../img_www/granitesm_right.gif" border="0" width="15" height="15"> Layering Multiple Images</a>
<ul>
<li>
<a href="#flatten">Flatten - onto a Background Image</a>
</li>
<li>
<a href="#mosaic">Mosaics - Canvas Expanding</a>
</li>
<li>
<a href="#merge">Merging - to Create a New Layer Image</a>
</li>
<li>
<a href="#coalesce">Coalesce Composition - a Progressive Layering</a>
</li>
<li>
<a href="#compose">Compose Methods and Layering</a>
</li>
<li>
<a href="#layer_composite">Layers Composite - Merge Two Image Lists</a>
</li>
</ul>
</dd>
<dd>
<a href="#layer_examples"><img src="../img_www/granitesm_right.gif" border="0" width="15" height="15"> Layering Image Examples</a>
<ul>
<li>
<a href="#layer_thumbnails">Layering Thumbnails</a>
</li>
<li>
<a href="#layer_calc">Calculated Positioning of Images</a>
</li>
<li>
<a href="#layer_prog">Two Stage Positioning of Images</a>
</li>
<li>
<a href="#layer_pins">Pins in a Map</a>
</li>
<li>
<a href="#layer_shadow">Layers of Shadows</a>
</li>
<li>
<a href="#layer_distort">Distorted Image Placement using Layers</a>
</li>
</ul>
</dd>
<dd><br></dd>
<dd>
<a href="#evaluate-sequence"><img src="../img_www/granitesm_right.gif" border="0" width="15" height="15"> Evaluate Sequence Multi-Image Merging</a>
<dl>
<dd>
<a href="#eval-seq_mean">Mean (average)</a>, <a href="#eval-seq_max">Min/Max Value</a>, <a href="#eval-seq_median">Median Pixel</a>, <a href="#eval-seq_add">Add</a>, <a href="#eval-seq_multiply">Multiply</a>
</dd>
</dl>
</dd>
<dd>
<a href="#poly"><img src="../img_www/granitesm_right.gif" border="0" width="15" height="15"> Poly - Merge Mutli-images Using a Polynomial</a>
</dd>
</dl>Overlaying multiple images onto each other to generate a larger 'composite' is generally known as using image 'layering'. These examples involve the combining of multiple 'layers' of images to produce the final larger more complex image.
<hr>
<!-- ---------------------------------------------------------------- -->
<a name="intro" id="intro"></a>
<h2>Layering Images Introduction</h2>As we have previously noted, ImageMagick does not deal with just one image, but a sequence or list of images. This allows you to use IM in two very special image processing techniques. You can for example think of each image in the list as a single frame in time, so that the whole list can be regarded as being a <i>Animation</i>. This will be explored in other IM Example Pages. See <a href="../anim_basics/">Animation Basics</a>. Alternatively, you can think of each image in the sequence as <i>Layers</i> of a set of see-through overhead transparencies. That is, each image represents a small part of the final image. For example: the first (lowest) layer can represent a background image. Above that you can have a fuzzy see though shadow. Then the next layer image contains the object that casts that shadow. On top of this a layer with some text that is written over that object. That is, you can have a sequence of images or 'layers' that each adds one more piece to a much more complex image. Each image layer can be moved, edited, or modified completely separately from any other layer, and even saved into a multi-image file (such as TIFF:, MIFF: or XCF:) or as separate images, for future processing. And that is the point of image layering. Only when all the image layers have been created do you <a href="#flatten">Flatten</a>, <a href="#mosaic">Mosaic</a>, or <a href="#merge">Merge</a> all the <a href="#example">Layered Images</a> into a single final image.
<hr>
<!-- ---------------------------------------------------------------- -->
<a name="append" id="append"></a>
<h2>Appending Images</h2>Appending is probably the simplest, of the multi-image operations provided to handle multiple images. Basically it joins the current sequence of images in memory into a column, or a row, without gaps. The "<code><a href="https://imagemagick.org/script/command-line-options.php?#append">-append</a></code>" option appends vertically, while the plus form "<code><a href="https://imagemagick.org/script/command-line-options.php?#append">+append</a></code>" appends horizontally. For example, here we append a set of letter images together, side-by-side, to form a fancy word, in a similar way that individual 'glyphs' or letters of a 'font', are joined together.
<div align="center">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="90%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick font_A.gif font_P.gif font_P.gif font_E.gif font_N.gif \
font_D.gif font_E.gif font_D.gif +append append_row.gif
</samp></pre>
</td>
</tr>
</table><a href="append_row.gif"><img src="append_row.gif" align="middle" vspace="5" hspace="5" border="0" alt="[IM Output]"></a>
</div>The above is similar (in a very basic way) to how fonts are handled. Unlike real fonts you are not limited to just two colors, but can generate some very fancy colorful alphabets from individual character images. Many of these 'image fonts' are available on the WWW for download. A very small set can be found in <a href="http://www.ict.griffith.edu.au/anthony/icons/">Anthony's Icon Library</a>, in <a href="http://www.ict.griffith.edu.au/anthony/icons/prog/fonts/Icons.html">Fonts for Text and Counters</a>, which is also where I found the above <a href="http://www.ict.griffith.edu.au/anthony/icons/prog/fonts/bubble_blue.xpm">Blue Bubble Font</a>. Note also how the "<code><a href="https://imagemagick.org/script/command-line-options.php?#append">+append</a></code>" operator was done as the last operation, after all the images that you want to append have been added to the current image sequence. This is great for appending a label to an image, for example...
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td width="100%" align="justify">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick rose: -background LawnGreen label:Rose \
-background white -append append_label.jpg
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="append_label.jpg"><img src="append_label.jpg" align="middle" vspace="0" hspace="5" border="1" alt="[IM Output]"></a>
</td>
</tr>
</table>Note that the "<code><a href="https://imagemagick.org/script/command-line-options.php?#background">-background</a></code>" color was used to fill in any space that was not filled in. Of course if the all the images are the same width, no space will be left for this fill. From IM v6.4.7-1 the "<code><a href="https://imagemagick.org/script/command-line-options.php?#gravity">-gravity</a></code>" setting can be used to specify how the images should be added together. As such in a vertical append, a setting of '<code>Center</code>' will center the image relative to the final resulting image (so will a setting of either '<code>North</code>' or '<code>South</code>').
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td width="100%" align="justify">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick rose: -background LawnGreen label:Rose \
-background white -gravity center -append \
append_center.jpg
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="append_center.jpg"><img src="append_center.jpg" align="middle" vspace="0" hspace="5" border="1" alt="[IM Output]"></a>
</td>
</tr>
</table>Naturally any '<code>East</code>' gravity setting will align the images on the right side.
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td width="100%" align="justify">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick rose: -background LawnGreen label:Rose \
-background white -gravity east -append \
append_east.jpg
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="append_east.jpg"><img src="append_east.jpg" align="middle" vspace="0" hspace="5" border="1" alt="[IM Output]"></a>
</td>
</tr>
</table>Similar vertical alignment can be achieved when using "<code><a href="https://imagemagick.org/script/command-line-options.php?#append">+append</a></code>"
<table border="0" cellspacing="0" cellpadding="0" width="90%" align="center">
<tr valign="top">
<td><img src="../img_www/warning.gif" width="28" height="28"><img src="../img_www/space.gif" width="12" height="16"></td>
<td align="justify" width="100%">
<font size="-1"><i>Before IM v6.4.7 it was much more difficult to align appended images, and generally involved using a "<code><a href="https://imagemagick.org/script/command-line-options.php?#flop">-flop</a></code>" for right alignment. Or using "<code><a href="https://imagemagick.org/script/command-line-options.php?#extent">-extent</a></code>" or "<code><a href="https://imagemagick.org/script/command-line-options.php?#border">-border</a></code>" to adjust the image width for centered aligned appends.<br>
<br>
For example, this will work with an older 6.3.2 version of IM...</i></font>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td width="100%" align="justify">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick rose: -background SkyBlue label:Rose \
-background White -gravity center -extent 200x \
-append -trim +repage append_center_old.jpg
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="append_center_old.jpg"><img src="append_center_old.jpg" align="middle" vspace="0" hspace="5" border="1" alt="[IM Output]"></a>
</td>
</tr>
</table>
</td>
</tr>
</table>You can also use multiple append operations, in the same command without conflict or confusion as to the outcome of the operations (which was not the case before IM v6).
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr valign="top">
<td width="100%" align="justify">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<!-- <CODE EXECUTE>
magick $HOME/icons/dragons/dragon_long.xpm dragon_long.gif
</CODE> -->
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick font_{0,0,6,1,2}.gif +append dragon_long.gif \
-background none -append append_multi.gif
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="append_multi.gif"><img src="append_multi.gif" align="middle" vspace="0" hspace="5" border="0" alt="[IM Output]"></a>
</td>
</tr>
</table>We appended each row of images together, then appende a larger image below that. This is very simple, and straight-forward. By using <a href="../basics/#parenthesis">parenthesis</a>, you can append just the numbers after the larger image. For example, here append all the numbers together, before appending them vertically to the dragon image we read in before the numbers.
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr valign="top">
<td width="100%" align="justify">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick dragon_long.gif '(' font_{0,0,6,2,9}.gif +append ')' \
-background none -append append_parenthesis.gif
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="append_parenthesis.gif"><img src="append_parenthesis.gif" align="middle" vspace="0" hspace="5" border="0" alt="[IM Output]"></a>
</td>
</tr>
</table>
<table border="0" cellspacing="0" cellpadding="0" width="90%" align="center">
<tr valign="top">
<td><img src="../img_www/reminder.gif" width="20" height="16"><img src="../img_www/space.gif" width="20" height="16"></td>
<td align="justify" width="100%"><font size="-1"><i>The parenthesis in the above must be either quoted, or escaped with a backslashed ('<code>\</code>') when used with an UNIX shell, otherwise they will be interpreted by the shell as something completely different.</i></font></td>
</tr>
</table>
<table border="0" cellspacing="0" cellpadding="0" width="90%" align="center">
<tr valign="top">
<td><img src="../img_www/reminder.gif" width="20" height="16"><img src="../img_www/space.gif" width="20" height="16"></td>
<td align="justify" width="100%"><font size="-1"><i>As only two images were involved we could have just used "<code><a href="https://imagemagick.org/script/command-line-options.php?#swap">+swap</a></code>" or "<code><a href="https://imagemagick.org/script/command-line-options.php?#reverse">-reverse</a></code>" instead of using parenthesis.</i></font></td>
</tr>
</table><a name="append_array" id="append_array"></a>
<h2>Append an Array of Images</h2><a name="append_array" id="append_array"></a> You can take this further to make a whole array of images, and build them either by rows, or by columns.
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr valign="top">
<td width="100%" align="justify">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick \( font_1.gif font_2.gif font_3.gif +append \) \
\( font_4.gif font_5.gif font_6.gif +append \) \
\( font_7.gif font_8.gif font_9.gif +append \) \
\( -size 32x32 xc:none font_0.gif +append \) \
-background none -append append_array.gif
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="append_array.gif"><img src="append_array.gif" align="middle" vspace="0" hspace="5" border="0" alt="[IM Output]"></a>
</td>
</tr>
</table>Technically the first set of parenthesis is not needed, as no images have been read in yet, but it makes the whole thing look uniform and shows the intent of the command, in making an array of images. See also <a href="../montage/#concatenate">Montage Concatenation Mode</a>, for an alternative way of creating arrays of equal sized images.
<table border="0" cellspacing="0" cellpadding="0" width="90%" align="center">
<tr valign="top">
<td><img src="../img_www/expert.gif" width="23" height="26"><img src="../img_www/space.gif" width="17" height="16"></td>
<td align="justify" width="100%"><font size="-1"><i>The "<code><a href="https://imagemagick.org/script/command-line-options.php?#append">-append</a></code>" operator will only append the actual images, and does not make use the virtual canvas (image page) size, or the image offset. However the virtual canvas information seems to be left in a funny state with the canvas sizes being added together and the offset set to some undefined value.<br>
<br>
This may be regarded as a bug, and means either the input images or result should have the virtual canvas reset using "<code><a href="https://imagemagick.org/script/command-line-options.php?#repage">+repage</a></code>", before saving, or using the image in operations where this information can become important.<br>
<br>
This situation will probably be fixed in some future expansion of the operation. Caution is thus advised, especially if re-appending <a href="../crop/#crop_tile">Tile Cropped</a> images.</i></font></td>
</tr>
</table><a name="append_overlap" id="append_overlap"></a>
<h2>Append with Overlap</h2>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr valign="top">
<td width="100%" align="justify">
On the IM Forum an user asked for a simple way to <a href="https://magick.imagemagick.org/viewtopic.php?t=30720">Append images with some overlap</a>. Many solutions were offered. This was one of the simplest solutions, with the amount of overlap given in a single location.
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick granite: rose: -gravity east -background none \
\( -clone 1 -chop 30x0 \) \( -clone 0,2 +append \) \
-delete 0,2 +swap -composite append_overlap.gif
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="append_overlap.gif"><img src="append_overlap.gif" align="middle" vspace="0" hspace="5" border="0" alt="[IM Output]"></a>
</td>
</tr>
</table>The above did not need to any image positioning calculations, typically involving image sizes, that would represent a more general solution. See <a href="#layer_examples">Handling Image Layers</a> below. What this did was chop off the part that overlapped, before appending the result to the first image, producing the final image size. The original image is then composed (with gravity) on top to generate the actual overlap. It can be easily modified for vertical overlapping, or even right to left overlapping relatively easily. <a name="smush" id="smush"></a>
<h2>Smushing Append</h2>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr valign="top">
<td width="100%" align="justify">
Another way of appending images is by smushing. The "<code><a href="https://imagemagick.org/script/command-line-options.php?#smush">-smush</a></code>" operator works much like the <a href="#addend">Append Operator</a> (see above) does, but it takes an argument of how much space (or anti-space) you want between the images. For example, lets use it to so the previous example more simply.
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick granite: rose: -background none -gravity Center \
+smush -20 smush_overlap.png
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="smush_overlap.png"><img src="smush_overlap.png" align="middle" vspace="0" hspace="5" border="0" alt="[IM Output]"></a>
</td>
</tr>
</table>That works very well, though that is not what the operator is actually designed for, and it is probably a lot slower. What smush actually is ment to do is move 'shaped images' as close togther as posible. For example, here I generate the letters '<code>A</code>' and '<code>V</code>' and 'smush' them together with as little space between them as posible.
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr valign="top">
<td width="100%" align="justify">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick -background none -pointsize 72 \
-fill red label:A -fill blue label:V \
+smush 0 smush_append.png
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="smush_append.png"><img src="smush_append.png" align="middle" vspace="0" hspace="5" border="0" alt="[IM Output]"></a>
</td>
</tr>
</table>Notice that how the two letters were appended together far closer than append would, taking advantage of the empty space of the images 'shape'. The gap in the above is caused by anti-aliasing edge pixels of the two letters. That is, what "<code><a href="https://imagemagick.org/script/command-line-options.php?#smush">-smush</a></code>" is designed to do, though it requires a lot of calculations, so is a lot slower than <a href="#addend">Append</a> (see above). The argument, is an offset for that final position, and is usually a positive value to generate a gap, but can be negative to create an overlap.
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr valign="top">
<td width="100%" align="justify">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick -background none -pointsize 72 \
-fill red label:A -fill blue label:V \
+smush -15 smush_offset.png
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="smush_offset.png"><img src="smush_offset.png" align="middle" vspace="0" hspace="5" border="0" alt="[IM Output]"></a>
</td>
</tr>
</table>Images may become clipped in undocumented ways if a very large negative value is used.
<hr>
<!-- ---------------------------------------------------------------- -->
<a name="composition" id="composition"></a>
<h2>Composition of Multiple Pairs of Images</h2>Composition is the low-level operation that is used to merge two individual images together. Almost all layering techniques eventually devolve down to merging images together two at a time, until only one image is left. So lets start by looking at ways of doing low-level composition of image pairs. <a name="composite" id="composite"></a>
<h3>Using the Composite Command</h3>The traditional method of combining two images together using ImageMagick is though the "<code>magick composite</code>" command. This command can only combine only two images at a time, saving the results of each operation into a file. This of course does not stop you from using it to layer multiple images, one image at a time...
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td width="100%" align="justify">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre><code execute="" nowarn="">
magick -size 100x100 xc:skyblue composite.gif
magick composite -geometry +5+10 balloon.gif composite.gif composite.gif
magick composite -geometry +35+30 medical.gif composite.gif composite.gif
magick composite -geometry +62+50 present.gif composite.gif composite.gif
magick composite -geometry +10+55 shading.gif composite.gif composite.gif
</code></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="composite.gif"><img src="composite.gif" width="100" height="100" align="middle" vspace="0" hspace="5" border="1" alt="[IM Output]"></a>
</td>
</tr>
</table>
<table border="0" cellspacing="0" cellpadding="0" width="90%" align="center">
<tr valign="top">
<td><img src="../img_www/reminder.gif" width="20" height="16"><img src="../img_www/space.gif" width="20" height="16"></td>
<td align="justify" width="100%"><font size="-1"><i>As all input images are read in by ImageMagick BEFORE the output image is opened, you can output to one of the input images. This allows you to work on the same image over and over, as shown above, without problems.<br>
<br>
Do not do this with a lossy image format like "JPEG" as the format errors are accumulative, and the base image will quickly degrade.</i></font></td>
</tr>
</table>You can also resize the overlaid image as well as position it using the "<code><a href="https://imagemagick.org/script/command-line-options.php?#geometry">-geometry</a></code>" setting.
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td width="100%" align="justify">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre><code execute="" nowarn="">
magick -size 100x100 xc:skyblue comp_resize.gif
magick composite -geometry 40x40+5+10 balloon.gif comp_resize.gif comp_resize.gif
magick composite -geometry +35+30 medical.gif comp_resize.gif comp_resize.gif
magick composite -geometry 24x24+62+50 present.gif comp_resize.gif comp_resize.gif
magick composite -geometry 16x16+10+55 shading.gif comp_resize.gif comp_resize.gif
</code></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="comp_resize.gif"><img src="comp_resize.gif" width="100" height="100" align="middle" vspace="0" hspace="5" border="1" alt="[IM Output]"></a>
</td>
</tr>
</table>The "<code>magick composite</code>" command also has a few other advantages in that you can use to control the way the image is drawn onto the background with the "<code><a href="https://imagemagick.org/script/command-line-options.php?#compose">-compose</a></code>" option and its relative position is effected by the "<code><a href="https://imagemagick.org/script/command-line-options.php?#gravity">-gravity</a></code>" setting. You can also "<code><a href="https://imagemagick.org/script/command-line-options.php?#tile">-tile</a></code>" the overlay so that it will just cover the background image, without needing to specify tile limits. This is something only available when using "<code>magick composite</code>". The big disadvantage with this method is that you are using multiple commands, and IM has to write-out the working image, either to a pipeline, or to disk, for the next command to read-in again. To find more examples of using the "<code>magick composite</code>" command, to overlay images on top of other images, see "<a href="../annotating/#overlay">Annotating by Overlaying Images</a>" and "<a href="../annotating/#image_gravity">Image Positioning using Gravity</a>". <a name="magick" id="magick"></a>
<h3>Composite Operator of Convert</h3>The "<code><a href="https://imagemagick.org/script/command-line-options.php?#composite">-composite</a></code>" operator is available within the "<code>magick</code>" command. For more details see <a href="../compose/#composite">Image Composition in IM</a>. This allows you to do the same as the above, but all in one command.
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td width="100%" align="justify">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick -size 100x100 xc:skyblue \
balloon.gif -geometry +5+10 -composite \
medical.gif -geometry +35+30 -composite \
present.gif -geometry +62+50 -composite \
shading.gif -geometry +10+55 -composite \
compose.gif
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="compose.gif"><img src="compose.gif" width="100" height="100" align="middle" vspace="0" hspace="5" border="1" alt="[IM Output]"></a>
</td>
</tr>
</table>This first creates a <a href="../canvas/#solid">Canvas Image</a> which is "<code>skyblue</code>" in color, and then layers each of the later images onto that canvas at the given locations. Now the "<code><a href="https://imagemagick.org/script/command-line-options.php?#geometry">-geometry</a></code>" is is a very special operator that not only sets an overlay position for the next "<code><a href="https://imagemagick.org/script/command-line-options.php?#composite">-composite</a></code>" operation, it will also "<code><a href="https://imagemagick.org/script/command-line-options.php?#resize">-resize</a></code>" the <i>last</i> image (and only the last image) in the current image sequence.
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td width="100%" align="justify">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick -size 100x100 xc:skyblue \
balloon.gif -geometry 40x40+5+10 -composite \
medical.gif -geometry +35+30 -composite \
present.gif -geometry 24x24+62+50 -composite \
shading.gif -geometry 16x16+10+55 -composite \
compose_geometry.gif
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="compose_geometry.gif"><img src="compose_geometry.gif" width="100" height="100" align="middle" vspace="0" hspace="5" border="1" alt="[IM Output]"></a>
</td>
</tr>
</table>Note it is recommended that you avoid this 'resize' side-effect of of the "<code><a href="https://imagemagick.org/script/command-line-options.php?#geometry">-geometry</a></code>", even if it is convenient. Basically as it is more of a backward compatibility effect and may in some situations generate other effects. Here is the more verbose recommendation...
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td width="100%" align="justify">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick -size 100x100 xc:skyblue \
\( balloon.gif -resize 40x40 \) -geometry +5+10 -composite \
\( medical.gif \) -geometry +35+30 -composite \
\( present.gif -resize 24x24 \) -geometry +62+50 -composite \
\( shading.gif -resize 16x16 \) -geometry +10+55 -composite \
compose_resize.gif
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="compose_resize.gif"><img src="compose_resize.gif" width="100" height="100" align="middle" vspace="0" hspace="5" border="1" alt="[IM Output]"></a>
</td>
</tr>
</table><a name="draw" id="draw"></a>
<h3>Draw Multiple Images</h3>Also using "<code>magick</code>" you can also use <a href="../draw/#primitive">Draw Primitives</a> to overlay images onto its working canvas.
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td width="100%" align="justify">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick -size 100x100 xc:skyblue \
-draw "image over 5,10 0,0 'balloon.gif'" \
-draw "image over 35,30 0,0 'medical.gif'" \
-draw "image over 62,50 0,0 'present.gif'" \
-draw "image over 10,55 0,0 'shading.gif'" \
drawn.gif
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="drawn.gif"><img src="drawn.gif" width="100" height="100" align="middle" vspace="0" hspace="5" border="1" alt="[IM Output]"></a>
</td>
</tr>
</table>You can of course also specify a resize for the overlaid image too..
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td width="100%" align="justify">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick -size 100x100 xc:skyblue \
-draw "image over 5,10 40,40 'balloon.gif'" \
-draw "image over 35,30 0,0 'medical.gif'" \
-draw "image over 62,50 24,24 'present.gif'" \
-draw "image over 10,55 16,16 'shading.gif'" \
drawn_resize.gif
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="drawn_resize.gif"><img src="drawn_resize.gif" width="100" height="100" align="middle" vspace="0" hspace="5" border="1" alt="[IM Output]"></a>
</td>
</tr>
</table>The 'drawn' images can also be <a href="../draw/#transform">Rotated, Scaled, and Affine Distorted</a> during the overlay process. Though that can be tricky to get working the way you want. Drawn images are "<code><a href="https://imagemagick.org/script/command-line-options.php?#gravity">-gravity</a></code>" effected, just like text.
<hr>
<!-- ---------------------------------------------------------------- -->
<a name="layers" id="layers"></a>
<h2>Layering Multiple Images</h2>True layering of images requires methods to combine multiple images together, without needing to individually compose each pair of images separately. This is where the various <code><a href="https://imagemagick.org/script/command-line-options.php?#layers">-layers</a></code> operator methods come into their own. Ordering of layered images can be important, so it is a good idea to understand the special <a href="../basics/#image_seq">Image Sequence or List Operators</a>. Note that 'layered images' is practically identical to the handling 'animated frames'. As such it is recommended you also look at both <a href="../anim_basics/">Animation Basics</a> and <a href="../anim_mods/">Animation Modifications</a> for techniques involving processing individual 'layers' or 'frames'. Actually animations often use the same <code><a href="https://imagemagick.org/script/command-line-options.php?#layers">-layers</a></code> operator for processing images. <a name="flatten" id="flatten"></a>
<h3>Flatten - onto a Background Image</h3>The "<code><a href="https://imagemagick.org/script/command-line-options.php?#layers">-layers</a> <b>flatten</b></code>" image list operator, (or its shortcut "<code><a href="https://imagemagick.org/script/command-line-options.php?#flatten">-flatten</a></code>") will basically "<a href="../compose/">Compose</a>" each of the given images on to a background to form one single image. However the image positions are specified using their current <a href="../basics/#page">Virtual Canvas, or Page</a> offset. For example, here I create a nice canvas, and specify each of the images I want to overlay onto that canvas.
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td width="100%" align="justify">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick -size 100x100 xc:skyblue \
-fill dodgerblue -draw 'circle 50,50 15,25' \
\( -page +5+10 balloon.gif \) \( -page +35+30 medical.gif \) \
\( -page +62+50 present.gif \) \( -page +10+55 shading.gif \) \
-layers flatten flatten_canvas.gif
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="flatten_canvas.gif"><img src="flatten_canvas.gif" align="middle" vspace="0" hspace="5" border="1" alt="[IM Output]"></a>
</td>
</tr>
</table>
<table border="0" cellspacing="0" cellpadding="0" width="90%" align="center">
<tr valign="top">
<td><img src="../img_www/warning.gif" width="28" height="28"><img src="../img_www/space.gif" width="12" height="16"></td>
<td align="justify" width="100%"><font size="-1"><i>As of IM v6.3.6-2 the "<code><a href="https://imagemagick.org/script/command-line-options.php?#flatten">-flatten</a></code>" operator is only an alias for a "<code><a href="https://imagemagick.org/script/command-line-options.php?#layers">-layers</a> 'flatten'</code>" method.<br>
<br>
Thus the "<code><a href="https://imagemagick.org/script/command-line-options.php?#flatten">-flatten</a></code>" option can be regarded as a short cut for the "<code><a href="https://imagemagick.org/script/command-line-options.php?#layers">-layers</a></code>" method of the same name.</i></font></td>
</tr>
</table>You don't need to create an initial canvas as we did above, you can instead let "<code><a href="https://imagemagick.org/script/command-line-options.php?#flatten">-flatten</a></code>" create one for you. The canvas color will be the current "<code><a href="https://imagemagick.org/script/command-line-options.php?#background">-background</a></code>" color, while its size is defined by the first images <a href="../basics/#page">Virtual Canvas</a> size.
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td width="100%" align="justify">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick \( -page 100x100+5+10 balloon.gif \) \( -page +35+30 medical.gif \) \
\( --page +62+50 present.gif \) \( -page +10+55 shading.gif \) \
-background dodgerblue -layers flatten flatten_page.gif
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="flatten_page.gif"><img src="flatten_page.gif" width="100" height="100" align="middle" vspace="0" hspace="5" border="1" alt="[IM Output]"></a>
</td>
</tr>
</table>
<table border="0" cellspacing="0" cellpadding="0" width="90%" align="center">
<tr valign="top">
<td><img src="../img_www/reminder.gif" width="20" height="16"><img src="../img_www/space.gif" width="20" height="16"></td>
<td align="justify" width="100%"><font size="-1"><i>While the "<code><a href="https://imagemagick.org/script/command-line-options.php?#gravity">-gravity</a></code>" setting will effect image placement defined using "<code><a href="https://imagemagick.org/script/command-line-options.php?#geometry">-geometry</a></code>" settings, it will not effect image positioning using <a href="../basics/#page">virtual canvas offsets</a> set via the "<code><a href="https://imagemagick.org/script/command-line-options.php?#page">-page</a></code>" setting. This is part of the definition of such offsets. See <a href="../compose/#geometry">Geometry vs Page Offsets</a> for more details.<br>
<br>
If placement with "<code><a href="https://imagemagick.org/script/command-line-options.php?#gravity">-gravity</a></code>" is need look at either the above multi-image composition methods, or the special <a href="../anim_mods/#composite">Layers Composition</a> method that can handle both positioning methods simultaneously.</i></font></td>
</tr>
</table>If any image does not appear in the defined virtual canvas area, it will either be clipped or ignored, as appropriate. For example, here we used a smaller canvas size, causing the later images not to appear completely on that canvas.
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td width="100%" align="justify">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick \( -page 75x75+5+10 balloon.gif \) \( -page +35+30 medical.gif \) \
\( -page +62+50 present.gif \) \( -page +10+55 shading.gif \) \
-background dodgerblue -flatten flatten_bounds.gif
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="flatten_bounds.gif"><img src="flatten_bounds.gif" width="75" height="75" align="middle" vspace="0" hspace="5" border="1" alt="[IM Output]"></a>
</td>
</tr>
</table>The normal use of <a href="#flatten">Flatten</a> is to merge multiple 'layers' of images together. That is, you can be generating various parts of a larger image, usually using <a href="../basics/#parenthesis">Parenthesis</a> to limit image operators to the single 'layer' image being generated, and then flatten the final result together. For example one typical use is to create a <a href="../blur/#shadow">Shadow Image</a> layer, onto which the original image is flattened. For example...
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td width="100%" align="justify">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick balloon.gif \( +clone -background navy -shadow 80x3+5+5 \) +swap \
-background none -flatten flatten_shadow.png
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="flatten_shadow.png"><img src="flatten_shadow.png" align="middle" vspace="0" hspace="5" border="0" alt="[IM Output]"></a>
</td>
</tr>
</table>Note that as I want the shadow under the original image, I needed to <a href="../basics/#swap">swap</a> the two images place them in the right order.
<table border="0" cellspacing="0" cellpadding="0" width="90%" align="center">
<tr valign="top">
<td><img src="../img_www/reminder.gif" width="20" height="16"><img src="../img_www/space.gif" width="20" height="16"></td>
<td align="justify" width="100%"><font size="-1"><i>Using <a href="#flatten">Flatten</a> for adding generated <a href="../blur/#shadow">Shadow Images</a> is not recommended, as generated shadow images can have negative image offsets.<br>
<br>
The recommended solution, as given in the section on <a href="../blur/#shadow">Shadow Images</a>, is to use the more advanced <a href="#merge">Layer Merging</a> technique, we will look at later.</i></font></td>
</tr>
</table>Because the <a href="../basics/#page">Virtual Canvas</a> consists of just a size, the resulting image will be that size, but have no virtual canvas offset, as such you do not need to worry about any offsets present in the final image. This use of the virtual canvas to define the canvas on which to overlay the image means you can use it to add a surrounding border to an image. For example here I set an image's size and virtual offset to 'pad out' an image to a specific size.
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td width="100%" align="justify">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick medical.gif -set page 64x64+20+20 \
-background SkyBlue -flatten flatten_padding.gif
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="flatten_padding.gif"><img src="flatten_padding.gif" width="64" height="64" align="middle" vspace="0" hspace="5" border="1" alt="[IM Output]"></a>
</td>
</tr>
</table>Of course there are better ways to <a href="../thumbnails/#pad">Pad Out an Image</a> so that IM automatically centers the image in the larger area.
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr valign="bottom">
<td width="100%" align="justify">
Strangely the exact same handling can be used to 'clip' or <a href="../crop/#crop">Crop</a> an image to a virtual canvas that is smaller than the original image. In this case however you want to use a negative offset to position the 'crop' location, as you are offsetting the image and not positioning the crop 'window'.
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick logo: -repage 100x100-190-60 -flatten flatten_crop.gif
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="flatten_crop.gif"><img src="flatten_crop.gif" width="100" height="100" align="middle" vspace="0" hspace="5" border="1" alt="[IM Output]"></a>
</td>
</tr>
</table>Of course a <a href="../crop/#crop_viewport">Viewport Crop</a> would also do this better, without the extra processing of canvas generation and overlaying that "<code><a href="https://imagemagick.org/script/command-line-options.php?#flatten">-flatten</a></code>" also does. It also will not 'expand' the image itself to cover the whole viewport if the image was only partially contained in that viewing window. A common mis-use of the "<code><a href="https://imagemagick.org/script/command-line-options.php?#flatten">-flatten</a></code>" operator is to <a href="../masking/#remove">Remove Transparency</a> from an image. That is, to get rid of any transparency that an image may have, but overlaying it on the background color. However this will not work when multiple images are involved as as such no longer recommended. <a name="mosaic" id="mosaic"></a>
<h3>Mosaic - Canvas Expanding</h3>The "<code><a href="https://imagemagick.org/script/command-line-options.php?#layers">-layers</a> <b>mosaic</b></code>" operator (or its "<code><a href="https://imagemagick.org/script/command-line-options.php?#mosaic">-mosaic</a></code>" shortcut) is more like an expanding canvas version of the <a href=".#flatten">Flatten Operator</a>. Rather than only creating an initial canvas based on just the canvas size of the initial image, the <a href="#mosaic">Mosaic Operator</a> creates a canvas that is large enough to hold all the images (in the positive direction only). For example, here I don't even set an appropriate <a href="../basics/#page">Virtual Canvas</a>, however the "<code><a href="https://imagemagick.org/script/command-line-options.php?#mosaic">-mosaic</a></code>" operator will work out how big such a canvas needs to be to hold all the image layers.
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr valign="top">
<td width="100%" align="justify">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick \( -page +5+10 balloon.gif \) \( -page +35+30 medical.gif \) \
\( -page +62+50 present.gif \) \( -page +10+55 shading.gif \) \
-background dodgerblue -layers mosaic mosaic.gif
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="mosaic.gif"><img src="mosaic.gif" align="middle" vspace="0" hspace="5" border="1" alt="[IM Output]"></a>
</td>
</tr>
</table>
<table border="0" cellspacing="0" cellpadding="0" width="90%" align="center">
<tr valign="top">
<td><img src="../img_www/warning.gif" width="28" height="28"><img src="../img_www/space.gif" width="12" height="16"></td>
<td align="justify" width="100%"><font size="-1"><i>As on IM v6.3.6-2 the "<code><a href="https://imagemagick.org/script/command-line-options.php?#mosaic">-mosaic</a></code>" operator is only an alias for a "<code><a href="https://imagemagick.org/script/command-line-options.php?#layers">-layers</a> 'mosaic'</code>".<br>
<br>
Thus the "<code><a href="https://imagemagick.org/script/command-line-options.php?#mosaic">-mosaic</a></code>" option can be regarded as a short cut for the "<code><a href="https://imagemagick.org/script/command-line-options.php?#layers">-layers</a></code>" method of the same name.</i></font></td>
</tr>
</table>Note that both "<code><a href="https://imagemagick.org/script/command-line-options.php?#mosaic">-mosaic</a></code>" and "<code><a href="https://imagemagick.org/script/command-line-options.php?#flatten">-flatten</a></code>" still creates a canvas that started from the 'origin' or 0,0 pixel. This is part of the definition of an images 'virtual canvas' or 'page' and because of this you can be sure that the final image for both operators will have a no virtual offset, and the whole canvas will be fully defined in terms of actual pixel data. Also note that "<code><a href="https://imagemagick.org/script/command-line-options.php?#mosaic">-mosaic</a></code>" will only expand the canvas in the positive directions (the bottom or right edges), as the top and left edge are fixed to the virtual origin. That of course means "<code><a href="https://imagemagick.org/script/command-line-options.php?#mosaic">-mosaic</a></code>" will still clip images with negative offsets...
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr valign="top">
<td width="100%" align="justify">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick \( -page -5-10 balloon.gif \) \( -page +35+30 medical.gif \) \
\( -page +62+50 present.gif \) \( -page +10+55 shading.gif \) \
-background dodgerblue -mosaic mosaic_clip.gif
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="mosaic_clip.gif"><img src="mosaic_clip.gif" align="middle" vspace="0" hspace="5" border="1" alt="[IM Output]"></a>
</td>
</tr>
</table><a name="merge" id="merge"></a>
<h3>Merging - to Create a New Layer Image</h3>The "<code><a href="https://imagemagick.org/script/command-line-options.php?#layers">-layers</a> <b>merge</b></code>" operator is almost identical to the previous operators and was added with IM v6.3.6-2. It only creates a canvas image just large enough to hold all the given images at their respective offsets. Like <a href="#mosaic">Mosaic</a> will also expand the canvas, but not only in the positive direction, but also in the negative direction. Basically it means that you don't have to worry about clipping, offset, or other aspects when merging layer images together. All images will be merged relative to each others location. The output does not include or ensure the origin is part of the expanded canvas. As such the output of a <a href="#merge">Layers Merge</a> can contain a 'layers offset' which may be positive or negative. In other words.. <a href="#merge">Layers Merge</a> merges layer images to produce a new <i>layer image</i>. As such if you don't want that offset when finished you will probably want to include a "<code><a href="https://imagemagick.org/script/command-line-options.php?#repage">+repage</a></code>" operator before the final save. For example, here is the same set of layer image we have used previously...
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr valign="top">
<td width="100%" align="justify">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick \( -page +5+10 balloon.gif \) \( -page +35+30 medical.gif \) \
\( -page +62+50 present.gif \) \( -page +10+55 shading.gif \) \
-background dodgerblue -layers merge +repage layers_merge.gif
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="layers_merge.gif"><img src="layers_merge.gif" align="middle" vspace="0" hspace="5" border="1" alt="[IM Output]"></a>
</td>
</tr>
</table>As you can see the image is only just big enough to hold all the images which were placed relative to each other, while I discarded the resulting images offset relative to the virtual canvas origin. This preservation of relative position without clipping or extra unneeded space is what make this variant so powerful. Lets try this again by giving one image a negative offset...
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr valign="top">
<td width="100%" align="justify">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick \( -page -5-10 balloon.gif \) \( -page +35+30 medical.gif \) \
\( -page +62+50 present.gif \) \( -page +10+55 shading.gif \) \
-background dodgerblue -layers merge +repage layers_merge_2.gif
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="layers_merge_2.gif"><img src="layers_merge_2.gif" align="middle" vspace="0" hspace="5" border="1" alt="[IM Output]"></a>
</td>
</tr>
</table>As you can see the "balloon" was not clipped, just moved further away from the others so as to preserve its relative distance to them. Of course the "<code><a href="https://imagemagick.org/script/command-line-options.php?#repage">+repage</a></code>" operator in the above examples, removes the absolute virtual canvas offset in the final image, preserving only the relative image placements between the images. The offset was removed as web browsers often have trouble with image offsets and especially negative image offsets, unless part of a GIF animation. But if I did not remove that offset, all the images will remain in their correct location on the virtual canvas within the generated single layer image, allowing you to continue to process and add more images to the merged image. Typically you would use a "<code><a href="https://imagemagick.org/script/command-line-options.php?#background">-background</a></code>" color of '<code>None</code>', to make the unused areas of the merged image transparent. When applied to a single image, <a href="#merge">Layer Merging</a> will replace any transparency in the image with the solid color background, but preserve the images original size, as well as any any offsets in that image, The virtual canvas size of the image however may be adjusted to 'best fit' that images size and offset. The operators original purpose was allow users to more easily merge multiple distorted images into an unified whole, regardless of the individual images offset. For example when aligning photos to form a larger 'panorama'. You could simply start with a central undistorted base image (without an offset), and use this operator to overlay the other images around that starting point (using either negative or positive offsets) that have been aligned and distorted to match that central image. For other examples of using this operator by distorting images to align common control points, see <a href="../distorts/#cube3d">3D Isometric Photo Cube</a>, and <a href="../distorts/#cube3d">3D Perspective Box</a>. Other examples of using this operator is to generate a simple series of <a href="../photos/#overlap">Overlapping Photos</a>.
<pre>
The operation "<code>-layers trim-bounds</code>" can be used to ensure all
images get a positive offset on a minimal canvas size, while retaining there
relative positions, and without actually layer merging the images into one
final image.
This lets you then perform further processing of the images before they are
actually merged, such as placing more images relative to the that image group
but looking up the resulting virtual canvas bounds.
However if images have a transparency, it is probably a good idea to trim
that transparency from images first, making the ideal usage...
-alpha set -bordercolor none -border 1x1 -trim -layers trim-bounds
This minimizes the image layers including any and all transparent areas of
actual image data, while ensuring everything is contained on a valid
virtual (positive) canvas of minimal size.
</pre><a name="coalesce" id="coalesce"></a>
<h3>Coalesce Composition - a Progressive Layering</h3>The "<code><a href="https://imagemagick.org/script/command-line-options.php?#layers">-layers</a> <b>coalesce</b></code>" image operator (or its "<code><a href="https://imagemagick.org/script/command-line-options.php?#coalesce">-coalesce</a></code>" shortcut) is really designed for converting GIF animations into a sequence of images. For examples, see <a href="../anim_basics/#coalesce">Coalescing Animations</a> for details. However, it is very closely associated with "<code><a href="https://imagemagick.org/script/command-line-options.php?#flatten">-flatten</a></code>" and has very useful effects for multi-layered images in this regard.
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr valign="bottom">
<td width="100%" align="justify">
For example using <a href="#coalesce">Coalesce</a> on a single image, will do exact the same job as using <a href="#flatten">Flatten</a> with a "<code><a href="https://imagemagick.org/script/command-line-options.php?#background">-background</a></code>" color of '<code>None</code>' or '<code>Transparency</code>'. That is, it will 'fill out' the canvas of the image with transparent pixels.
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick \( -page 100x100+5+10 balloon.gif \) -layers coalesce coalesce_canvas.gif
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="coalesce_canvas.gif"><img src="coalesce_canvas.gif" align="middle" vspace="0" hspace="5" border="1" alt="[IM Output]"></a>
</td>
</tr>
</table>When dealing with an image consisting on multiple layers, <a href="#coalesce">Coalesce</a> can be used to generate a 'Progressive Layering' of the image. But to do this we need to take a few precautions, to disable any 'GIF animation' handling by the operator.
<div align="center">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="90%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick \( -page 100x100+5+10 balloon.gif \) \( -page +35+30 medical.gif \) \
\( --page +62+50 present.gif \) \( -page +10+55 shading.gif \) \
-set dispose None -coalesce miff:- |\
montage - -frame 4 -tile x1 -geometry +2+2 \
-background none -bordercolor none coalesce_none.gif
</samp></pre>
</td>
</tr>
</table><a href="coalesce_none.gif"><img src="coalesce_none.gif" align="middle" vspace="2" hspace="5" border="0" alt="[IM Output]"></a>
</div>In the above, we "<code><a href="https://imagemagick.org/script/command-line-options.php?#set">-set</a></code>" all the "<code><a href="https://imagemagick.org/script/command-line-options.php?#dispose">-dispose</a></code>" settings to '<code><a href="../anim_basics/#none">None</a></code>'. This effectively tells "<code><a href="https://imagemagick.org/script/command-line-options.php?#coalesce">-coalesce</a></code>" to just overlay each frame on top the results of the previous overlays. The result is the first image is just a 'fill out' of the images canvas, with a transparency background. The next image is the previous image with that layer overlaid. And so on. A 'progressive' flatten of the image sequence. The last image in the sequence will thus be the same as if you did a normal "<code><a href="https://imagemagick.org/script/command-line-options.php?#flatten">-flatten</a></code>" with a transparent background. You can get a completely different sort of effect if you had used a "<code><a href="https://imagemagick.org/script/command-line-options.php?#dispose">-dispose</a></code>" setting of '<code><a href="../anim_basics/#background">Background</a></code>'. In this case "<code><a href="https://imagemagick.org/script/command-line-options.php?#coalesce">-coalesce</a></code>" will just 'fill out' the canvas of each image, as if they were completely separate images!
<div align="center">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="90%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick \( -page 100x100+5+10 balloon.gif \) \( -page +35+30 medical.gif \) \
\( --page +62+50 present.gif \) \( -page +10+55 shading.gif \) \
-set dispose Background -coalesce miff:- |\
montage - -frame 4 -tile x1 -geometry +2+2 \
-background none -bordercolor none coalesce_bgnd.gif
</samp></pre>
</td>
</tr>
</table><a href="coalesce_bgnd.gif"><img src="coalesce_bgnd.gif" align="middle" vspace="2" hspace="5" border="0" alt="[IM Output]"></a>
</div>Please note however that unlike <a href="#flatten">Flatten</a>, <a href="#mosaic">Mosaic</a>, or <a href="#merge">Merge</a> the "<code><a href="https://imagemagick.org/script/command-line-options.php?#coalesce">-coalesce</a></code>" operator does <i>not</i> make use of the current "<code><a href="https://imagemagick.org/script/command-line-options.php?#compose">-compose</a></code>" alpha composition setting. It only uses an '<code><a href="../compose/#over">Over</a></code>' compose method, as this is what is required for GIF animation handling. Using different "<code><a href="https://imagemagick.org/script/command-line-options.php?#compose">-compose</a></code>" methods with the more standard image layering operators is the subject of the next set of examples. <a name="compose" id="compose"></a>
<h3>Compose Methods and Layering</h3>The three <a href="#layers">Layering</a> methods: <a href="#flatten">Flatten</a>, <a href="#mosaic">Mosaic</a>, and <a href="#merge">Merge</a>; will make use of the "<code><a href="https://imagemagick.org/script/command-line-options.php?#compose">-compose</a></code>" setting to determine the composition method used to overlay each image in sequence. As such you could think of these functions as a multi-image "<code><a href="https://imagemagick.org/script/command-line-options.php?#composite">-composite</a></code>" operator with the ability to set an initial "<code><a href="https://imagemagick.org/script/command-line-options.php?#background">-background</a></code>" canvas of a specified color. However using anything but the default <a href="../compose/">Alpha Composition</a> of '<code>Over</code>' requires some thought before applying or you will get unexpected results. You may also may need to think about the effect of the "<code><a href="https://imagemagick.org/script/command-line-options.php?#background">-background</a></code>" color that is used by these operators to generate a starting canvas, onto with each image (including the first) in composed. For example lets place each successive image <i>under</i> the previous images using a '<code><a href="../compose/#dstover">DstOver</a></code>'...
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td width="100%" align="justify">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick \( -page 100x100+5+10 balloon.gif \) \( -page +35+30 medical.gif \) \
\( --page +62+50 present.gif \) \( -page +10+55 shading.gif \) \
-background none -compose DstOver -flatten flatten_dstover.gif
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="flatten_dstover.gif"><img src="flatten_dstover.gif" align="middle" vspace="0" hspace="5" border="1" alt="[IM Output]"></a>
</td>
</tr>
</table>Here the background was set to be transparent, otherwise you will only see the background canvas in the result as all the other images will have been placed 'under' this initial canvas! This does provide a way of 'blanking' an image with a particular color, as shown in <a href="../canvas/#sized">Canvases Sized to an Existing Image</a>. Here is a more practical example. Rather than layering the images with the background canvas first, which awkward and un-natural in some image processing situations, you can just generate the images top-down or foreground to background order.
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td width="100%" align="justify">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick rose: -repage +10+10 \
\( +clone -background black -shadow 60x3+5+5 \) \
\( granite: -crop 100x80+0+0 +repage \) \
-background none -compose DstOver -layers merge layer_dstover.gif
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="layer_dstover.gif"><img src="layer_dstover.gif" width="100" height="80" align="middle" vspace="0" hspace="5" border="1" alt="[IM Output]"></a>
</td>
</tr>
</table>Each of the first three lines generates one layer image, with the final line merging all the layers under the previous layers, effectively reversing the order.
<p>As you can see the image processing for the above was simpler and cleaner than you normally would see with shadow generation, just by underlaying each image in sequence (with a transparent starting canvas)</p>Of course I could have just as easily <a href="../basics/#reverse">Reverse</a> the image list instead.
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td width="100%" align="justify">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick rose: -repage +10+10 \
\( +clone -background black -shadow 60x3+5+5 \) \
\( granite: -crop 100x80+0+0 +repage \) \
-reverse -layers merge layer_reverse.gif
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="layer_reverse.gif"><img src="layer_reverse.gif" width="100" height="80" align="middle" vspace="0" hspace="5" border="1" alt="[IM Output]"></a>
</td>
</tr>
</table>However remember that this only re-orders the existing images, and does not effect the 'starting background canvas' that the layering methods create. The compose methods can also be used to produce some interesting effects. For example, if you draw three circles, then by overlaying them using the '<code>Xor</code>' compose method, you get an unusual and complex looking symbol, for minimal effort.
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td width="100%" align="justify">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick -size 60x60 \
\( xc:none -fill blue -draw 'circle 21,39 24,57' \) \
\( xc:none -fill red -draw 'circle 39,39 36,57' \) \
\( xc:none -fill green -draw 'circle 30,21 30,3' \) \
-background none -compose Xor -flatten flatten_xor.png
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="flatten_xor.png"><img src="flatten_xor.png" align="middle" vspace="0" hspace="5" border="1" alt="[IM Output]"></a>
</td>
</tr>
</table><a name="layer_composite" id="layer_composite"></a>
<h3>Layers Composite - Merge Two Layer Lists</h3>With IM v6.3.3-7 the "<code><a href="https://imagemagick.org/script/command-line-options.php?#layers">-layers</a></code>" method, '<code><b>Composite</b></code>' was added allowing you compose two completely separate sets of images together. To do this on the command line a special '<code><a href="../files/#null">null:</a></code>' marker image is needed to define where the first <i>destination</i> list of images ends and the overlaid <i>source</i> image list begins. But that is the only real complication of this method. Basically each image from the first list is composed against the corresponding image in the second list, effectively merging the two lists together. The second list can be positioned globally relative to the first list, using a <a href="../compose/#geometry">Geometry Offset</a>, just as you can with a normal <a href="../compose/#convert">Composite Operator</a> (see above). Gravity is also applied using the canvas size of the first image, to do the calculations. On top of that 'global offset', the individual virtual offset of image is also preserved, as each pair of images is composited together. One special case is also handled. If one of the image lists contains only one image, that image will be composed against all the images of the other list. Also in that case the image meta-data (such as animation timings) of larger list is what will be kept, even if it is not the destination side of the composition.<br>
This laying operator is more typically used when composing two animations, which can be regarded as a sort of time-wise layered image list. Because of this it is better exampled in the <a href="../anim_mods/">Animation Modifications</a> section of the examples. So see <a href="../anim_mods/#compose">Multi-Image Alpha Composition</a> for more details.
<hr>
<!-- ---------------------------------------------------------------- -->
<a name="layer_handling" id="layer_handling"></a> <a name="layer_examples" id="layer_examples"></a>
<h2>Handling Image Layers</h2>Laying multiple images using the various layer operators above is a very versatile technique. It lets you work on a large number of images individually, and then when finished you combine them all into a single unified whole. So far we have shown various ways of merging (composing or layering) multiple images in many different ways. Here I provide some more practical examples on just how to make use of those techniques. <a name="layer_thumbnails" id="layer_thumbnails"></a>
<h3>Layering Of Thumbnail Images</h3>You can also use this technique for merging multiple thumbnails together in various complex ways. Here I add a <a href="../thumbnails/#soft_edges">Soft Edge</a> to the images as you read and position them, you can generate a rather nice composition of images, on a <a href="../canvas/#tile">Tiled Canvas</a>.
<div align="center">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="90%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick -page +5+5 holocaust_tn.gif \
-page +80+50 spiral_stairs_tn.gif \
-page +40+105 chinese_chess_tn.gif \
+page \
-alpha Set -virtual-pixel transparent \
-channel A -blur 0x10 -level 50,100% +channel \
\( -size 200x200 tile:tile_fabric.gif -alpha Set \) -insert 0 \
-background None -flatten overlap_canvas.jpg
</samp></pre>
</td>
</tr>
</table><a href="overlap_canvas.jpg"><img src="overlap_canvas.jpg" width="200" height="200" align="middle" vspace="5" hspace="5" border="1" alt="[IM Output]"></a>
</div><a name="layer_calc" id="layer_calc"></a>
<h3>Calculated Positioning of Images.</h3>The <a href="../basics/#page">Virtual Canvas Offset (page)</a> can be set in many ways. More specifically you can "<code><a href="https://imagemagick.org/script/command-line-options.php?#set">-set</a></code>" set this per-image <a href="../basics/#attribute">Attribute</a>, and even calculate a different location for each and every image. For example, here I read in a big set of images (small icon images all the same size) and arrange them in a circle.
<div align="center">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="90%" bgcolor="#F8F8F8">
<tr>
<td>
<pre class="bg-light text-dark mx-4"><samp>
magick {balloon,castle,eye,eyeguy,ghost,hand_point,medical}.gif \
{news,noseguy,paint_brush,pencil,present,recycle}.gif \
{shading,skull,snowman,storm,terminal,tree}.gif \
\
-set page '+%[fx:80*cos((t/n)*2*pi)]+%[fx:80*sin((t/n)*2*pi)]' \
\
-background none -layers merge +repage image_circle.png
</samp></pre>
</td>