Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/PIL/JpegPresets.py: 100%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2JPEG quality settings equivalent to the Photoshop settings.
3Can be used when saving JPEG files.
5The following presets are available by default:
6``web_low``, ``web_medium``, ``web_high``, ``web_very_high``, ``web_maximum``,
7``low``, ``medium``, ``high``, ``maximum``.
8More presets can be added to the :py:data:`presets` dict if needed.
10To apply the preset, specify::
12 quality="preset_name"
14To apply only the quantization table::
16 qtables="preset_name"
18To apply only the subsampling setting::
20 subsampling="preset_name"
22Example::
24 im.save("image_name.jpg", quality="web_high")
26Subsampling
27-----------
29Subsampling is the practice of encoding images by implementing less resolution
30for chroma information than for luma information.
31(ref.: https://en.wikipedia.org/wiki/Chroma_subsampling)
33Possible subsampling values are 0, 1 and 2 that correspond to 4:4:4, 4:2:2 and
344:2:0.
36You can get the subsampling of a JPEG with the
37:func:`.JpegImagePlugin.get_sampling` function.
39In JPEG compressed data a JPEG marker is used instead of an EXIF tag.
40(ref.: https://exiv2.org/tags.html)
43Quantization tables
44-------------------
46They are values use by the DCT (Discrete cosine transform) to remove
47*unnecessary* information from the image (the lossy part of the compression).
48(ref.: https://en.wikipedia.org/wiki/Quantization_matrix#Quantization_matrices,
49https://en.wikipedia.org/wiki/JPEG#Quantization)
51You can get the quantization tables of a JPEG with::
53 im.quantization
55This will return a dict with a number of lists. You can pass this dict
56directly as the qtables argument when saving a JPEG.
58The quantization table format in presets is a list with sublists. These formats
59are interchangeable.
61Libjpeg ref.:
62https://web.archive.org/web/20120328125543/http://www.jpegcameras.com/libjpeg/libjpeg-3.html
64"""
66from __future__ import annotations
68presets = {
69 'web_low': {'subsampling': 2, # "4:2:0"
70 'quantization': [
71 [20, 16, 25, 39, 50, 46, 62, 68,
72 16, 18, 23, 38, 38, 53, 65, 68,
73 25, 23, 31, 38, 53, 65, 68, 68,
74 39, 38, 38, 53, 65, 68, 68, 68,
75 50, 38, 53, 65, 68, 68, 68, 68,
76 46, 53, 65, 68, 68, 68, 68, 68,
77 62, 65, 68, 68, 68, 68, 68, 68,
78 68, 68, 68, 68, 68, 68, 68, 68],
79 [21, 25, 32, 38, 54, 68, 68, 68,
80 25, 28, 24, 38, 54, 68, 68, 68,
81 32, 24, 32, 43, 66, 68, 68, 68,
82 38, 38, 43, 53, 68, 68, 68, 68,
83 54, 54, 66, 68, 68, 68, 68, 68,
84 68, 68, 68, 68, 68, 68, 68, 68,
85 68, 68, 68, 68, 68, 68, 68, 68,
86 68, 68, 68, 68, 68, 68, 68, 68]
87 ]},
88 'web_medium': {'subsampling': 2, # "4:2:0"
89 'quantization': [
90 [16, 11, 11, 16, 23, 27, 31, 30,
91 11, 12, 12, 15, 20, 23, 23, 30,
92 11, 12, 13, 16, 23, 26, 35, 47,
93 16, 15, 16, 23, 26, 37, 47, 64,
94 23, 20, 23, 26, 39, 51, 64, 64,
95 27, 23, 26, 37, 51, 64, 64, 64,
96 31, 23, 35, 47, 64, 64, 64, 64,
97 30, 30, 47, 64, 64, 64, 64, 64],
98 [17, 15, 17, 21, 20, 26, 38, 48,
99 15, 19, 18, 17, 20, 26, 35, 43,
100 17, 18, 20, 22, 26, 30, 46, 53,
101 21, 17, 22, 28, 30, 39, 53, 64,
102 20, 20, 26, 30, 39, 48, 64, 64,
103 26, 26, 30, 39, 48, 63, 64, 64,
104 38, 35, 46, 53, 64, 64, 64, 64,
105 48, 43, 53, 64, 64, 64, 64, 64]
106 ]},
107 'web_high': {'subsampling': 0, # "4:4:4"
108 'quantization': [
109 [6, 4, 4, 6, 9, 11, 12, 16,
110 4, 5, 5, 6, 8, 10, 12, 12,
111 4, 5, 5, 6, 10, 12, 14, 19,
112 6, 6, 6, 11, 12, 15, 19, 28,
113 9, 8, 10, 12, 16, 20, 27, 31,
114 11, 10, 12, 15, 20, 27, 31, 31,
115 12, 12, 14, 19, 27, 31, 31, 31,
116 16, 12, 19, 28, 31, 31, 31, 31],
117 [7, 7, 13, 24, 26, 31, 31, 31,
118 7, 12, 16, 21, 31, 31, 31, 31,
119 13, 16, 17, 31, 31, 31, 31, 31,
120 24, 21, 31, 31, 31, 31, 31, 31,
121 26, 31, 31, 31, 31, 31, 31, 31,
122 31, 31, 31, 31, 31, 31, 31, 31,
123 31, 31, 31, 31, 31, 31, 31, 31,
124 31, 31, 31, 31, 31, 31, 31, 31]
125 ]},
126 'web_very_high': {'subsampling': 0, # "4:4:4"
127 'quantization': [
128 [2, 2, 2, 2, 3, 4, 5, 6,
129 2, 2, 2, 2, 3, 4, 5, 6,
130 2, 2, 2, 2, 4, 5, 7, 9,
131 2, 2, 2, 4, 5, 7, 9, 12,
132 3, 3, 4, 5, 8, 10, 12, 12,
133 4, 4, 5, 7, 10, 12, 12, 12,
134 5, 5, 7, 9, 12, 12, 12, 12,
135 6, 6, 9, 12, 12, 12, 12, 12],
136 [3, 3, 5, 9, 13, 15, 15, 15,
137 3, 4, 6, 11, 14, 12, 12, 12,
138 5, 6, 9, 14, 12, 12, 12, 12,
139 9, 11, 14, 12, 12, 12, 12, 12,
140 13, 14, 12, 12, 12, 12, 12, 12,
141 15, 12, 12, 12, 12, 12, 12, 12,
142 15, 12, 12, 12, 12, 12, 12, 12,
143 15, 12, 12, 12, 12, 12, 12, 12]
144 ]},
145 'web_maximum': {'subsampling': 0, # "4:4:4"
146 'quantization': [
147 [1, 1, 1, 1, 1, 1, 1, 1,
148 1, 1, 1, 1, 1, 1, 1, 1,
149 1, 1, 1, 1, 1, 1, 1, 2,
150 1, 1, 1, 1, 1, 1, 2, 2,
151 1, 1, 1, 1, 1, 2, 2, 3,
152 1, 1, 1, 1, 2, 2, 3, 3,
153 1, 1, 1, 2, 2, 3, 3, 3,
154 1, 1, 2, 2, 3, 3, 3, 3],
155 [1, 1, 1, 2, 2, 3, 3, 3,
156 1, 1, 1, 2, 3, 3, 3, 3,
157 1, 1, 1, 3, 3, 3, 3, 3,
158 2, 2, 3, 3, 3, 3, 3, 3,
159 2, 3, 3, 3, 3, 3, 3, 3,
160 3, 3, 3, 3, 3, 3, 3, 3,
161 3, 3, 3, 3, 3, 3, 3, 3,
162 3, 3, 3, 3, 3, 3, 3, 3]
163 ]},
164 'low': {'subsampling': 2, # "4:2:0"
165 'quantization': [
166 [18, 14, 14, 21, 30, 35, 34, 17,
167 14, 16, 16, 19, 26, 23, 12, 12,
168 14, 16, 17, 21, 23, 12, 12, 12,
169 21, 19, 21, 23, 12, 12, 12, 12,
170 30, 26, 23, 12, 12, 12, 12, 12,
171 35, 23, 12, 12, 12, 12, 12, 12,
172 34, 12, 12, 12, 12, 12, 12, 12,
173 17, 12, 12, 12, 12, 12, 12, 12],
174 [20, 19, 22, 27, 20, 20, 17, 17,
175 19, 25, 23, 14, 14, 12, 12, 12,
176 22, 23, 14, 14, 12, 12, 12, 12,
177 27, 14, 14, 12, 12, 12, 12, 12,
178 20, 14, 12, 12, 12, 12, 12, 12,
179 20, 12, 12, 12, 12, 12, 12, 12,
180 17, 12, 12, 12, 12, 12, 12, 12,
181 17, 12, 12, 12, 12, 12, 12, 12]
182 ]},
183 'medium': {'subsampling': 2, # "4:2:0"
184 'quantization': [
185 [12, 8, 8, 12, 17, 21, 24, 17,
186 8, 9, 9, 11, 15, 19, 12, 12,
187 8, 9, 10, 12, 19, 12, 12, 12,
188 12, 11, 12, 21, 12, 12, 12, 12,
189 17, 15, 19, 12, 12, 12, 12, 12,
190 21, 19, 12, 12, 12, 12, 12, 12,
191 24, 12, 12, 12, 12, 12, 12, 12,
192 17, 12, 12, 12, 12, 12, 12, 12],
193 [13, 11, 13, 16, 20, 20, 17, 17,
194 11, 14, 14, 14, 14, 12, 12, 12,
195 13, 14, 14, 14, 12, 12, 12, 12,
196 16, 14, 14, 12, 12, 12, 12, 12,
197 20, 14, 12, 12, 12, 12, 12, 12,
198 20, 12, 12, 12, 12, 12, 12, 12,
199 17, 12, 12, 12, 12, 12, 12, 12,
200 17, 12, 12, 12, 12, 12, 12, 12]
201 ]},
202 'high': {'subsampling': 0, # "4:4:4"
203 'quantization': [
204 [6, 4, 4, 6, 9, 11, 12, 16,
205 4, 5, 5, 6, 8, 10, 12, 12,
206 4, 5, 5, 6, 10, 12, 12, 12,
207 6, 6, 6, 11, 12, 12, 12, 12,
208 9, 8, 10, 12, 12, 12, 12, 12,
209 11, 10, 12, 12, 12, 12, 12, 12,
210 12, 12, 12, 12, 12, 12, 12, 12,
211 16, 12, 12, 12, 12, 12, 12, 12],
212 [7, 7, 13, 24, 20, 20, 17, 17,
213 7, 12, 16, 14, 14, 12, 12, 12,
214 13, 16, 14, 14, 12, 12, 12, 12,
215 24, 14, 14, 12, 12, 12, 12, 12,
216 20, 14, 12, 12, 12, 12, 12, 12,
217 20, 12, 12, 12, 12, 12, 12, 12,
218 17, 12, 12, 12, 12, 12, 12, 12,
219 17, 12, 12, 12, 12, 12, 12, 12]
220 ]},
221 'maximum': {'subsampling': 0, # "4:4:4"
222 'quantization': [
223 [2, 2, 2, 2, 3, 4, 5, 6,
224 2, 2, 2, 2, 3, 4, 5, 6,
225 2, 2, 2, 2, 4, 5, 7, 9,
226 2, 2, 2, 4, 5, 7, 9, 12,
227 3, 3, 4, 5, 8, 10, 12, 12,
228 4, 4, 5, 7, 10, 12, 12, 12,
229 5, 5, 7, 9, 12, 12, 12, 12,
230 6, 6, 9, 12, 12, 12, 12, 12],
231 [3, 3, 5, 9, 13, 15, 15, 15,
232 3, 4, 6, 10, 14, 12, 12, 12,
233 5, 6, 9, 14, 12, 12, 12, 12,
234 9, 10, 14, 12, 12, 12, 12, 12,
235 13, 14, 12, 12, 12, 12, 12, 12,
236 15, 12, 12, 12, 12, 12, 12, 12,
237 15, 12, 12, 12, 12, 12, 12, 12,
238 15, 12, 12, 12, 12, 12, 12, 12]
239 ]},
240} # fmt: skip