-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
3538 lines (2913 loc) · 163 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
<html lang="en"><HEAD>
<meta charset="utf-8" >
<meta name="viewport" content="width=device-width, initial-scale=1" >
<link rel="stylesheet" href="../assets/usage.css">
<TITLE>Masks -- 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/masking/">
</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>
Masks</H1>
<div>
<DL>
<DT><B>Index</B>
<DT><A HREF="../"
><IMG SRC="../img_www/granitesm_left.gif" BORDER=0 WIDTH=15 HEIGHT=15
> ImageMagick Examples Preface and Index</A>
<DD><A HREF="#alpha_channel"
><IMG SRC="../img_www/granitesm_right.gif" BORDER=0 WIDTH=15 HEIGHT=15
>Alpha (Matte) Channel</A>
<UL>
<LI><A HREF="#alpha"
>Internal Matte Channel</A>
<LI><A HREF="#alpha"
>Alpha Channel Operator</A>
<DL><DD>
<A HREF="#alpha_off" ><CODE>Off</CODE></A>
(or <A HREF="#alpha_off"<CODE>-alpha off</CODE></A>),
<A HREF="#alpha_set" ><CODE>Set</CODE></A>
(or <A HREF="#alpha_set"><CODE>-alpha set</CODE></A>),
<A HREF="#alpha_on" ><CODE>On</CODE></A>, <BR>
<A HREF="#alpha_activate" ><CODE>Activate</CODE></A>, <BR>
<A HREF="#alpha_discrete" ><CODE>Discrete</CODE></A>, <BR>
<A HREF="#alpha_opaque" ><CODE>Opaque</CODE></A>,
<A HREF="#alpha_transparent" ><CODE>Transparent</CODE></A>,
<A HREF="#alpha_extract" ><CODE>Extract</CODE></A>,
<A HREF="#alpha_copy" ><CODE>Copy</CODE></A>, <BR>
<A HREF="#alpha_shape" ><CODE>Shape</CODE></A>,
<A HREF="#alpha_remove" ><CODE>Remove</CODE></A>,
<A HREF="#alpha_background" ><CODE>Background</CODE></A><BR>
</DL>
<LI><A HREF="#remove"
>Remove Transparency of an Image</A>
<LI><A HREF="#boolean_transparency"
>Boolean Alpha Transparency</A>
<LI><A HREF="#outline"
>Outline or Halo Transparency</A>
</UL>
<DD><A HREF="#masks"
><IMG SRC="../img_www/granitesm_right.gif" BORDER=0 WIDTH=15 HEIGHT=15
> Using Masks with Images </A>
<UL>
<LI><A HREF="#editing" >Editing an Image Mask</A>
<LI><A HREF="#shapes" >Masks as Colored Shapes</A>
<LI><A HREF="#compose" >Mathematical Composition</A>
<LI><A HREF="#masked_compose" >Masked Alpha Composition </A>
<LI><A HREF="#aligning" >Aligning Two Masked Images</A>
(under construction)
</UL>
<DD><A HREF="#special_masks"
><IMG SRC="../img_www/granitesm_right.gif" BORDER=0 WIDTH=15 HEIGHT=15
> Special Image Masks</A>
<UL>
<LI><A HREF="#write_mask" >Write Mask - Protect Pixels form Change</A>
<LI><A HREF="#clip_mask" >Clip Mask and Clip Paths</A>
<LI><A HREF="#read_mask" >Read Masks - Ignore Pixel Input</A>
</UL>
<DD><A HREF="#regions"
><IMG SRC="../img_www/granitesm_right.gif" BORDER=0 WIDTH=15 HEIGHT=15
> Regions and Region Sub-Images</A>
<UL>
<LI><A HREF="#region_warping" >Warping a Local Region</A>
<LI><A HREF="#region_internals" >How Regions Work, and its Problems</A>
</UL>
<DD><A HREF="#bg_remove"
><IMG SRC="../img_www/granitesm_right.gif" BORDER=0 WIDTH=15 HEIGHT=15
> Background Removal</A>
<UL>
<LI><A HREF="#floodfill"
>Simple Backgrounds (floodfill)</A>
<LI><A HREF="#border_cut"
>Masking Bordered Objects</A>
<LI><A HREF="#known_bgnd"
>Removing a Known Background</A>
<LI><A HREF="#difference"
>Difference Image Masking and Feathering</A>
<LI><A HREF="#semi-trans"
>Recovering Semi-Transparent Edges</A>
<LI><A HREF="#two_background"
>Background Removal using Two Backgrounds</A>
</UL>
<DD><A HREF="#hole_filling"
><IMG SRC="../img_www/granitesm_right.gif" BORDER=0 WIDTH=15 HEIGHT=15
> Hole_Filling</A>
<UL>
<LI><A HREF="#create_a_hole"
>Creating a Hole to Fill</A>
<LI><A HREF="#blur_fill"
>Filling using a Blur</A>
</UL>
(Under Construction)
</DL>
In these examples we look at the special handling of transparency, the
transparency channel, using masks, and ultimately the removal of unwanted
backgrounds, or other elements, such as signs, text, spam. </P>
<HR><!-- ---------------------------------------------------------------- -->
<A NAME="alpha_channel"></A>
<H2>Alpha Channel</H2>
The transparency (alpha) channel of an image is completely optional, and often
requires special handling separate to the normal 'color' channels. See <A
HREF="../color_basics/#colorspace" >Image Color Space</A> above. </P>
The existence of a transparency channel can also effect how the various
operators treat the other color channels, generally because
a fully-transparent color should often be completely ignored by an operation.
If this was not the case you get 'Black Halos' around images, such as was seen
in major IM Bugs in the early days of IM v6. For example the <A
HREF="../bugs/resize_halo/" >Resize Halo Bug</A>, and the <A
HREF="../bugs/blur_trans/" >Blur with Transparency Bug</A>. </P>
To make matters worse, this channel is also sometimes referred to at an image's
'transparency' or 'opacity' channel, or even the image's 'mask'. All however
refer to the same, special, fourth channel of the image. </P>
<A HREF="../images/moon.png"
><IMG SRC="../images/moon.png" WIDTH=70 HEIGHT=70
ALIGN=right VSPACE=5 HSPACE=5 BORDER=0 ALT="[IM Output]"></A>
To explain the difference we need a working example image and for this I'll
use a PNG image of a 'crescent moon' image (from a <A
HREF="../compose/#copyopacity" >CopyOpacity Composition</A> example).
Now as you can see this image has a lot of areas which are fully transparent.
Not only that I needed to save the image using the 'PNG' image format which is
one of the small number of image formats that properly understands and handles
transparent and semi-transparent colors. </P>
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 width="100%">
<TR VALIGN=bottom><TD width="100%" ALIGN=justify>
I can demonstrate this transparency by overlaying the image onto the IM
built-in checkerboard pattern, using <A HREF="../compose/" >Alpha
Composition</A>.
<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 composite -compose Dst_Over -tile pattern:checkerboard \
moon.png moon_background.jpg
</samp></pre></TD></TR></TABLE></TD><TD>
<A HREF="moon_background.jpg"
><IMG SRC="moon_background.jpg" WIDTH=70 HEIGHT=70
ALIGN=middle VSPACE=0 HSPACE=5 BORDER=1 ALT="[IM Output]"></A>
</TD></TR></TABLE></P>
<A NAME="alpha"></A>
<H3>Internal Alpha Channel</H3>
Now internally IM v7 stores the transparency information in a 'alpha' channel,
which just like the color channel is just a plain grey scale image of values
which range from white, for fully-transparent (or clear), to black for
fully-opaque. It is sort of like what you would get if you look at a
silhouette of the original image. </P>
Low level operators such as "<CODE><A HREF="../option_link.cgi?level"
>-level</A></CODE>" and "<CODE><A HREF="../option_link.cgi?threshold"
>-threshold</A></CODE>" handle the data as alpha.
Check the <A HREF="../option_link.cgi" >Official Option Reference</A> if you
are unsure. </P>
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 width="100%">
<TR valign="top"><TD width="100%" ALIGN=justify>
Here is a very old way to extract a 'alpha' transparency values from an
image. It saves the transparency channel as a 'alpha' image file format, and
required two separate steps, and commands to define the right image file
format.
<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>
<c> magick moon.png alpha:moon.matte
<c> magick MIFF:moon.alpha moon_matte2.png
# You can join those two steps in a pipeline as well...
magick moon.png alpha:- | magick - moon_matte3.png
</samp></pre></TD></TR></TABLE></TD><TD>
<A HREF="moon_matte3.png"
><IMG SRC="moon_matte3.png" WIDTH=70 HEIGHT=70
ALIGN=middle VSPACE=5 HSPACE=5 BORDER=1 ALT="[IM Output]"></A>
</TD></TR></TABLE></P>
This technique for extracting the 'alpha' of an image was common when IM v5
was in use. Basically it was the only method provided to get access to the
transparency of an image. It is now very rarely used. </P>
<A NAME="alpha"></A>
<H3>Controlling Image Transparency</H3>
There are two operators that give you low-level control of the transparency
channel of an image in memory. The newer operator "<CODE><A
HREF="../option_link.cgi?alpha" >-alpha</A></CODE>" methods are now the
recommended method of control, though many IM Examples still show and use the
older "<CODE><A HREF="../option_link.cgi?alpha" >-alpha</A></CODE>" operator.
</P>
An image cannot only have alpha channel data, but it also has a 'switch' that
defines if the channel data is viewable or valid. This means images can have
three states with regards to the alpha channel. </P>
<TABLE BORDER=0 ALIGN=center>
<TR><TH>Switch <TD ROWSPAN=4> <TD><B>Channel Data</B>
<TR><TD> alpha off <TD>no alpha data (no memory has been allocated)
<TR><TD> alpha off <TD>old alpha data present (but not in use)
<TR><TD> alpha on <TD>alpha data that is currently in use
</TABLE></P>
This needs to be remembered as how the various methods behave depends on which
of the above three states the image was in. </P>
If the 'switch' is off operators will not touch the alpha data, as it may not
actually exist at all. In such a case old alpha could still be present,
unmodified, and thus out-of-date. As you will see this is actually sometime
useful in some situations. </P>
Note however that some operators may automatically turn on, or turn off the
alpha switch for one reason or another. </P>
For example, "<CODE>-compose CopyOpacity -composite</CODE>" will always turn on
the alpha channel in the resulting image, as it is the operator's job to copy
data into an alpha channel. As such it must exist in the final result. However
its existence in the input data can have other consequences. See <A
HREF="../compose/#copyopacity" >Copy_Opacity Composition Method</A> for more
details. </P>
Similarly creating a canvas using the color '<CODE>None</CODE>' will also
automatically create and enable the transparency channel, so as to ensure the
blank image really is transparent. On the other hand, creating a canvas using
some other <A HREF="../color_basics/#color" >Color Name</A> will generally not
create any transparency channel as images are opaque by default. </P>
<BR>
Here are the various "<CODE><A HREF="../option_link.cgi?alpha"
>-alpha</A></CODE>" methods and examples of how they effect images and their
transparency. </P>
<A NAME="alpha_off"></A>
<H4>Alpha Off <FONT SIZE=-1>or</FONT> "<CODE>-alpha off</CODE>"</H4>
This is just a simple switch on the image, which turns off any effect the
transparency has on the image. It does not actually delete or destroy the
alpha channel attached to the image, it just turns off any effect that channel
has on the image. Similarly no operator will effect the attached alpha
channel while it has been turned off. </P>
For example let's use the 'crescent moon' image (from a <A
HREF="../compose/#copyopacity" >CopyOpacity Composition</A> example), and
simply turn the image alpha channel off.
<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 moon.png -alpha off alpha_off.png
</samp></pre></TD></TR></TABLE>
<A HREF="../images/moon.png"
><IMG SRC="../images/moon.png" WIDTH=70 HEIGHT=70
ALIGN=middle VSPACE=2 HSPACE=15 BORDER=1 ALT="[IM Output]"></A>
<IMG SRC="../img_www/right.gif" ALIGN=middle WIDTH=20 HEIGHT=20 ALT="==>">
<A HREF="alpha_off.png"
><IMG SRC="alpha_off.png" WIDTH=70 HEIGHT=70
ALIGN=middle VSPACE=2 HSPACE=15 BORDER=1 ALT="[IM Output]"></A>
</DIV></P>
Note that the moon shape completely vanished when the transparency was turned
off, though that is actually rarely the case. Basically even the 'transparent'
areas have color, which is just not normally visible, in this case the hidden
color was the fractal canvas image that was used to create the moon image.
</P>
This hidden color could be anything, from a simple <A
HREF="../formats/#gif_trans" >GIF Transparency Color</A>, that the GIF format
uses to represent transparency in its color table, to garbage colors left
behind during the images creation, as above. More typically the transparency
color is simply pure-black for any pixel that was fully-transparent. Note
that pixels close to the edge may be semi-transparent, and thus still have
a valid color that is only partially visible. </P>
The "<CODE><A HREF="../option_link.cgi?alpha" >-alpha</A> Off</CODE>"
operation in the above will have simply 'deactivate' or 'turn off' the
channel. The transparency data itself has not been cleared or removed from
the image data stored in-memory. It is still present, just unavailable for
the moment. But... </P>
If the image is saved, while the transparency data is turned off, none of the
transparency data will be saved into the image file format. As such the
turned-off alpha data is not present in the saved copy of the image, even
though it is present (just turned off) in the in-memory version. </P>
Also as many file formats do not allow transparency (such as JPEG), these file
formats automatically do the equivalent of a "<CODE><A
HREF="../option_link.cgi?alpha" >-alpha</A> Off</CODE>" when the image is
saved (without actually doing so). Generally this results in all transparent
areas typically turning black when saved as a JPEG image. See <A
HREF="#alpha_remove" >Alpha Remove - Removing Transparency</A> below for the
correct way to remove transparency before saving to a JPEG file format. </P>
The "<CODE><A HREF="../option_link.cgi?alpha" >+alpha</A></CODE>" operator is
an older command that is exactly the same as "<CODE>-alpha Off</CODE>". That
is it just turns off the transparency channel. </P>
Note that turning off alpha, is often required before using a gray-scale mask
image with the <A HREF="../compose/#copyopacity" >CopyOpacity</A> Alpha
composition method. If you don't do this the compostion operator will copy
the enabled transparency (opacity channel) rather that use the intended
grayscale colors. </P>
<A NAME="alpha_set"></A>
<H4>Alpha Set <FONT SIZE=-1>or</FONT> "<CODE>-alpha set</CODE>"</H4>
The '<CODE>Set</CODE>' alpha method is the same as the older "<CODE><A
HREF="../option_link.cgi?alpha" >-alpha on</A></CODE>" option. </P>
This ensures that the image has a 'transparency' or alpha channel, but
if it was not present or turned off, that it is initialised to be fully-opaque
(See the <A HREF="#alpha_opaque" >Alpha Opaque</A> method below). </P>
However if the image already has an alpha channel present and enabled, it will
do nothing. </P>
In other words this operator ensures an alpha channel is present, without
modifying the look of the image as it is currently in memory. As such on its
own this operator does not show any change to the image, but has real effects
when combined with other operators. </P>
So if you turn off the alpha channel using <A HREF="#alpha_off" >Alpha
Off</A>, and then enable it again using <A HREF="#alpha_set" >Alpha Set</A>,
the image will have an alpha channel, but it will be fully-opaque, just as the
image looked, when '<CODE>Set</CODE>' operation was requested.
<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 moon.png -alpha off -alpha set alpha_set.png
</samp></pre></TD></TR></TABLE></TD><TD>
<A HREF="alpha_set.png"
><IMG SRC="alpha_set.png" WIDTH=70 HEIGHT=70
ALIGN=middle VSPACE=0 HSPACE=5 BORDER=1 ALT="[IM Output]"></A>
</TD></TR></TABLE></P>
If applied to an image that has an enabled alpha channel, no change is made.
<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 moon.png -alpha set alpha_noset.png
</samp></pre></TD></TR></TABLE></TD><TD>
<A HREF="alpha_noset.png"
><IMG SRC="alpha_noset.png" WIDTH=70 HEIGHT=70
ALIGN=middle VSPACE=0 HSPACE=5 BORDER=1 ALT="[IM Output]"></A>
</TD></TR></TABLE></P>
In summery, this operator should never change the appearance of the image, at
the time the operator is applied. It just ensures the alpha channel set up
such that the image is left <I>as is</I>. </P>
This is typically used <B>after reading images</B> from an unknown image file
format, or input source, which may or may not have an alpha channel present.
This operator will then ensure that the image does have an alpha channel (for
image formats like JPEG), but leaving any enabled and existing alpha channel
alone (such as for GIF or PNG formats). </P>
This is the recommended way of ensuring an image has an alpha channel after
reading it into memory, or more importantly, after an image has been processed
and you want to re-enable a clean alpha channel. </P>
<A NAME="alpha_on"></A>
<H4>Alpha On</H4>
The "<CODE><A HREF="../option_link.cgi?alpha" >-alpha</A> On</CODE>" is the
exact opposite to the previously looked at <A HREF="#alpha_off" >Alpha Off</A>
method. Typically this is <I>too simplistic</I> for the purpose you are
wanting and as such <B>should be very RARELY used</B>. You should use "<CODE><A
HREF="#alpha_set" >-alpha Set</A></CODE>" in almost all cases. </P>
Basically '<CODE>On</CODE>' method just flips the switch so that the image
transparency data is visible again. Any existing transparency data is not
modified, so if the in-memory image still has some old alpha channel data,
that data will suddenly be visible again. </P>
For example, here we turn '<CODE>Off</CODE>' the transparency data, then
immediately turn it back '<CODE>On</CODE>, reproducing the original image.
<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 moon.png -alpha off -alpha on alpha_on.png
</samp></pre></TD></TR></TABLE></TD><TD>
<A HREF="alpha_on.png"
><IMG SRC="alpha_on.png" WIDTH=70 HEIGHT=70
ALIGN=middle VSPACE=0 HSPACE=5 BORDER=1 ALT="[IM Output]"></A>
</TD></TR></TABLE></P>
However if the image does not have any previous alpha data (yet) it will
initialize it to be fully-opaque. Which is the logical thing to do. As such
for new images just read into memory, it is equivalent to <A HREF="#alpha_set"
>Alpha Set</A>, but it should not be used for this purpose. </P>
The only time <A HREF="../option_link.cgi?alpha" >Alpha On</A> should be used
is when you previously, and <I>purposefully turned off alpha</I> for some
reason, and now wish to restore that data. </P>
For example turning the alpha channel off then on can be used to preserve the
alpha channel data before applying some very specific operators, such as
"<CODE><A HREF="../option_link.cgi?shade" >-shade</A></CODE>". </P>
For an example of this special usage see <A HREF="../transform/#shade_shape"
>Shaded Shape Images</A>. </P>
<!-- <CODE EXECUTE ASSERT>
[ "`magick xc:none -alpha off -alpha on txt:- | md5sum`" = \
"`magick xc:black -alpha on txt:- | md5sum`" ] || echo >&2 \
"ASSERTION FAILURE: Alpha Channel not set opaque using '-alpha one'"
[ "`magick xc:none -alpha Off txt:- | md5sum`" = \
"`magick xc:black txt:- | md5sum`" ] || echo >&2 \
"ASSERTION FAILURE: Alpha Channel 'Off' did not turn off alpha"
[ "`magick xc:none -alpha Off -alpha On txt:- | md5sum`" = \
"`magick xc:none txt:- | md5sum`" ] || echo >&2 \
"ASSERTION FAILURE: Alpha Channel Incorrectly Cleared using '-alpha Off/On'"
</CODE> -->
<A NAME="alpha_activate"></A>
<H4>Alpha Activate/Deactivate</H4>
<p>enables and disables the alpha channel, respectively, with persistence. This is like on/off in Imagemagick 6. In Imagemagick 7, -alpha off will remove the alpha channel permanently such that -alpha on will not re-enable it.</p>
<A NAME="alpha_discrete"></A>
<H4>Alpha Discrete</H4>
treat the alpha channel independently (do not blend).
<A NAME="alpha_opaque"></A>
<H4>Alpha Opaque</H4>
<p>This method not only ensures the alpha channel is 'active' but that it is also
completely opaque, regardless of if the image had transparency 'activated/on'
or 'deactivated/off'. For example...</p>
<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 moon.png -alpha opaque alpha_opaque.png
</samp></pre></TD></TR></TABLE></TD><TD>
<A HREF="alpha_opaque.png"
><IMG SRC="alpha_opaque.png" WIDTH=70 HEIGHT=70
ALIGN=middle VSPACE=0 HSPACE=5 BORDER=1 ALT="[IM Output]"></A>
</TD></TR></TABLE></P>
On older versions of IM, this was equivalent to using both "<CODE><A
HREF="../option_link.cgi?alph" >-alpha off</A></CODE>" to turn off the alpha
channel, then using "<CODE><A HREF="../option_link.cgi?alpha"
>-alpha on</A></CODE>" to turn it on, while resetting it to be opaque.
<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 moon.png -alpha off -alpha on alpha_opaque_matte.png
</samp></pre></TD></TR></TABLE></TD><TD>
<A HREF="alpha_opaque_matte.png"
><IMG SRC="alpha_opaque_matte.png" WIDTH=70 HEIGHT=70
ALIGN=middle VSPACE=0 HSPACE=5 BORDER=1 ALT="[IM Output]"></A>
</TD></TR></TABLE></P>
The original 'shape' of the image can no longer be recovered after this
operation as the original alpha channel data has been overwritten. </P>
Of course that is also equivalent to using "<CODE>-alpha off -alpha
set</CODE>", though you may as well use "<CODE>-alpha opaque</CODE>" in that
case. </P>
<A NAME="alpha_transparent"></A>
<H4>Alpha Transparent</H4>
Similarly this ensures the alpha channel is 'active' but also fully
transparent.
<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 moon.png -alpha transparent alpha_transparent.png
</samp></pre></TD></TR></TABLE></TD><TD>
<A HREF="alpha_transparent.png"
><IMG SRC="alpha_transparent.png" WIDTH=70 HEIGHT=70
ALIGN=middle VSPACE=0 HSPACE=5 BORDER=1 ALT="[IM Output]"></A>
</TD></TR></TABLE></P>
The color data of the image is still present, so turning off transparency
afterward will again show the images existing colors.
<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 moon.png -alpha transparent -alpha off alpha_transparent_off.png
</samp></pre></TD></TR></TABLE></TD><TD>
<A HREF="alpha_transparent_off.png"
><IMG SRC="alpha_transparent_off.png" WIDTH=70 HEIGHT=70
ALIGN=middle VSPACE=0 HSPACE=5 BORDER=1 ALT="[IM Output]"></A>
</TD></TR></TABLE></P>
Of course the original 'shape' of the image was actually destroyed, so it can
no longer be recovered after this operation. </P>
Other ways of making an image fully transparent is presented in <A
HREF="../canvas/#transparent" >Transparent Canvas</A>. </P>
<A NAME="alpha_extract"></A>
<H4>Alpha Extract</H4>
The '<CODE>Extract</CODE>' method will simply copy the 'alpha' mask of the
image as a gray-scale channel mask.
<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 moon.png -alpha extract alpha_extract.png
</samp></pre></TD></TR></TABLE>
<A HREF="../images/moon.png"
><IMG SRC="../images/moon.png" WIDTH=70 HEIGHT=70
ALIGN=middle VSPACE=2 HSPACE=15 BORDER=1 ALT="[IM Output]"></A>
<IMG SRC="../img_www/right.gif" ALIGN=middle WIDTH=20 HEIGHT=20 ALT="==>">
<A HREF="alpha_extract.png"
><IMG SRC="alpha_extract.png" WIDTH=70 HEIGHT=70
ALIGN=middle VSPACE=2 HSPACE=15 BORDER=1 ALT="[IM Output]"></A>
</DIV></P>
Note that fully-opaque is white, while fully-transparent is pure black.
</P>
As image contained some semi-transparent pixels along the edges (for
anti-aliasing providing the images shape with a smoother look), this image is
not pure black and white, but also contains some gray colored pixels around
the edges. </P>
If your ImageMagick is an old IMv7 version, this is a (near) equivalent
technique, using channel extraction.
<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 moon.png -channel a -separate +channel -negate alpha_extract.png
</samp></pre></TD></TR></TABLE>
</DIV></P>
The '<CODE><A HREF="#alpha_extract" >Extract</A></CODE>' method will also
turn '<CODE><A HREF="#alpha_extract" >Off</A></CODE>' the alpha, but it is
not cleared, so turning the alpha channel back '<CODE><A HREF="#alpha_extract"
>On</A></CODE>' will re-create a shape mask of the original image.
<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 moon.png -alpha extract -alpha on alpha_extract_on.png
</samp></pre></TD></TR></TABLE></TD><TD>
<A HREF="alpha_extract_on.png"
><IMG SRC="alpha_extract_on.png" WIDTH=70 HEIGHT=70
ALIGN=middle VSPACE=0 HSPACE=5 BORDER=1 ALT="[IM Output]"></A>
</TD></TR></TABLE></P>
Note that all the original colors will have been replaced with white with
various shades grays around the edges. We can see this if we remove the
transparency with a white background, (See <A HREF="#alpha_remove" >Alpha
Remove</A> method below)
<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 alpha_extract_on.png -background white -alpha remove alpha_edge.png
</samp></pre></TD></TR></TABLE></TD><TD>
<A HREF="alpha_edge.png"
><IMG SRC="alpha_edge.png" WIDTH=70 HEIGHT=70
ALIGN=middle VSPACE=0 HSPACE=5 BORDER=1 ALT="[IM Output]"></A>
</TD></TR></TABLE></P>
These 'gray' pixels are actually used to good effect in <A
HREF="../transform/#edge_jitter" >Edge Outlines from Anti-Aliased Shapes</A> to
generate a smooth edge or outline from an image shape. </P>
This side-effect of saving the alpha channel, has particular benefits when
Using the <A HREF="../transform/#shade" >Shade Operator</A>, which does not
understand or use the alpha channel of an image. See the sub-section, <A
HREF="../transform/#shade_mask" >Masking Shaded Shapes</A>. </P>
<A NAME="alpha_copy"></A>
<H4>Alpha Copy</H4>
The '<CODE>Copy</CODE>' method is the reverse of '<CODE><A
HREF="#alpha_extract" >Extract</A></CODE>', and essentially performs a <A
HREF="../compose/#copyopacity" >CopyOpacity</A> against itself. That is, it
will turn a gray-scale image (regardless if its alpha channel is enabled or
not) into a shape mask image.
<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 alpha_extract.png -alpha copy alpha_copy.png
</samp></pre></TD></TR></TABLE>
<A HREF="alpha_extract.png"
><IMG SRC="alpha_extract.png" WIDTH=70 HEIGHT=70
ALIGN=middle VSPACE=2 HSPACE=15 BORDER=1 ALT="[IM Output]"></A>
<IMG SRC="../img_www/right.gif" ALIGN=middle WIDTH=20 HEIGHT=20 ALT="==>">
<A HREF="alpha_copy.png"
><IMG SRC="alpha_copy.png" WIDTH=70 HEIGHT=70
ALIGN=middle VSPACE=2 HSPACE=15 BORDER=1 ALT="[IM Output]"></A>
</DIV></P>
It does not matter if the image had an existing alpha channel or not, all it
does is create the images transparency from the image gray-scale values. </P>
Once you have a shape mask, you can use various <A
HREF="../color_mods/#tinting" >Color Tinting</A> or <A
HREF="../compose/#duff-porter" >Duff-Porter</A> alpha composition methods, to
color it. For examples of using a shape mask see <A HREF="#shapes" >Masks as
Colored Shapes</A>. </P>
<A NAME="alpha_shape"></A>
<H4>Alpha Shape</H4>
To make use of a gray-scale image easier, the '<CODE>Shape</CODE>' method not
only creates a shape mask (as per <A HREF="#alpha_extract" >Alpha Extract</A>,
but will also color it using the current background color.
<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 alpha_extract.png -background Yellow -alpha shape alpha_shape.png
</samp></pre></TD></TR></TABLE>
<A HREF="alpha_extract.png"
><IMG SRC="alpha_extract.png" WIDTH=70 HEIGHT=70
ALIGN=middle VSPACE=2 HSPACE=15 BORDER=1 ALT="[IM Output]"></A>
<IMG SRC="../img_www/right.gif" ALIGN=middle WIDTH=20 HEIGHT=20 ALT="==>">
<A HREF="alpha_shape.png"
><IMG SRC="alpha_shape.png" WIDTH=70 HEIGHT=70
ALIGN=middle VSPACE=2 HSPACE=15 BORDER=1 ALT="[IM Output]"></A>
</DIV></P>
This means you can very quickly color a gray-scale mask simply by shaping the
image, then flattening it onto a different background color
<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 alpha_extract.png -background Yellow -alpha shape \
-background Blue -alpha remove alpha_colormask.png
</samp></pre></TD></TR></TABLE></TD><TD>
<A HREF="alpha_colormask.png"
><IMG SRC="alpha_colormask.png" WIDTH=70 HEIGHT=70
ALIGN=middle VSPACE=0 HSPACE=5 BORDER=1 ALT="[IM Output]"></A>
</TD></TR></TABLE></P>
<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>
Background is not actually the right color to use for this 'shape' coloring
operation. It should be using the 'fill' color to set the shapes foreground
color. As such which color should be used is likely to change. Background
is only used due to internal difficulties in accessing the current fill
color. This change will likely happen as part of IMv7.
</I></FONT></TD></TR></TABLE></P>
Of course a faster and better way to map a black and white image, directly to
specific colors is by using the more specialised <A
HREF="../color_mods/#level-colors" >Level Adjustment by Color</A>. This will
avoid the need to enable or even modify the existing images transparency
channel.
<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 alpha_extract.png +level-colors Blue,Yellow level_color.png
</samp></pre></TD></TR></TABLE></TD><TD>
<A HREF="level_color.png"
><IMG SRC="level_color.png" WIDTH=70 HEIGHT=70
ALIGN=middle VSPACE=0 HSPACE=5 BORDER=1 ALT="[IM Output]"></A>
</TD></TR></TABLE></P>
<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 above will map the colors using a linear colorspace, and may need to be
converted to sRGB at some point to get a more visually correct gradient of
colors.
</I></FONT></TD></TR></TABLE></P>
<A NAME="alpha_remove"></A>
<H4>Alpha Remove</H4>
The "<CODE><A HREF="../option_link.cgi?alpha" >-alpha</A>
<B>Remove</B></CODE>" method (added to IMv7.7.5) is designed to remove the
transparency from an image, using the current "<CODE><A
HREF="../option_link.cgi?background" >-background</A></CODE>".
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 width="100%" ALIGN=center>
<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 moon.png -background tan -alpha remove alpha_remove.png
</samp></pre></TD></TR></TABLE></TD><TD>
<A HREF="alpha_remove.png"
><IMG SRC="alpha_remove.png" WIDTH=70 HEIGHT=70
ALIGN=middle VSPACE=0 HSPACE=5 BORDER=1 ALT="[IM Output]"></A>
</TD></TR></TABLE></P>
Note that while transparency is 'removed' the alpha channel will remain turned
on, but will now be fully-opaque. If you no longer need the alpha channel you
can then use <A HREF="#alpha_off" >Alpha Off</A> to disable it. </P>
This operation is simple and fast, and does the job without needing any extra
memory use, or other side effects that may be associated with alternative
transparency removal techniques. It is thus the preferred way of removing image
transparency. </P>
For other techniques, or if your ImageMagick is older that v6.7.5, then look
at the larger discussion <A HREF="#remove" >Removing Transparency from
Images</A>) below. </P>
<A NAME="alpha_background"></A>
<H4>Alpha Background</H4>
As of IM v6.5.2-10, a '<CODE>Background</CODE>' method was made available that
will set the hidden color of fully-transparent pixels to the current
background color. </P>
Normally this color is of no consequence, as it can only be seen if the alpha
channel is <A HREF="#alpha_off" >turned off</A>. However the color of
fully-transparent pixels is saved in PNG Image file format, and for large
images, having random unknown fully-transparent colors can significantly
effect its compression handling. </P>
See <A HREF="../formats/#png_compress" >PNG with Better Compression</A> and
the IM Forum Discussion <A HREF="../forum_link.cgi?t=13746" >Eliminating alpha
channel garbage</A> for more details. </P>
Note that no color mixing is applied, only a direct color assignment to any
fully-transparent color. The pixels however will still remain
fully-transparent, and as such you will see not change to the image. </P>
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 width="100%"> <TR valign="top"><TD
width="100%" ALIGN=justify>
For example, here I use it to set all fully-transparent pixels to
'<CODE>HotPink</CODE>'.
<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 moon.png -background HotPink -alpha Background moon_hotpink.png
</samp></pre></TD></TR></TABLE></TD><TD>
<A HREF="moon_hotpink.png"
><IMG SRC="moon_hotpink.png" WIDTH=70 HEIGHT=70
ALIGN=middle VSPACE=0 HSPACE=5 BORDER=1 ALT="[IM Output]"></A>
</TD></TR></TABLE></P>
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 width="100%">
<TR VALIGN=bottom><TD width="100%" ALIGN=justify>
As you can see this made no change to the actual look of the image. </P>
To see the change we will now <A HREF="#alpha_off" >turn off</A> the alpha
channel.
<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 moon_hotpink.png -alpha off moon_hotpink_off.png
</samp></pre></TD></TR></TABLE></TD><TD>
<A HREF="moon_hotpink_off.png"
><IMG SRC="moon_hotpink_off.png" WIDTH=70 HEIGHT=70
ALIGN=middle VSPACE=0 HSPACE=5 BORDER=1 ALT="[IM Output]"></A>
</TD></TR></TABLE></P>
<DIV ALIGN=center><B>
This is not the same as <A HREF="#alpha_remove" >Removing Transparency</A>
</B></DIV></P>
The edges of the shape will have made all semi-transparent pixels opaque, and
as a result produced some strong aliasing (stair-cased) edge effects. </P>
Note that even the normally opaque only format PNG24, can still save boolean
transparency if all the fully transparent colors are the same. For details
see the example in <A HREF="../formats/#png_formats" >PNG Sub-Formats</A>. </P>
This process of replacing the colors is actually almost the same as doing
a "<CODE>-channel RGB -fill <I>color</I> -opaque None +channel</CODE>". See
<A HREF="../color_basics/#replace" >Direct Color Replacement</A>. </P>
Note that many other image processing operators will also magick any
fully-transparent pixels, to fully-transparent black (color
'<CODE>None</CODE>'), as this is the color equivalent of a mathematical zero.
Here is a summary of some image operations that are known to do this, though
none are as direct or as fast as using this 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 moon.png \( +clone -alpha off \) \
-compose SrcIn -composite moon_black.png
magick moon.png -channel RGBA -blur 1x.000000001 moon_black.png
magick moon.png -channel RGBA -gaussian 1x0 moon_black.png
magick moon.png -fuzz 0% -transparent none moon_black.png
</samp></pre></TD></TR></TABLE>
</DIV></P>
That last method (see <A HREF="../color_basics/#fuzz_alpha" >Fuzz Factor and
Transparent colors</A> is particularly useful as you cannot only set all
transparent colors to full-transparent-black ('<CODE>None</CODE>'), but also
all near-fully-transparent colors (which otherwise does have a valid but
practically invisible color), simply by specifying a fuzz factor. It will
produce some data loss, but may improve compression in images with lots of
near-fully-transparent colors. Often these nearly total transparent pixels can
have very odd or wrong colors, and this method will allow you to remove such
odd pixels before they cause other problems. </P>
<A NAME="remove"></A>
<H3>Removing Transparency from Images</H3>
While the <A HREF="#alpha_off" >Alpha Off</A> will simply flip a switch and
turn off the transparency channel. You can also get the same effect if you
attempt to save the image into a file format that does not allow the use of
transparency. For example by saving to JPEG...
<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 -size 70x60 xc:none -font Candice -pointsize 50 \
-fill Black -annotate +10+45 'A' -channel RGBA -blur 0x5 \
-fill white -stroke black -draw "text 5,40 'A'" a.png
magick a.png a.jpg
</samp></pre></TD></TR></TABLE>
<A HREF="a.png"
><IMG SRC="a.png"
ALIGN=middle VSPACE=5 HSPACE=15 BORDER=0 ALT="[IM Output]"></A>
<IMG SRC="../img_www/right.gif" ALIGN=middle WIDTH=20 HEIGHT=20 ALT="==>">
<A HREF="a.jpg"
><IMG SRC="a.jpg"
ALIGN=middle VSPACE=5 HSPACE=15 BORDER=1 ALT="[IM Output]"></A>
</DIV></P>
Remember the <A HREF="../formats/#jpg" >JPEG File Format</A>, does not save
the alpha (transparency) channel, and as such simply turned it off. </P>
In this case transparent parts just became black (a typical result). But
depending on the image source the transparent areas could have just as easily
become some other random, or other inappropriate color. </P>
Also in many cases semi-transparent pixels can have some very odd colors that
is typically not visible because they are almost completely transparent.
Simply turning off transparency will make these pixels stand out like a sore
thumb, making the result look even worse than you may have expected. See for
example the top-left edges of 'A' in the above. </P>
In either case simply turning off transparency is typically NOT what is
wanted. </P>
The <I>best solution</I> is to use the <A HREF="#alpha_remove" >Alpha
Remove</A> method to quickly and simply replace the transparency with
a background color underlay...
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 width="100%" ALIGN=center>
<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 a.png -background skyblue -alpha remove -alpha off a_remove.jpg
</samp></pre></TD></TR></TABLE></TD><TD>
<A HREF="a_compose.jpg"
><IMG SRC="a_compose.jpg" WIDTH=70 HEIGHT=60
ALIGN=middle VSPACE=0 HSPACE=5 BORDER=1 ALT="[IM Output]"></A>
</TD></TR></TABLE></P>
Strictly speaking the <A HREF="#alpha_off" >Alpha Off</A> is not needed in
this case, as the save to JPEG does this automatically. </P>
<BR>
Alternative techniques of removing transparency, is to somehow generate a new
'background' or 'canvas' image and <A HREF="../compose/#over" >Over
Compose</A> your image onto that background so that the transparency is
replaced. Preferably while preserving the original image's meta-data, such as
profiles, labels, captions and comments that may be present. </P>
Methods for generating such a canvas is exampled in <A HREF="../canvas/#blank"
>Creating Image Canvases of Same Size</A>. Here is one such method...
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 width="100%" ALIGN=center>
<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 a.png \( +clone -alpha opaque -fill SkyBlue -colorize 100% \) \
+swap -geometry +0+0 -compose Over -composite \
-alpha off a_compose.jpg
</samp></pre></TD></TR></TABLE></TD><TD>
<A HREF="a_compose.jpg"
><IMG SRC="a_compose.jpg" WIDTH=70 HEIGHT=60
ALIGN=middle VSPACE=0 HSPACE=5 BORDER=1 ALT="[IM Output]"></A>
</TD></TR></TABLE></P>
Other simpler ways to do this, is to use an operation that internally creates
a '<I>cloned background canvas</I>' for you, generating it as part of the
larger image processing operation that the operator is performing. </P>
The most common method is to <A HREF="../layers/#flatten" >Flatten</A> the
image. This operator is so often used for this purpose that the process of
removing transparency has often erroneously been called 'flattening'. For
example...
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 width="100%" ALIGN=center>
<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 a.png -background skyblue -flatten -alpha off a_flatten.jpg
</samp></pre></TD></TR></TABLE></TD><TD>
<A HREF="a_flatten.jpg"
><IMG SRC="a_flatten.jpg" WIDTH=70 HEIGHT=60
ALIGN=middle VSPACE=0 HSPACE=5 BORDER=1 ALT="[IM Output]"></A>
</TD></TR></TABLE></P>
However this will not work with "<CODE><A HREF="../basics/#mogrify"
>mogrify</A></CODE>" or with a sequence of multiple images, basically because
the "<CODE><A HREF="../option_link.cgi?flatten">-flatten</A></CODE>" operator
is really designed to merge multiple images into a single image. </P>
The other common method that does work with multiple images is to give the
image a zero sized <A HREF="../crop/#border" >Border</A> with the appropriate
"<CODE><A HREF="../option_link.cgi?bordercolor">-bordercolor</A></CODE>". For
example...
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 width="100%" ALIGN=center>
<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 a.png -bordercolor skyblue -border 0 -alpha off a_border.jpg
</samp></pre></TD></TR></TABLE></TD><TD>
<A HREF="a_border.jpg"
><IMG SRC="a_border.jpg" WIDTH=70 HEIGHT=60
ALIGN=middle VSPACE=0 HSPACE=5 BORDER=1 ALT="[IM Output]"></A>
</TD></TR></TABLE></P>
Other image processing operators which are closely related to the above
methods, can also remove transparency from an image. These include: <A
HREF="../layers/#mosaic" >Mosaic</A>, <A HREF="../layers/#merge" >Merge</A>,
and <A HREF="../crop/#frame" >Frame</A>. </P>
The <A HREF="../crop/#extent" >Extent</A> operator can also be used, and
allows you to expand or crop images at the same time as you remove the
transparency, but only if you know the size of final image you desire. </P>
You do not have to replace transparency with a solid color. If you use a DIY
composition (as shown above) you can use any image for the replacement
background. One simple example of this is to use the "<CODE><A
HREF="../basics/#composite" >composite</A></CODE>" command to <A
HREF="../compose/#tile" >Tile</A> an image 'under' the original, (using <A
HREF="../compose/#dstover" >Dst_Over</A>). This compose method ensures the
original images meta-data and size is preserved.