-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
2916 lines (2761 loc) · 252 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>Common Formats -- 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/formats/">
</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"> Common Image Formats</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="#summary"><img src="../img_www/granitesm_right.gif" border="0" width="15" height="15"> A Brief Summary of Common Image File Formats</a>
</dd>
<dd>
<a href="#gif"><img src="../img_www/granitesm_right.gif" border="0" width="15" height="15"> GIF Image File Format</a>
<ul>
<li>
<a href="#gif_colors">GIF Limited Color Table</a>
</li>
<li>
<a href="#gif_trans">GIF Transparency Color</a>
</li>
<li>
<a href="#boolean_trans">GIF Boolean Transparency</a>
</li>
<li>
<a href="#bgnd">GIFs on a solid color background</a>
</li>
<li>
<a href="#bg_pattern">GIFs on a background pattern</a>
</li>
<li>
<a href="#dither">GIFs for non-specific backgrounds</a> (transparency dithering)
</li>
<li>
<a href="#gif_non-im">Non-ImageMagick GIF Processing</a>
</li>
<li>
<a href="#gif_offsets">GIF Offset Handling</a>
</li>
<li>
<a href="#gif87">Related GIF Output Formats - GIF87</a>
</li>
</ul>
</dd>
<dd>
<a href="#jpg"><img src="../img_www/granitesm_right.gif" border="0" width="15" height="15"> JPEG Image File Format</a>
<ul>
<li>
<a href="#jpg">JPEG compression</a>
</li>
<li>
<a href="#jpg_trans">JPEG transparency - NOT</a>
</li>
<li>
<a href="#jpg_color">JPEG Color Distortion</a>
</li>
<li>
<a href="#jpg_read">Reading JPEG images</a>
</li>
<li>
<a href="#jpg_write">Writing JPEG images</a>
</li>
<li>
<a href="#jpg_formats">Related JPEG Output Formats</a> (A quick summary)
</li>
<li>
<a href="#jpg_non-im">Non-ImageMagick JPEG Processing</a> (A quick summary)
</li>
<li>
<a href="#jpg_lossless">Lossless JPEG Processing</a>
</li>
</ul>
</dd>
<dd>
<a href="#png"><img src="../img_www/granitesm_right.gif" border="0" width="15" height="15"> PNG Image File Format</a>
<ul>
<li>
<a href="#png_quality">PNG compression</a>
</li>
<li>
<a href="#png_compress">PNG better compression</a>
</li>
<li>
<a href="#png_www">PNG, Web Browsers and Transparency</a>
</li>
<li>
<a href="#png_offsets">PNG and the Virtual Canvas</a>
</li>
<li>
<a href="#png_density">PNG Resolution, Density and Units</a>
</li>
<li>
<a href="#png_formats">PNG Sub-Formats</a>
</li>
<li>
<a href="#png_write">Writing PNG Image Controls</a>
</li>
<li>
<a href="#png_non-im">Non-ImageMagick PNG Processing</a>
</li>
</ul>
</dd>
<dd>
<a href="#profiles"><img src="../img_www/granitesm_right.gif" border="0" width="15" height="15"> Image Profiles</a>
<ul>
<li>
<a href="#color_profile">Changing Colorspace of an Image</a> -- RGB <=> CMYK conversion
</li>
<li>
<a href="#profile_iptc">IPTC Profiles</a>
</li>
</ul>
</dd>
<dd>
<a href="#vector"><img src="../img_www/granitesm_right.gif" border="0" width="15" height="15"> A Word about Vector Image Formats</a>
</dd>
<dd>
<a href="#other"><img src="../img_www/granitesm_right.gif" border="0" width="15" height="15"> Other Image File Formats</a>
<dl>
<dd>
<a href="#ps">Postscript (PS), Encapsulated PS (EPS), PDF</a>,<br>
<a href="#pbmplus">PbmPlus/NetPBM (PBM, PGM, PPM, PNM, PAM)</a>,<br>
<a href="#tiff">TIFF</a>, <a href="#bmp">BMP</a>, <a href="#ico">ICO</a>, <a href="#crw">RAW Digital Image (CRW,CR2,etc)</a>,<br>
<a href="#mpeg">MPEG, M2V and AVI</a>, <a href="#mng">MNG</a>, <a href="#dpx">Digital Picture Exchange (DPX)</a>,<br>
<a href="#psd">PSD</a>, <a href="#wmf">WMF</a>, <a href="#swf">MacroMedia Flash (SWF)</a>,<br>
<a href="#html">Webpage HTML Conversion</a>, <a href="#pcl">PCL Printing Format</a>,<br>
<a href="#pcd">Kodak PhotoCD Format (PCD)</a>, <a href="#rgb">Raw RGB Data</a>,
</dd>
</dl>
</dd>
</dl>Many of the image file formats have particularities which you need to keep in mind when using that format. This page deals with these special needs, and ways to improve results in those formats.
<hr>
<!-- ---------------------------------------------------------------- -->
<a name="summary" id="summary"></a>
<h2>A Brief Summary of Common Image File Formats</h2>For an introduction to reading and writing image formats see <a href="../files/">Image File Formats</a>. While a list of all the ImageMagick file formats are given on the <a href="https://imagemagick.org/script/formats.php">IM Image Formats Page</a>. Here is a very quick summary of the most common 'normal' image file formats, as well as their general advantages and disadvantages...
<dl>
<dt>
<a href="#gif"><b>GIF</b></a>
</dt>
<dd>This format is extremely common, and has been around for so long that all image handling programs understand it. But only uses a limited number of colors (a 256 color table) and only saves using 8 bit quality. However its built-in run-length encoding allows it to save images with only a few colors very efficiently. While the format has transparency, it only understands Boolean (on/off) transparency and so consequently suffers from 'aliasing' or 'jaggies'. Plain text with thin lines suffers badly when saved as a transparent GIF image. The only solution to this problem is to tie the GIF image to a specific background of the web page in which it is used. The GIF format can save multiple images to form an animation sequence, and for this purpose also saves the image canvas size and offset (page) information. Note however that negative offsets are not supported, and attempts to do so resets that offset to zero. Its best used for small images of cartoons, line drawings, and small icons, all of which have limited colors, and will allow it to compress well. Its use however should be avoided when a newer format like PNG is available.</dd>
<dt>
<a href="#jpg"><b>JPEG</b></a>
</dt>
<dd>Does not handle transparency at all. The image is equivalent to using "<code><a href="https://imagemagick.org/script/command-line-options.php?#alpha">-alpha off</a></code>" operation to remove the alpha channel, so any background transparency commonly becomes black depending on the image processing used to generate the image. This format is also 'lossy', producing edge effects on sharp lines and borders and thus should not be used for any intermediate image processing, or storage of image originals (unless they were already in this format). It is well suited to long term storage of real life photographs, but avoid it if you plan to further process the image, or the image contains large areas of solid colors.</dd>
<dt>
<a href="#png"><b>PNG</b></a>
</dt>
<dd>This format is intended to eventually replace older formats like GIF and TIFF. It is a modern format capable of handling 16 bit quality with four color channels allowing the full use of semi-transparent colors. It also includes a huge number of lossless image compression options. Its biggest disadvantage is that it is still relatively new, such that the Microsoft IE (v6) web browser does not automatically handle it correctly. However a fix is available for this problem. The format does not save canvas size information (where GIF does), but it does save the canvas offsets and even negative offsets (which GIF does not), though some web browsers have problems when a negative offset is used, so this is not recommended for a final image to be displayed in a browser. For saving intermediate 'layered' images, the ability to save negative offsets can be very important and is often much more important than its not saving canvas size information.</dd>
<dt><b>MNG</b></dt>
<dd>This is the multi-image format for PNG, and allows animations to movie quality levels and speed. <i>A simple example of using MNG is wanted, so if you have one mail me.</i> The MNG animation format appears to becomeing obsolete and has been abandoned by some web broswers such as FireFox.</dd>
<dt>
<a href="#tiff"><b>TIFF</b></a>
</dt>
<dd>
This is the Image interchange format that was developed to transfer high quality images between programs before any serious image formats were available. Unfortunately, because of this beginning, the format has been modified with a haphazard array of features and compression styles and no programs understands them all. The format is now pretty well only use by "<code><a href="http://www.adobe.com/products/photoshop/">Photoshop</a></code>" on windows platforms, and this is the only source that provides any sort of standard reference for the TIFF image format. TIFF files can handle multiple images, though few applications other than IM handle multiple image TIFFs. Generally, unless the internal format of the TIFF image is kept relatively basic, there is no guarantee that a TIFF file generated by one program will be usable by another program, including IM or even "<code><a href="http://www.adobe.com/products/photoshop/">Photoshop</a></code>" itself. As such I do not recommend this format period! I suggest you use some other format than TIFF (or JPEG), especially for long term storing of images. The few notes I have on this format and its problems are provided below in the <a href="#tiff">Miscellaneous Formats, TIFF</a> section. These usage notes were found in the IM mailing lists and forums, as I myself don't use or need to use TIFF.
</dd>
<dt><b>Video Formats</b></dt>
<dd>
Other movie quality animation formats generally based on using lossy compression to reduce the size (and quality) of the movie. Both formats are in a constant state of flux, improvements and security limiting features, making any form of processing difficult. At last count there was more than 200 video format 'codecs' that are in general use for one purpose or another. Because of this IM does not directly handle this format, instead it relies on other software packages, to handling the processing of the individual frames into and out of the animations. These 'delegate' programs include "<code>mpeg2decode</code>", "<code>mpeg2encode</code>", and "<code>mplayer</code>". See <a href="#mpeg">MPEG, M2V, and AVI</a> below).
</dd>
</dl>
<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>Some system (like ubuntu) disable the use specific image file formats using a security policy. Type <code>magick -list policy</code> to see what policies and where they are set from are present on your system.</i></font></td>
</tr>
</table>
<hr>
<!-- ---------------------------------------------------------------- -->
<a name="gif" id="gif"></a>
<h2>GIF Image File Format</h2>The GIF format is a very widely known image file format, as it has been around for a very very very long time (from the late 1980's). It is often picked for images which are to be displayed on web pages that involve transparency or image animation. It is also about the only format absolutely universally understood by all web browsers. Unfortunately it is not a very good format for anything but line drawings, figures, diagrams, and cartoons. That is, because it is limited to a maximum of 256 colors, one of which is usually flagged as being transparent. Flagging one specific color in the image as transparent has some drawbacks. If the color to use as transparent is badly chosen, it can result in other parts of the image being transparent when that was not intended. Care must be taken to ensure that does not happen. Further more, the transparency ability is 'Boolean', which basically means it is either fully on, or fully off. Semi-transparent colors are just not possible, and if present need to be made either transparent or opaque. That means the format can not provide any form of anti-aliasing of edges of an image, usually resulting in a bad case of the 'jaggies'. (See <a href="../antialiasing/">Anti-Aliasing</a>) Because the "GIF" image formats color limitations causes so many problems, especially from a high quality image processing package like ImageMagick, I would like to say up front...
<div align="center">
<b>Avoid GIF format, if at all possible.<br>
If you must use it, do so only as the final step.</b>
</div>Finally for a long time the compression algorithm used by GIF was patented. Consequently it was not available for use by many image processing programs, such as ImageMagick. Thus very old IM programs will output GIF format images un-compressed, and thus using more disk space than it should. You can fix this using a GIF batch compression program such as "<code><b><a href="http://www.lcdf.org/gifsicle/">Gifsicle</a></b></code>" or "<code><b><a href="http://utter.chaos.org.uk/~pdh/software/intergif.htm">InterGIF</a></b></code>". However as the patent expired completely in mid-2004, the current release of IM has the GIF image compression re-enabled again. The image compression is also rather simple, and works best on images with large areas of solid, unchanging colors. Or on simple repeated patterns of the same set of colors, such as you get using <a href="../quantize/#ordered-dither">Ordered Dithering</a> (not the default dither in IM). Finally GIF images can save multiple images in the one file. And this is used to generate <a href="../anim_basics/">GIF Animations</a> as understood by pretty well all web browsers, since the technique was first introduction by the very old "Netscape" browser. <b>In Summary</b> The GIF image file format with its limited color table, Boolean transparency, and simplistic compression (if enabled), makes it ideal for small images, such as thumbnails, and especially "cartoon-like" icons, logos, and symbols images with large areas of solid colors. Its animation abilities also make it an ideal method of generating flashy attention grabbing logos and advertisements you see all over the World Wide Web. For anything else its limitations make it a poor image file format and you may be better moving to JPEG, PNG, or a video image format for your needs. <a name="gif_colors" id="gif_colors"></a>
<h2>GIF Limited Color Table</h2>
<pre>
FUTURE: color reduction examples -- reference basic color dithering
Ensuring that a specific color is present in the final GIF image
Map color tables to color reduce.
See <a href="../quantize/#colors">Color Quantization</a>.
</pre>See <a href="../advanced/#3d-bullets">Advanced 3-D Bullet Scripting</a> for an example of generating multiple images over a range of colors. This technique can also be used to auto-magick your image into multiple images for many different backgrounds colors and patterns. <!-- <CODE EXECUTE ASSERT>
[ `magick -size 1x256 gradient: gif:- |\
magick identify -format %k - ` -eq 256 ] || echo >&2 \
"ASSERTION FAILURE: GIF did not save a 256 color gradient"
[ `magick -size 1x256 gradient: -transparent Gray30 gif:- |\
magick identify -format %k - ` -eq 256 ] || echo >&2 \
"ASSERTION FAILURE: GIF did not save a 256 color gradient (with trans)"
</CODE> -->
<a name="gif_trans" id="gif_trans"></a>
<h2>GIF Transparency Color</h2>For example, here we use identify to extract the transparent color, and the color table a particular GIF image file used to represent transparency. The perl script extracts just the specific fields of interest (which can be multi-line).
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr valign="top">
<td width="10%" align="justify" rowspan="2"></td>
<td width="90%" align="justify">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre><code execute="" noimage="" out="hand_point.txt">
magick identify -verbose hand_point.gif |\
perl -0777 -ne 's/^ //gm; \
print $& while /^(Colors|Alpha|Colormap):.*?(?=^\S)/gms'
</code></pre>
</td>
</tr>
</table>
</td>
<td align="center" rowspan="2">
<a href="../images/hand_point.gif"><img src="../images/hand_point.gif" align="middle" vspace="0" hspace="5" border="1" alt="[IM Output]"></a>
</td>
</tr>
<tr>
<td>
<table border="0" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<a href="hand_point.txt"><img src="hand_point.txt.gif" align="middle" vspace="0" hspace="0" border="0" alt="[IM Output]"></a>
</td>
</tr>
</table>
</td>
</tr>
</table>As you can see, a transparent grey color ('<code>#CCCCCC00</code>') was used for this image and this color has its own separate entry in the color table. You can also see that even though this image only uses 5 colors (one transparent), the color table used is for 8 colors. that is because the GIF file format can only use a color table that is a power of 2 in size. That is the color table is always 2, 4, 8, 16, 32, 64, 128 or 256 color entries in size. As such the last 3 color table entries are not used. Actually they are just not refered to. In some cases these unused entries may not be the last three entries in the color table, and could actually contain any color value. You can also actually have duplicate color values, though IM typically removes any such duplicate color entries if it processes the image in some way. As of IM version 6.2.9-2 (and in some older versions), IM will preserve the color table, and more specifically the transparent color value, whenever it reads, processes and writes a GIF image.
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr valign="top">
<td width="10%" align="justify" rowspan="2"></td>
<td width="90%" align="justify">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre><code execute="" noimage="" out="hand_white.txt">
magick hand_point.gif -fill white -opaque wheat hand_white.gif
magick identify -verbose hand_white.gif |\
perl -0777 -ne 's/^ //gm; \
print $& while /^(Colors|Alpha|Colormap):.*?(?=^\S)/gms'
</code></pre>
</td>
</tr>
</table>
</td>
<td align="center" rowspan="2">
<a href="hand_white.gif"><img src="hand_white.gif" align="middle" vspace="0" hspace="5" border="1" alt="[IM Output]"></a>
</td>
</tr>
<tr>
<td>
<table border="0" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<a href="hand_white.txt"><img src="hand_white.txt.gif" align="middle" vspace="0" hspace="0" border="0" alt="[IM Output]"></a>
</td>
</tr>
</table>
</td>
</tr>
</table>As you can see even though the image was modified (all '<code>wheat</code>' color pixels were replaced with a '<code>white</code>' color) the transparent color used was preserved However if the final image has no transparency, the transparency color entry ('<code>Alpha:</code>') in the color table is completely removed.
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr valign="top">
<td width="10%" align="justify" rowspan="2"></td>
<td width="90%" align="justify">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre><code execute="" noimage="" out="hand_flatten.txt">
magick hand_point.gif -background white -flatten hand_flatten.gif
magick identify -verbose hand_flatten.gif |\
perl -0777 -ne 's/^ //gm; \
print $& while /^(Colors|Alpha|Colormap):.*?(?=^\S)/gms'
</code></pre>
</td>
</tr>
</table>
</td>
<td align="center" rowspan="2">
<a href="hand_flatten.gif"><img src="hand_flatten.gif" align="middle" vspace="0" hspace="5" border="1" alt="[IM Output]"></a>
</td>
</tr>
<tr>
<td>
<table border="0" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<a href="hand_flatten.txt"><img src="hand_flatten.txt.gif" align="middle" vspace="0" hspace="0" border="0" alt="[IM Output]"></a>
</td>
</tr>
</table>
</td>
</tr>
</table>If you like to change the transparent color that the GIF file format is using, you can use the "<code><a href="https://imagemagick.org/script/command-line-options.php?#transparent-color">-transparent-color</a></code>" output setting (added IM v6.2.9-2). For example...
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr valign="top">
<td width="10%" align="justify" rowspan="2"></td>
<td width="90%" align="justify">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre><code execute="" noimage="" out="hand_wheat.txt">
magick hand_point.gif -transparent-color wheat hand_wheat.gif
magick identify -verbose hand_wheat.gif |\
perl -0777 -ne 's/^ //gm; \
print $& while /^(Colors|Alpha|Colormap):.*?(?=^\S)/gms'
</code></pre>
</td>
</tr>
</table>
</td>
<td align="center" rowspan="2">
<a href="hand_wheat.gif"><img src="hand_wheat.gif" align="middle" vspace="0" hspace="5" border="1" alt="[IM Output]"></a>
</td>
</tr>
<tr>
<td>
<table border="0" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<a href="hand_wheat.txt"><img src="hand_wheat.txt.gif" align="middle" vspace="0" hspace="0" border="0" alt="[IM Output]"></a>
</td>
</tr>
</table>
</td>
</tr>
</table>As you can see even though the result is not visibly different from the original, the transparent color was changed to a fully-transparent version of the '<code>wheat</code>' color. If you look closely you will also see that the image now has two '<code>wheat</code>' or '<code>#F5DEB3</code>' colors in its color table. That is, one transparent wheat and one opaque wheat. As of IM version 6.2.9-2, this presents no problem. Though only one transparent color can be defined by the GIF image file format. Why would you do that? Because some very old web browsers and graphic programs do not understand GIF transparency. So this option lets you set what color the transparent areas should be in that situation. Typical choices for the transparent color are '<code>white</code>' for modern browsers, OR more typically '<code>grey75</code>' ('<code>#BFBFBF</code>'), which was the original "<code>mosaic</code>" web browser page color. Other popular transparent color choices are '<code>grey</code>' ('<code>#BEBEBE</code>'), and '<code>silver</code>' ('<code>#C0C0C0</code>') which is what the 'hand' image above used. This shows just how popular that specific area of the gray-scale color range is for the transparent color. <i>FUTURE: add link to color selection.</i>
<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.2.9-2, and the creation of the "<code><a href="https://imagemagick.org/script/command-line-options.php?#transparent-color">-transparent-color</a></code>" output setting, IM would typically save the transparency of an image as the special color '<code>none</code>' (fully-transparent black), which is not particularly nice when transparency fails.</i></font></td>
</tr>
</table>Note that setting "<code><a href="https://imagemagick.org/script/command-line-options.php?#transparent-color">-transparent-color</a></code>" does NOT add any transparency to a GIF image, nor does it magick the specified color to become transparent. All the option does is specify what color should placed in the color table for the color index that is used representing the transparent colors in a GIF image. If you want to change a specific (exact) color to become transparent, then use the "<code><a href="https://imagemagick.org/script/command-line-options.php?#transparent">-transparent</a></code>" <a href="../color_basics/#replace">Color Replacement Operator</a>. <a name="trans" id="trans"></a> <a name="boolean_trans" id="boolean_trans"></a>
<h2>GIF Boolean Transparency</h2>Because the GIF format does NOT understand semi-transparent colors, and as ImageMagick by default generates semi-transparent color as part of its normal <a href="../antialiasing/">Anti-Aliasing Methods</a>, when you save a image to this format it will often come out horrible looking. For example, here I draw a simple black circle on a transparent background. Also I will generate an enlarged view of the edge of the images, to make it clear what is happening. First I will output using the PNG format...
<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 -size 60x60 xc:none -fill white -stroke black \
-draw 'circle 30,30 5,20' circle.png
magick circle.png -crop 10x10+40+3 +repage -scale 600% circle_mag.png
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="circle.png"><img src="circle.png" width="60" height="60" align="middle" vspace="2" hspace="5" border="0" alt="[IM Output]"></a> <a href="circle_mag.png"><img src="circle_mag.png" width="60" height="60" align="middle" vspace="2" hspace="0" border="1" alt="[IM Output]"></a>
</td>
</tr>
</table>As you can see the edge of the circle on the left drawn (in PNG format) as a very clean looking (though slightly fuzzy) edge to the image. You can see the semi-transparent pixels in its enlargement. Now lets output the same image using the "GIF" image format...
<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 -size 60x60 xc:none -fill white -stroke black \
-draw 'circle 30,30 5,20' circle.gif
magick circle.gif -crop 10x10+40+3 +repage -scale 600% circle_mag.gif
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="circle.gif"><img src="circle.gif" width="60" height="60" align="middle" vspace="2" hspace="5" border="0" alt="[IM Output]"></a> <a href="circle_mag.gif"><img src="circle_mag.gif" width="60" height="60" align="middle" vspace="2" hspace="0" border="1" alt="[IM Output]"></a>
</td>
</tr>
</table>The result is that the circle has a very sharp stair case effects along the outside edge of the circle, while the inside remains properly anti-aliased. Basically while PNG format can save semi-transparency pixel information, GIF cannot. The GIF image format can only save a single pure transparent color. In other words...
<div align="center">
<b>GIF format has an on/off or Boolean transparency</b>
</div>If you look more closely at the resulting GIF, you will find that the semi-transparent pixels could have either become fully-transparent or full-opaque.
<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>What ImageMagick actually does with semi-transparent pixels depends on just what version of IM you are using. It was for a long time not properly defined and what a version did, often depended on the last 'bug fix' that was applied due to bug reports from users.<br>
<br>
As of v6.2.9-6 ImageMagick should by default threshold the image at a 50% level for both GIF and XPM image formats. This has become the accepted standard as used by image handlers, while still allowing you to set your own methods of dealing with the transparency problems of the GIF file format.</i></font></td>
</tr>
</table>
<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>Because of the GIF limitations, IM performs the following set of operations before saving to the GIF file format...</i></font>
<pre><font size="-1"><i> -channel A -threshold 50%
if (fully-)transparent pixels are present it then...
-quantize transparent -colors 255
otherwise if no transparent pixels present...
-colors 256</i></font></pre><font size="-1"><i>The -colors quantization process automatically does nothing if less that that many colors are present in the image. Nor will it do anything if the image has a valid colormap (as assigned by "<code>+/-map</code>").<br>
<br>
It also does not attempt to use a common color map for multi-image GIF files. As such if the colors are very different from one frame to the next, a local color table may be added to each individual image saved into the GIF file format.<br>
<br>
Also the settings used in the above are not permanent just temporary for the image being saved. That is, if you used "<code>-write image.gif</code>" the settings used during the process do not effect later operations.</i></font>
</td>
</tr>
</table>You may like to do the thresholding yourself, and this is recommended if you are not certain of what version of IM (especially older versions) you are using.
<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 -size 60x60 xc:none -fill white -stroke black \
-draw 'circle 30,30 5,20' \
-channel A -threshold 50% circle_threshold.gif
magick circle_threshold.gif -crop 10x10+40+3 +repage \
-scale 600% circle_threshold_mag.gif
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="circle_threshold.gif"><img src="circle_threshold.gif" width="60" height="60" align="middle" vspace="2" hspace="5" border="0" alt="[IM Output]"></a> <a href="circle_threshold_mag.gif"><img src="circle_threshold_mag.gif" width="60" height="60" align="middle" vspace="2" hspace="0" border="1" alt="[IM Output]"></a>
</td>
</tr>
</table>The above example performs the same "<code><a href="https://imagemagick.org/script/command-line-options.php?#threshold">-threshold</a> 50%</code>" on the alpha channel that IM now does automatically, that is if a pixel is more than 50% transparent, it will be made fully-transparent (using the color given by the "<code><a href="https://imagemagick.org/script/command-line-options.php?#transparent-color">-transparent-color</a></code>" setting if defined. However you now have control of the threshold level as you like. Thresholding the alpha channel at 50% works well for most types of images. Especially those with a simple edge, but the technique breaks down rather badly, when you need to deal with large areas of semi-transparent pixels. This is what the most of the following examples for GIF handling will look at. For example suppose we want to save an image with a large <a href="../fonts/#fuzzy_shadow">fuzzy semi-transparent shadow</a> such as this image (in PNG format)... <!-- The shadow equivalent -- not as much control over the final size.
magick -background none -fill white -stroke black \
-font Candice -pointsize 50 label:A \
\( +clone -background black -shadow 100x5+5+5 \) +swap \
-background none -flatten -crop 70x60+10+0 +repage a.png
-->
<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 -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
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="a.png"><img src="a.png" width="70" height="60" align="middle" vspace="0" hspace="5" border="0" alt="[IM Output]"></a>
</td>
</tr>
</table>If you just magick this letter directly to GIF format or even use a "<code><a href="https://imagemagick.org/script/command-line-options.php?#threshold">-threshold</a></code>" operation to control the Boolean transparency, you will be sorely disappointed.
<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 a.gif
magick a.png -channel A -threshold 75% a_threshold.gif
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="a.gif"><img src="a.gif" width="70" height="60" align="middle" vspace="0" hspace="5" border="0" alt="[IM Output]"></a> <a href="a_threshold.gif"><img src="a_threshold.gif" width="70" height="60" align="middle" vspace="0" hspace="5" border="0" alt="[IM Output]"></a>
</td>
</tr>
</table><!-- <CODE EXECUTE ASSERT>
# The number of transparent pixels in the image changes.
# from 2786 Jan06 (FC5), 2794 in Dec06 (FC6), 2775 in June07 (FC7)
# These appear to be font changes due to OS upgrades.
# (Better non-font related test needed)
a50=`magick a.png -fill black -colorize 100% \
-channel A -threshold 50% -verbose info: | \
sed -n '/Histogram:/,/Colormap:/s/^ *\([^ :]*\):.*none.*/\1/p;'`; \
[ "$a50" -lt 2750 -o "$a50" -gt 2800 ] && echo >&2 \
"ASSERTION FAILURE: 50% threshold not in expected range"; \
a=`magick identify -verbose a.gif | \
sed -n '/Histogram:/,/Colormap:/s/^ *\([^ :]*\):.*none.*/\1/p;'`; \
[ "$a" != "$a50" ] && echo >&2 \
"ASSERTION FAILURE: GIF did not threshold alpha at 50%!"
</CODE> -->
The first image is a normal save to GIF format, which as you can see thresholded the semi-transparent pixels at '<code>50%</code>', the second image was thresholded at '<code>75%</code>' allowing more semi-transparent pixels to become fully-opaque (or visible). If you just want to remove all the semi-transparent pixels (EG the shadow) you could try something like a "<code>-threshold 15%</code>", to remove just about all semi-transparent pixels.
<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 -channel A -threshold 15% a_no_shadow.gif
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="a_no_shadow.gif"><img src="a_no_shadow.gif" width="70" height="60" align="middle" vspace="0" hspace="5" border="0" alt="[IM Output]"></a>
</td>
</tr>
</table>Most other solutions to the GIF Boolean transparency problem is to inextricably tie the image to the background color of the web page on which it lives. Methods for doing this are complex and tricky, and this is what we will now look at. <a name="bgnd" id="bgnd">
<h3>GIFs on a solid color background</h3></a> What we would really like to to somehow preserve the shading of the semi-transparent and anti-aliased pixels, and still display it nicely on the WWW. To do this we have to be a little tricky. The typical solution is to match the image to the background on which you are going to display the image on. This is simple to do, just overlay the image onto a background of the appropriate color, before you save it to the GIF format. This removes the need for any form of transparency and the whole thing becomes a non-issue. Of course the limited number of colors is still an issue, but often not a big problem.
<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 LightSteelBlue -flatten a_overlay.gif
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="a_overlay.gif"><img src="a_overlay.gif" width="70" height="60" align="middle" vspace="0" hspace="5" border="0" alt="[IM Output]"></a>
</td>
</tr>
</table>See just about perfect! Of course for this method to work correctly you need to know what exactly the background color the image will be used on. Also after we are finished the image will not be much good on any other background. A big sacrifice to make. <a name="bg_pattern" id="bg_pattern">
<h3>GIFs on a background pattern</h3></a> But what if you are using some pattern for a background, instead of a simple solid color? You could try positioning the overlay onto a copy of the background pattern so that the pattern in the resulting image matches the pattern of the web page. However that would require a lot of trial and error to get the background in the image to match up with the web page. Also you could only guarantee it to work for a particular browser, and then only that specific version of the browser. Not a good idea for a web page, so don't even bother to try. I certainly won't. Instead of trying to do a perfect match-up with the background pattern, lets just overlay it onto a color that at least matches the background we intend to use. For example lets overlay our image onto a 'typical' bubble like background pattern. But first we need to know the average color of this background. A simple way to find this color is to just <a href="../resize/#scale">scale</a> the image down to a single pixel, then read the resulting color.
<div align="center">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="90%" bgcolor="#F8F8F8">
<tr>
<td>
<pre><code execute="" out="bg_color_avg.txt">
magick bg.gif -scale 1x1\! -depth 8 txt:-
</code></pre>
</td>
</tr>
</table>
<table border="0" cellspacing="0" cellpadding="5" width="90%" bgcolor="#F8F8F8">
<tr>
<td>
<pre>
<a href="bg_color_avg.txt"><img src="bg_color_avg.txt.gif" align="middle" vspace="0" hspace="0" border="0" alt="[IM Text]"></a>
</pre>
</td>
</tr>
</table>
</div>See <a href="../files/#txt">IM Pixel Enumeration Text Format</a> for more information on the special "<code>txt:</code>" output format used. Now lets set the background transparency of the image using "<code><a href="https://imagemagick.org/script/command-line-options.php?#flatten">-flatten</a></code>".
<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 '#BABBD7' -flatten a_bg.gif
</samp></pre>
</td>
</tr>
</table>
</td>
<td></td>
<td>
<table class="table table-sm table-hover table-striped" cellpadding="10" background="../images/bg.gif" align="center">
<tr>
<td>
<a href="a_bg.gif"><img src="a_bg.gif" width="70" height="60" align="middle" vspace="0" hspace="0" border="0" alt="[IM Output]"></a>
</td>
</tr>
</table>
</td>
</tr>
</table>I have setup the web page to overlay our image on that background, even though that background is NOT part of the image itself. Though the background color used matched the general color of the background pattern, it still has a very obvious rectangle of solid color, devoid of the the background pattern, around it. One practical solution is to declare the color we overlay, as the "<code><a href="https://imagemagick.org/script/command-line-options.php?#transparent">-transparent</a></code>" color in the GIF output. By doing this we remove the 'squareness' of the image. Also adding a small <a href="../color_basics/#fuzz">fuzz factor</a> improves the result and adjusts the amount of space the transparent color uses, in the same way threshold did above.
<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 '#B9BBD6' -flatten \
-fuzz 5% -transparent '#B9BBD6' a_bg_trans.gif
</samp></pre>
</td>
</tr>
</table>
</td>
<td></td>
<td>
<table class="table table-sm table-hover table-striped" cellpadding="10" background="../images/bg.gif" align="center">
<tr>
<td>
<a href="a_bg_trans.gif"><img src="a_bg_trans.gif" width="70" height="60" align="middle" vspace="0" hspace="0" border="0" alt="[IM Output]"></a>
</td>
</tr>
</table>
</td>
</tr>
</table>This is typically good enough to handle transparency in most GIF images, though it does tie the image to a specific background color. In essence we are using the transparency to set a basic outline shape to the image, rather than a true transparency. By using a color for the overlay and GIF transparency so that it matches the background pattern means it is no longer clear exactly where the image stops, and the background pattern starts.<br>
Be cautious however with the "<code><a href="https://imagemagick.org/script/command-line-options.php?#fuzz">-fuzz</a></code>" setting, as too much and you can end up with more than just the outside of your image becoming transparent!
<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 '#B9BBD6' -flatten \
-fuzz 25% -transparent '#B9BBD6' a_bg_overfuzz.gif
</samp></pre>
</td>
</tr>
</table>
</td>
<td></td>
<td>
<table class="table table-sm table-hover table-striped" cellpadding="10" background="../images/bg.gif" align="center">
<tr>
<td>
<a href="a_bg_overfuzz.gif"><img src="a_bg_overfuzz.gif" width="70" height="60" align="middle" vspace="0" hspace="0" border="0" alt="[IM Output]"></a>
</td>
</tr>
</table>
</td>
</tr>
</table>It will also fail if you used a color close to the background colour within the image itself. As such this technique is <i>not recommended</i> for general images, but only in specific cases. To solve this problem we use a '<code><a href="../draw/#alpha">-alpha floodfill</a></code>' to set the areas we want transparent.
<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 '#B9BBD6' -flatten \
-fuzz 25% -draw 'fill none alpha 0,0 floodfill' a_bg_none.gif
</samp></pre>
</td>
</tr>
</table>
</td>
<td> </td>
<td>
<table class="table table-sm table-hover table-striped" cellpadding="10" background="../images/bg.gif" align="center">
<tr>
<td>
<a href="a_bg_none.gif"><img src="a_bg_none.gif" width="70" height="60" align="middle" vspace="0" hspace="0" border="0" alt="[IM Output]"></a>
</td>
</tr>
</table>
</td>
</tr>
</table>Now as long as the borders of our image do not 'leak' we can use similar colors inside the image as our background, and not have them turn transparent on us, due to 'over fuzzing'. Of course if our image has 'holes' in it, then those holes will also have to be taken care of too. In which case the previous 'fuzzed transparency' may work better. Did say handling a GIF transparency color is easy! NOT!<br>
An alternative technique especially for images with a sharp anti-aliased edge is to simply add a minimum outline of the background color. See <a href="../masking/#outline">Outline or Halo Transparency</a>.<br>
<b>Remove the Background Color</b>... Trying an remove a specific background color from an existing GIF image is not easy. It is especially difficult if the overlaid image also contains the background color, as you then don't really know what is background and what isn't. The best solution is to get a copy of the same GIF overlay on two different and well known background colors. With two such images, you can recover the original overlay and all its semi-transparent pixels perfectly. See <a href="../masking/#two_background">Background Removal using Two Backgrounds</a>. If you don't have two such images, then you can not perfectly recover the images semi-transparency, but there are techniques that can do a reasonable though imperfect job. For this see the other sections of <a href="../masking/#bg_remove">Background Removal</a>.<br>
<a name="dither" id="dither">
<h3>GIFs for non-specific backgrounds <font size="2">(or Dithering the Transparency)</font></h3></a>
<pre>FUTURE: This will move into a more generalise (non-GIF specific), alpha
dithering section.</pre>The biggest problem with the above is that it would only work if you happened to know exactly what color the background, or background pattern your image will be used on. If you don't know all is not lost. As you saw above, threshold does not work well for an image with a very large area of transparency, such as a fuzzy shadow. But another technique known as dithering can, and does NOT require knowledge of the background it will be used on. Basically dithering limits the transparency to on/off values, creating an effect of semi-transparency over a larger area using a pattern if pixels. In other words it fakes semi-transparency. This method was exampled in what is now known as the "<a href="http://pmt.sourceforge.net/opossum/">Opossum Examples</a>". Unfortunately these examples did not actually give the commands that were used to generate the example. For completeness I will attempt to demo them again here. The "<code><a href="https://imagemagick.org/script/command-line-options.php?#monochrome">-monochrome</a></code>" operator converts all colors in an image into a pure black and white "Floyd-Steinberg error correction dither". However as it converts a grey scale image into just pure back and white colors we will need to extract an alpha channel mask from the image, dither that, and return it back into the image.
<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 -fx a -alpha off -monochrome \) \
-compose CopyOpacity -composite a_dither.gif
</samp></pre>
</td>
</tr>
</table>
</td>
<td>
<a href="a_dither.gif"><img src="a_dither.gif" width="70" height="60" align="middle" vspace="0" hspace="5" border="0" alt="[IM Output]"></a>
</td>
</tr>
</table>In a similar way, there are a couple of other dither operators which can be limited to just the alpha channel using the "<code><a href="https://imagemagick.org/script/command-line-options.php?#channel">-channel</a></code>" setting (unlike "<code><a href="https://imagemagick.org/script/command-line-options.php?#monochrome">-monochrome</a></code>").
<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 a.png -channel A -ordered-dither o2x2 a_ordered_2x2.gif
magick a.png -channel A -ordered-dither o3x3 a_ordered_3x3.gif
magick a.png -channel A -ordered-dither o4x4 a_ordered_4x4.gif
magick a.png -channel A -ordered-dither checks a_halftone_2.gif
magick a.png -channel A -ordered-dither h4x4a a_halftone_4.gif
magick a.png -channel A -ordered-dither h6x6a a_halftone_6.gif
magick a.png -channel A -ordered-dither h8x8a a_halftone_8.gif
<br> magick a.png -channel A -random-threshold 5x95% a_random_5x95.gif
<br> magick a.png -channel A -random-threshold 5x70% a_random_5x60.gif
<br> magick a.png -channel A -random-threshold 50x95% a_random_50x95.gif
<br> magick a.png -channel A -random-threshold 45x55% a_random_45x55.gif
magick a.png -channel A -random-threshold 50x50% a_random_50x50.gif
</samp></pre>
</td>
</tr>
</table><a href="a_ordered_2x2.gif"><img src="a_ordered_2x2.gif" width="70" height="60" align="middle" vspace="5" hspace="15" border="0" alt="[IM Output]"></a> <a href="a_ordered_3x3.gif"><img src="a_ordered_3x3.gif" width="70" height="60" align="middle" vspace="5" hspace="15" border="0" alt="[IM Output]"></a> <a href="a_ordered_4x4.gif"><img src="a_ordered_4x4.gif" width="70" height="60" align="middle" vspace="5" hspace="15" border="0" alt="[IM Output]"></a><br>
<a href="a_halftone_2.gif"><img src="a_halftone_2.gif" width="70" height="60" align="middle" vspace="5" hspace="15" border="0" alt="[IM Output]"></a> <a href="a_halftone_4.gif"><img src="a_halftone_4.gif" width="70" height="60" align="middle" vspace="5" hspace="15" border="0" alt="[IM Output]"></a> <a href="a_halftone_6.gif"><img src="a_halftone_6.gif" width="70" height="60" align="middle" vspace="5" hspace="15" border="0" alt="[IM Output]"></a> <a href="a_halftone_8.gif"><img src="a_halftone_8.gif" width="70" height="60" align="middle" vspace="5" hspace="15" border="0" alt="[IM Output]"></a><br>
<a href="a_random_5x95.gif"><img src="a_random_5x95.gif" width="70" height="60" align="middle" vspace="5" hspace="15" border="0" alt="[IM Output]"></a> <a href="a_random_5x60.gif"><img src="a_random_5x60.gif" width="70" height="60" align="middle" vspace="5" hspace="15" border="0" alt="[IM Output]"></a> <a href="a_random_50x95.gif"><img src="a_random_50x95.gif" width="70" height="60" align="middle" vspace="5" hspace="15" border="0" alt="[IM Output]"></a> <a href="a_random_45x55.gif"><img src="a_random_45x55.gif" width="70" height="60" align="middle" vspace="5" hspace="15" border="0" alt="[IM Output]"></a> <a href="a_random_50x50.gif"><img src="a_random_50x50.gif" width="70" height="60" align="middle" vspace="5" hspace="15" border="0" alt="[IM Output]"></a>
</div>As you can see "<code><a href="https://imagemagick.org/script/command-line-options.php?#ordered-dither">-ordered-dither</a></code>" produces a pattern of transparent and opaque colors to represent the overall transparency. This however produces a very noticeable regular pattern. However if you use a shadow color that is similar too but darker than the normal background then you can make this pattern almost completely invisible. The '<code>checks</code>' pattern (first image on second line) is of particular interest as it is a very simple 3 level pattern that is very clean, and neat.
<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>The "<code><a href="https://imagemagick.org/script/command-line-options.php?#ordered-dither">-ordered-dither</a></code>" was extended in IM v6.2.8-6 with 'half-tone' dither patterns. The operator was then completely revised for IM v6.3.0 with named dither patterns (use "<code><a href="https://imagemagick.org/script/command-line-options.php?#list">-list</a> threshold</code>" to see the full list). You can even generate your own dithering pattern to generate other special effects. See <a href="../quantize/#ordered-dither">Ordered Dithering Examples</a> and the <a href="../bugs/ordered-dither/">Ordered Dither Upgrade notes</a> for more details.<br>
<br>
Before this redevelopment, arguments could only consist of the geometry strings '<code>2x2</code>', '<code>3x3</code>' and '<code>4x4</code>' (which will still work). However, anything else was then treated as being a "<code><a href="https://imagemagick.org/script/command-line-options.php?#random-threshold">-random-threshold</a></code>" argument, usually with disastrous results. Caution is required when using this option on very old versions of IM.</i></font></td>
</tr>
</table>The "<code><a href="https://imagemagick.org/script/command-line-options.php?#random-threshold">-random-threshold</a></code>" on the other hand produces a highly variable randomized dither that is different each time it is run. The purely random nature of the this dither algorithm however tends to produce large 'clumps' of pixels, rather than the smoother, algorithmic placed dithering generated by the "Floyd-Steinberg" "<code><a href="https://imagemagick.org/script/command-line-options.php?#monochrome">-monochrome</a></code>" operator. The big advantage of "<code><a href="https://imagemagick.org/script/command-line-options.php?#random-threshold">-random-threshold</a></code>" however is the limit controls it provides. By making the parameters very restrictive (for example as '50x50%') you would magick <code><a href="https://imagemagick.org/script/command-line-options.php?#random-threshold">-random-threshold</a></code>" into a simple "<code><a href="https://imagemagick.org/script/command-line-options.php?#threshold">-threshold</a></code>" operator. By being only a little less restrictive you can randomize just the very edge of the threshold limit, (for example using '45x55%').
<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 "<code><a href="https://imagemagick.org/script/command-line-options.php?#random-threshold">-random-threshold</a></code>" argument '<code>PxQ</code>', where <code>P</code> is the min threshold and <code>Q</code> is the max (the '%' symbol is required). So "5x95%" says anything below is 5% of <code>MaxRGB</code> is set to 0, anything above 95% is set to <code>MaxRGB</code> otherwise we choose a random value between 5% and 95% of MaxRGB, as the threshold level to use for that pixel. an argument of "5x95%" value is probably the best value to use in most situations.</i></font></td>
</tr>
</table>You can improve the final look by using a darker mid-tone color (like a dark grey) instead of black for the shadow color. By doing this the color will tend to blur into the background more making the dither less pronounced that what is shown above. If you do know approximately what the background color is, you can even use a darker color of that shade to make the shadow bend in better without restricting yourself to the specific background shade. Sort of mix the two methods a little to improve the overall result. Basically The more work you put into what you want to do, the better the result will be.
<pre>
FUTURE: dither example with a dither color matching the light blue background
of this web page.
</pre><a name="gif_non-im" id="gif_non-im">
<h3>Non-ImageMagick GIF Processing</h3></a>
<table border="0" cellspacing="0" cellpadding="5">
<tr valign="top">
<td>
<a href="http://www.ict.griffith.edu.au/anthony/software/#giftrans">giftrans</a>
</td>
<td>Lists all the attributes and color table of GIF image. It can also set a specific color index as the transparent color without modifying the images color table ordering, or merging color indexes holding the same color (not a recommended situation). The IM "<code>magick identify</code>" command I have found to do a better job of listing image attributes, including the 'loop repeat limit' in the "Mosaic Application Extension" used in image animations. See also the "<code>gif2anim</code>" script (below), which previously used this program to extract the GIF image meta-data needed to re-create the GIF from the individual 'frames' extracted. It now only uses "<code>magick identify</code>", to extract this meta-data.</td>
</tr>
<tr valign="top">
<td>
<a href="http://www.lcdf.org/gifsicle/">GIFsicle</a>
</td>
<td>
This is a general-purpose image optimizer program, whose original purpose was to re-add compression to GIF images at a time when that algorithm was still under copyright. The program can also be used to add comments, create GIF animations and also optimise such animations in the same way that the IM "<code><a href="https://imagemagick.org/script/command-line-options.php?#deconstruct">-deconstruct</a></code>" operator does, though with further transparency optimizations such as <a href="../anim_opt/#opt_lzw">LZW Compression Optimization</a>.
</td>
</tr>
<tr valign="top">
<td>
<a href="http://utter.chaos.org.uk/~pdh/software/intergif.htm">InterGIF</a>
</td>
<td>
A similar program to GIFsicle, designed for processing animated GIFs. However it only provides <a href="../anim_opt/#opt_trans">Transparency Compression Optimization</a>. Other features however may be useful however. <i>Mail me your views.</i>
</td>
</tr>
<tr valign="top">
<td>
<a href="../scripts/gif2anim">gif2anim</a>
</td>
<td>A shell script which takes a GIF animation file, and extracts all the individual frame images, as well as a "<code>.anim</code>" file containing all the IM "<code>magick</code> settings needed to rebuild the animation from the extracted frame images.</td>
</tr>
<tr valign="top">
<td>
<a href="../scripts/anim2gif">anim2gif</a>
</td>
<td>
The reverse of the above script, which takes a "<code>.anim</code>" file containing all the IM "<code>magick</code> settings and rebuilding a GIF animation image. This script is very useful for studying, editing, adjusting and merging GIF animation files. For basic usage see <a href="../anim_basics/#list_info">Animation List Information</a>. Also see <a href="../anim_mods/#append">Appending Animations (time synced)</a> for a practical example of its use.
</td>
</tr>
</table><a name="gif_offsets" id="gif_offsets"></a>
<h3>GIF Image Offset handling</h3>While the GIF format saves images with offsets as part of its image animation handling, it will not save a negative offset. Any attempt to save a negative offset to a GIF image will result in the offset being reset to zero. This can be a real pain when designing GIF image animations. If Internet Explorer web browser is given an GIF image whose 'page offset' places the image somewhere outside the 'page canvas size', it will ignore the page size and offset and display it as if it has no such offset. The ancient Mozilla web browser on the other hand will just display the image canvas, and apply the offsets to the image. This can result in an empty canvas being display with no image data present, which while correct, can be unexpected. Both will display the image using the page canvas size, with the appropriate page offset if the image is wholly contained on that page canvas. <a name="gif87" id="gif87"></a>
<h3>Related GIF Output formats</h3>
<dl>
<dt>GIF87: Output the image in the older GIF 87a format.<br>
<br></dt>
<dd>If the "Mozilla" web browser sees this older format it will completely ignore the page geometry of the image, and will not use a larger 'page' frame, or use image offsets with the image. IM version 6.0.4 and earlier would normally produce a GIF89a format. But if the image was a GIF animation, and was split up into separate images using +adjoin, Im would use the GIF87a, resulting in inconsistent results when displayed in web browsers. IM after v6.0.4 will always produce a GIF 89a image format file, unless the user specifically asks for the older "<code>GIF87:</code>" output format.</dd>
</dl>
<hr>
<!-- ---------------------------------------------------------------- -->
<a name="jpg" id="jpg"></a>
<h2>JPEG Image File Format</h2>This format is about as common as the <a href="#gif">GIF format</a> above. But where GIF is designed with small simple "cartoon-like" images in mind, JPEG is designed for large real life images with lots of different colors, and shades of colors, such as photographs. A key feature of the JPEG file format is its compression, which reduces image size while keeping the image acceptable to the human eye. This is a very complex process and beyond the scope of this discussion. For more information about this process and its effects see <a href="http://www.photo.net/learn/jpeg/">Jpeg Compression Introduction</a>. And a great nitty-gritty explaination in the You Tube Video <a href="https://www.youtube.com/watch?v=Q2aEzeMDHMA">JPEG DCT, Discrete Cosine Transform (JPEG Pt2)- Computerphile</a> Unfortunately, to compress images well, the algorithm intentionally <i>loses</i> information. What is saved is <b>NOT</b> the same image as what is in memory; the color of a particular pixel or area of an image will generally will NOT be exactly the same color that was saved. This is particularly true near the edges of objects within the image. So as a quick word of warning...
<div align="center">
<b>IM is a general raster image processor, for modifying images.<br>
It will not do lossless JPEG modifications.</b>
</div>If you are interesting in lossless handling, see <a href="#jpg_non-im">Non-IM JPEG Handling</a>.<br>
This lossy behaviour becomes even more noticable if a JPEG image is changed so that the amount of change to the top or left boundary is not a multiple of 8. When this happens the JPEG compression 'blocks' or 'cells' will be completely different, and that can produce a large increase in the final image save size. That is, operations such as chop, trim, shave, border, frame, extent, etc.. (See <a href="../crop/">Cutting and Bordering Operations</a> that can shift the image data by a pixel offset that is not a 8. See the IM Forum discussion <a href="https://magick.imagemagick.org/viewtopic.php?f=1&t=20415&p=81480">Cropping an image result in an unexpected increased file</a> for more details.<br>
Normally this <i>lossy</i> nature of JPEG data is not very noticeable. However it can become noticeable when you either load and save a JPEG image multiple times or use a very low quality with a diagram showing sharp color changes. However as long as you don't load or re-use JPEG images over and over (preserve and apply operations from the original source), it is still a good file format even image types it is not particularly good at handling. As an example of this lossy JPEG nature, here I generate a simple image of two gradients appended together. While the gradients provide a smooth color change that JPEG handles very well, the sharp color change between the two gradients are not handled well.
<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 5x10 gradient: gradient:blue-navy +append jpg_lossy.gif
magick jpg_lossy.gif jpg_lossy.jpg
</samp></pre>
</td>
</tr>
</table><a href="jpg_lossy.gif"><img src="jpg_lossy_mag.gif" width="100" height="100" align="middle" vspace="5" hspace="5" border="1" alt="[IM Output]"></a> <img src="../img_www/right.gif" align="middle" width="20" height="20" alt="==>"> <a href="jpg_lossy.jpg"><img src="jpg_lossy_tn.gif" width="100" height="100" align="middle" vspace="5" hspace="5" border="1" alt="[IM Output]"></a>
</div>The first image is a magnified view of the undistorted GIF format version of the image (click the image to see or downlaod the un-magnified view). It only contains 20 colors, so in this case the GIF format can handle the image perfectly and actually generate a very small file size (see table below). On the other hand the JPEG version of the image shows clear color distortions that the JPEG compression added to the saved image, to allow it to compress it better. The distortions are greatest in the blue color channel, which is not surprising as blue is not resolved well by the human eye. That is, the human eye tends to 'spread out' blue colors naturally, so the JPEG algorithm takes advantage of this (by internally using a YCbCr colorspace). In fact without the magnification used above, you would be hard pressed to see the effect. Lets have a look at the effect of quality on the 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 jpg_lossy.gif -quality 100% jpg_lossy_100.jpg
magick jpg_lossy.gif -quality 80% jpg_lossy_80.jpg
magick jpg_lossy.gif -quality 50% jpg_lossy_50.jpg
magick jpg_lossy.gif -quality 20% jpg_lossy_20.jpg
magick jpg_lossy.gif -quality 5% jpg_lossy_5.jpg
</samp></pre>
</td>
</tr>
</table><a href="jpg_lossy.gif"><img src="jpg_lossy_mag.gif" width="100" height="100" align="middle" vspace="5" hspace="5" border="1" alt="[IM Output]"></a> <img src="../img_www/right.gif" align="middle" width="20" height="20" alt="==>"> <a href="jpg_lossy_100.jpg"><img src="jpg_lossy_100_tn.gif" width="100" height="100" align="middle" vspace="5" hspace="5" border="1" alt="[IM Output]"></a> <a href="jpg_lossy_80.jpg"><img src="jpg_lossy_80_tn.gif" width="100" height="100" align="middle" vspace="5" hspace="5" border="1" alt="[IM Output]"></a> <a href="jpg_lossy_50.jpg"><img src="jpg_lossy_50_tn.gif" width="100" height="100" align="middle" vspace="5" hspace="5" border="1" alt="[IM Output]"></a> <a href="jpg_lossy_20.jpg"><img src="jpg_lossy_20_tn.gif" width="100" height="100" align="middle" vspace="5" hspace="5" border="1" alt="[IM Output]"></a> <a href="jpg_lossy_5.jpg"><img src="jpg_lossy_5_tn.gif" width="100" height="100" align="middle" vspace="5" hspace="5" border="1" alt="[IM Output]"></a>
</div>If you look closely at first image result in the above, which we saved the test image at '<code>100%</code>' or maximum quality, there is still some slight color distortion. It is very hard to see, but it is present. On the other hand using a progressively lower "<code><a href="https://imagemagick.org/script/command-line-options.php?#quality">-quality</a></code>" setting for the JPEG image makes this color distortion even larger and more noticable. Not only that it sets up a sort of 'shadowing' of the edges producing 'waves' of color changes spreading out from the sharp edges. An effect commonly known as <a href="../filter/#ringing">Ringing Artefacts</a>. However the reason for using compression is that the size of the resulting image is very dramatically smaller, at least initially. Here is a file list of the results and their size in bytes. <!-- <CODE EXECUTE NOIMAGE OUT=jpg_ls_lossy.txt>
ls -lS --time-style=+ jpg_lossy*.jpg | awk '{ print $5, $6 }'
magick jpg_lossy.gif -scale 100x100 'jpg_lossy_mag.gif'
magick jpg_lossy*.jpg -scale 100x100 \
-set filename:fname '%t_tn' +adjoin '%[filename:fname].gif'
</CODE> -->
<div align="center">
<table border="0" cellspacing="0" cellpadding="5" width="90%" bgcolor="#F8F8F8">
<tr>
<td>
<a href="jpg_ls_lossy.txt"><img src="jpg_ls_lossy.txt.gif" align="middle" vspace="0" hspace="0" border="0" alt="[IM Text]"></a>
</td>
</tr>
</table>
</div>Note that the GIF image in this case is very small, as large 'blocks' of color compresses extremely well in GIF. As the JPEG quality gets lower, the size of the image also gets smaller. The default quality setting, when no JPEG quality is set, either by the user, or from the source image format file, is about 92%, which is a very high quality. However using a lower quality setting than '<code>50%</code>', image sizes do not get much smaller in terms of file size savings, only a much more progressively degraded image. It is a process of diminishing returns. In summary...
<div align="center">
<b>JPEG losses information, degrading images when saved.<br>
Use some other format for intermediate images during processing.<br>
Only use JPEG format, for the final image, not for further processing.</b>
</div>JPEG is also not good for artificial images with sharp color changes, such as line drawings, diagrams, or cartoon-like icons, text, and symbols. Such images with a low number of colors are better saved using a palette image format, such as GIF, or PNG8. A new JPEG image format, Jpeg2000, is becoming available which does allow lossless JPEG compression. However this requires the 'JasPer' library to also be installed. To use this special format, you also need to use a "<code>-compress jpeg2000</code>" option or save to a JP2 file format, so IM will call the right library. <a name="jpg_trans" id="jpg_trans"></a>
<h3>JPEG transparency - NOT</h3>Other than compression, the other major problem that JPEG users faces is that
<div align="center">
<b>JPEG does not save transparency</b>
</div>Thus while you can overlay images onto a background color or pattern and save to JPEG, you cannot give a JPEG image a free-form border or with see-through holes. As JPEG was designed to save real world images, and not parts of images, as such transparency was not an issue it was concerned about, when the format was created. Consequently the designers never worried about including an alpha channel, or other transparency information in the file format. For example let take the PNG with transparency we used above and magick it directly 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 a.png a.jpg
</samp></pre>
</td>
</tr>
</table><a href="a.png"><img src="a.png" align="middle" vspace="5" hspace="15" border="1" 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>As you can see all transparent parts just became black. But depending on the image source (especially GIF images) the transparent areas could have just as easily become some other random, or other inappropriate color. If this could be a problem the best idea is to have IM <a href="../masking/#remove">Remove Alpha Transparency</a>, before saving the image to the JPEG image file format. <a name="jpg_color" id="jpg_color"></a>
<h3>JPEG Color Distortion (testing)</h3>As mentioned above, the compression algorithm JPEG used is lossy. That image will be modified to allow it to compress better, reducing file space, hopefully. Exactly how much color distortion occurs depends on the quality settings use. For example let us look at how many colors are in the IM built-in "<code>netscape:</code>" image...
<table border="0" cellspacing="0" cellpadding="0" width="100%" align="center">
<tr valign="top">
<td width="10%"></td>
<td width="70%" valign="middle">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre><code execute="" noimage="" out="jpg_colors_none.txt">
magick identify -format "Colors: %k" netscape:
</code></pre>
</td>
</tr>
</table>
</td>
<td width="20%" height="100%">
<table border="0" width="100%" height="100%" bgcolor="#F8F8F8">
<tr>
<td>
<a href="jpg_colors_none.txt"><img src="jpg_colors_none.txt.gif" align="middle" vspace="0" hspace="0" border="0" alt="[IM Text]"></a>
</td>
</tr>
</table>
</td>
</tr>
</table>As you can see this image by default has 216 colors in a large rectangular array. This type of image is NOT a very good image for saving to JPEG format, which makes it ideal for our purposes. So lets look at the number of colors a JPEG image save of this image produces...
<table border="0" cellspacing="0" cellpadding="0" width="100%" align="center">
<tr valign="top">
<td width="10%"></td>
<td width="70%" valign="middle">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre><code execute="" noimage="" out="jpg_colors_def.txt">
magick netscape: JPG:- |\
magick identify -format "Colors: %k\nFile Size: %b" -
</code></pre>
</td>
</tr>
</table>
</td>
<td width="20%" height="100%">
<table border="0" width="100%" height="100%" bgcolor="#F8F8F8">
<tr>
<td>
<a href="jpg_colors_def.txt"><img src="jpg_colors_def.txt.gif" align="middle" vspace="0" hspace="0" border="0" alt="[IM Text]"></a>
</td>
</tr>
</table>
</td>
</tr>
</table>That is, by default, the saved JPEG file has almost 9 times as many colors! Though the result would still look like the original image, the edges of the rectangular area will have had colors added to nearby. Saving at the highest quality setting will <i>not</i> save the image without any color distortion...
<table border="0" cellspacing="0" cellpadding="0" width="100%" align="center">
<tr valign="top">
<td width="10%"></td>
<td width="70%" valign="middle">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre><code execute="" noimage="" out="jpg_colors_100.txt">
magick netscape: -quality 100 JPG:- |\
magick identify -format "Colors: %k\nFile Size: %b" -
</code></pre>
</td>
</tr>
</table>
</td>
<td width="20%" height="100%">
<table border="0" width="100%" height="100%" bgcolor="#F8F8F8">
<tr>
<td>
<a href="jpg_colors_100.txt"><img src="jpg_colors_100.txt.gif" align="middle" vspace="0" hspace="0" border="0" alt="[IM Text]"></a>
</td>
</tr>
</table>
</td>
</tr>
</table>As you can see a very high quality setting will only add a few extra colors but the image will still have a slight (minimal) color distorted. You can also see that the filesize is larger, as very little compression can be achieved. Now let us try "Lossless"...
<table border="0" cellspacing="0" cellpadding="0" width="100%" align="center">
<tr valign="top">
<td width="10%"></td>
<td width="70%" valign="middle">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre><code execute="" noimage="" out="jpg_colors_lless.txt">
magick netscape: -quality 100 -compress Lossless JPG:- |\
magick identify -format "Colors: %k\nFile Size: %b" -
</code></pre>
</td>
</tr>
</table>
</td>
<td width="20%" height="100%">
<table border="0" width="100%" height="100%" bgcolor="#F8F8F8">
<tr>
<td>
<a href="jpg_colors_lless.txt"><img src="jpg_colors_lless.txt.gif" align="middle" vspace="0" hspace="0" border="0" alt="[IM Text]"></a>
</td>
</tr>
</table>
</td>
</tr>
</table>Still a color distortion! Obviously my JPEG library is NOT patched for lossless encoding. However remember only another patched library can read such a lossless JPG image. Alternatively I recommend compiling your IM to use the JasPer library and the newer JP2 image file format.
<table border="0" cellspacing="0" cellpadding="0" width="100%" align="center">
<tr valign="top">
<td width="10%"></td>
<td width="70%" valign="middle">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre><code execute="" noimage="" out="jp2_colors_lless.txt">
magick netscape: JP2:- |\
magick identify -format "Colors: %k\nFile Size: %b" -
</code></pre>
</td>
</tr>
</table>
</td>
<td width="20%" height="100%">
<table border="0" width="100%" height="100%" bgcolor="#F8F8F8">
<tr>
<td>
<a href="jp2_colors_lless.txt"><img src="jp2_colors_lless.txt.gif" align="middle" vspace="0" hspace="0" border="0" alt="[IM Text]"></a>
</td>
</tr>
</table>
</td>
</tr>
</table>As you can see the Jpeg2000 format switched to other non-lossy methods of image compression, but does not color distort the image, by default. It also performs some very high compression methods on the image. However using a lower quality with the new JP2 format will again introduce the color distortions, so as to generate a smaller image, just like the normal JPEG image file format...
<table border="0" cellspacing="0" cellpadding="0" width="100%" align="center">
<tr valign="top">
<td width="10%"></td>
<td width="70%" valign="middle">
<table class="table table-sm table-hover table-striped" cellspacing="0" cellpadding="5" width="100%" bgcolor="#F8F8F8">
<tr>
<td>
<pre><code execute="" noimage="" out="jp2_colors_50.txt">
magick netscape: -quality 50% JP2:- |\
magick identify -format "Colors: %k\nFile Size: %b" -
</code></pre>
</td>
</tr>
</table>
</td>
<td width="20%" height="100%">
<table border="0" width="100%" height="100%" bgcolor="#F8F8F8">
<tr>
<td>
<a href="jp2_colors_50.txt"><img src="jp2_colors_50.txt.gif" align="middle" vspace="0" hspace="0" border="0" alt="[IM Text]"></a>
</td>
</tr>
</table>
</td>
</tr>
</table>For more information of using the JPEG2000 coder, see <a href="https://imagemagick.org/script/jp2.php">JPEG2000 encoding parameter documentation</a>. <a name="jpg_read" id="jpg_read"></a>
<h3>Reading JPEG Control Options</h3>
<blockquote>
<dl>