~ubuntu-branches/ubuntu/saucy/python-imaging/saucy-proposed

« back to all changes in this revision

Viewing changes to PIL/JpegPresets.py

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-03-20 16:44:01 UTC
  • mfrom: (2.1.13 experimental)
  • Revision ID: package-import@ubuntu.com-20130320164401-ptf6m0ttg4zw72az
Tags: 1.1.7+2.0.0-1
Pillow 2.0.0 release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
JPEG quality settings equivalent to the Photoshop settings.
 
3
 
 
4
More presets can be added to the presets dict if needed.
 
5
 
 
6
Can be use when saving JPEG file.
 
7
 
 
8
To apply the preset, specify:
 
9
 
 
10
 - quality=preset name
 
11
 
 
12
To apply only the quantization table:
 
13
 
 
14
- qtables=preset name
 
15
 
 
16
To apply only the subsampling setting:
 
17
 
 
18
- subsampling=preset name
 
19
 
 
20
Example:
 
21
 
 
22
    im.save("image_name.jpg", quality="web_high")
 
23
 
 
24
 
 
25
Subsampling
 
26
-----------
 
27
 
 
28
Subsampling is the practice of encoding images by implementing less resolution
 
29
for chroma information than for luma information.
 
30
(ref.: http://en.wikipedia.org/wiki/Chroma_subsampling)
 
31
    
 
32
Possible subsampling values are 0, 1 and 2 that correspond to 4:4:4, 4:2:2 and
 
33
4:1:1 (or 4:2:0?).
 
34
 
 
35
You can get the subsampling of a JPEG with the
 
36
`JpegImagePlugin.get_subsampling(im)` function.
 
37
 
 
38
 
 
39
Quantization tables
 
40
-------------------
 
41
 
 
42
They are values use by the DCT (Discrete cosine transform) to remove
 
43
*unnecessary* information from the image (the lossy part of the compression).
 
44
(ref.: http://en.wikipedia.org/wiki/Quantization_matrix#Quantization_matrices,
 
45
       http://en.wikipedia.org/wiki/JPEG#Quantization)
 
46
 
 
47
You can get the quantization tables of a JPEG with:
 
48
 
 
49
    im.quantization
 
50
    
 
51
This will return a dict with a number of arrays. You can pass this dict directly
 
52
as the qtables argument when saving a JPEG.
 
53
 
 
54
The tables format between im.quantization and quantization in presets differ in
 
55
3 ways:
 
56
 
 
57
 1. The base container of the preset is a list with sublists instead of dict.
 
58
    dict[0] -> list[0], dict[1] -> list[1], ...
 
59
    
 
60
 2. Each table in a preset is a list instead of an array.
 
61
 
 
62
 3. The zigzag order is remove in the preset (needed by libjpeg >= 6a).
 
63
 
 
64
You can convert the dict format to the preset format with the
 
65
`JpegImagePlugin.convert_dict_qtables(dict_qtables)` function.
 
66
 
 
67
Libjpeg ref.: http://www.jpegcameras.com/libjpeg/libjpeg-3.html
 
68
 
 
69
"""
 
70
 
 
71
presets = { 
 
72
            'web_low':      {'subsampling':  2, # "4:1:1"
 
73
                            'quantization': [
 
74
                               [20, 16, 25, 39, 50, 46, 62, 68,
 
75
                                16, 18, 23, 38, 38, 53, 65, 68,
 
76
                                25, 23, 31, 38, 53, 65, 68, 68,
 
77
                                39, 38, 38, 53, 65, 68, 68, 68,
 
78
                                50, 38, 53, 65, 68, 68, 68, 68,
 
79
                                46, 53, 65, 68, 68, 68, 68, 68,
 
80
                                62, 65, 68, 68, 68, 68, 68, 68,
 
81
                                68, 68, 68, 68, 68, 68, 68, 68],
 
82
                               [21, 25, 32, 38, 54, 68, 68, 68,
 
83
                                25, 28, 24, 38, 54, 68, 68, 68,
 
84
                                32, 24, 32, 43, 66, 68, 68, 68,
 
85
                                38, 38, 43, 53, 68, 68, 68, 68,
 
86
                                54, 54, 66, 68, 68, 68, 68, 68,
 
87
                                68, 68, 68, 68, 68, 68, 68, 68,
 
88
                                68, 68, 68, 68, 68, 68, 68, 68,
 
89
                                68, 68, 68, 68, 68, 68, 68, 68]
 
90
                            ]},
 
91
                            
 
92
            'web_medium':   {'subsampling':  2, # "4:1:1"
 
93
                            'quantization': [
 
94
                               [16, 11, 11, 16, 23, 27, 31, 30,
 
95
                                11, 12, 12, 15, 20, 23, 23, 30,
 
96
                                11, 12, 13, 16, 23, 26, 35, 47,
 
97
                                16, 15, 16, 23, 26, 37, 47, 64,
 
98
                                23, 20, 23, 26, 39, 51, 64, 64,
 
99
                                27, 23, 26, 37, 51, 64, 64, 64,
 
100
                                31, 23, 35, 47, 64, 64, 64, 64,
 
101
                                30, 30, 47, 64, 64, 64, 64, 64],
 
102
                               [17, 15, 17, 21, 20, 26, 38, 48,
 
103
                                15, 19, 18, 17, 20, 26, 35, 43,
 
104
                                17, 18, 20, 22, 26, 30, 46, 53,
 
105
                                21, 17, 22, 28, 30, 39, 53, 64,
 
106
                                20, 20, 26, 30, 39, 48, 64, 64,
 
107
                                26, 26, 30, 39, 48, 63, 64, 64,
 
108
                                38, 35, 46, 53, 64, 64, 64, 64,
 
109
                                48, 43, 53, 64, 64, 64, 64, 64]
 
110
                            ]},
 
111
                            
 
112
            'web_high':     {'subsampling':  0, # "4:4:4"
 
113
                            'quantization': [
 
114
                               [ 6,  4,  4,  6,  9, 11, 12, 16,
 
115
                                 4,  5,  5,  6,  8, 10, 12, 12,
 
116
                                 4,  5,  5,  6, 10, 12, 14, 19,
 
117
                                 6,  6,  6, 11, 12, 15, 19, 28,
 
118
                                 9,  8, 10, 12, 16, 20, 27, 31,
 
119
                                11, 10, 12, 15, 20, 27, 31, 31,
 
120
                                12, 12, 14, 19, 27, 31, 31, 31,
 
121
                                16, 12, 19, 28, 31, 31, 31, 31],
 
122
                               [ 7,  7, 13, 24, 26, 31, 31, 31,
 
123
                                 7, 12, 16, 21, 31, 31, 31, 31,
 
124
                                13, 16, 17, 31, 31, 31, 31, 31,
 
125
                                24, 21, 31, 31, 31, 31, 31, 31,
 
126
                                26, 31, 31, 31, 31, 31, 31, 31,
 
127
                                31, 31, 31, 31, 31, 31, 31, 31,
 
128
                                31, 31, 31, 31, 31, 31, 31, 31,
 
129
                                31, 31, 31, 31, 31, 31, 31, 31]
 
130
                            ]},
 
131
                            
 
132
            'web_very_high': {'subsampling':  0, # "4:4:4"
 
133
                            'quantization': [
 
134
                               [ 2,  2,  2,  2,  3,  4,  5,  6,
 
135
                                 2,  2,  2,  2,  3,  4,  5,  6,
 
136
                                 2,  2,  2,  2,  4,  5,  7,  9,
 
137
                                 2,  2,  2,  4,  5,  7,  9, 12,
 
138
                                 3,  3,  4,  5,  8, 10, 12, 12,
 
139
                                 4,  4,  5,  7, 10, 12, 12, 12,
 
140
                                 5,  5,  7,  9, 12, 12, 12, 12,
 
141
                                 6,  6,  9, 12, 12, 12, 12, 12],
 
142
                               [ 3,  3,  5,  9, 13, 15, 15, 15,
 
143
                                 3,  4,  6, 11, 14, 12, 12, 12,
 
144
                                 5,  6,  9, 14, 12, 12, 12, 12,
 
145
                                 9, 11, 14, 12, 12, 12, 12, 12,
 
146
                                13, 14, 12, 12, 12, 12, 12, 12,
 
147
                                15, 12, 12, 12, 12, 12, 12, 12,
 
148
                                15, 12, 12, 12, 12, 12, 12, 12,
 
149
                                15, 12, 12, 12, 12, 12, 12, 12]
 
150
                            ]},
 
151
                            
 
152
            'web_maximum':  {'subsampling':  0, # "4:4:4"
 
153
                            'quantization': [
 
154
                               [ 1,  1,  1,  1,  1,  1,  1,  1, 
 
155
                                 1,  1,  1,  1,  1,  1,  1,  1, 
 
156
                                 1,  1,  1,  1,  1,  1,  1,  2, 
 
157
                                 1,  1,  1,  1,  1,  1,  2,  2, 
 
158
                                 1,  1,  1,  1,  1,  2,  2,  3, 
 
159
                                 1,  1,  1,  1,  2,  2,  3,  3, 
 
160
                                 1,  1,  1,  2,  2,  3,  3,  3, 
 
161
                                 1,  1,  2,  2,  3,  3,  3,  3],
 
162
                               [ 1,  1,  1,  2,  2,  3,  3,  3,
 
163
                                 1,  1,  1,  2,  3,  3,  3,  3,
 
164
                                 1,  1,  1,  3,  3,  3,  3,  3,
 
165
                                 2,  2,  3,  3,  3,  3,  3,  3,
 
166
                                 2,  3,  3,  3,  3,  3,  3,  3,
 
167
                                 3,  3,  3,  3,  3,  3,  3,  3,
 
168
                                 3,  3,  3,  3,  3,  3,  3,  3, 
 
169
                                 3,  3,  3,  3,  3,  3,  3,  3]
 
170
                            ]},
 
171
                            
 
172
            'low':          {'subsampling':  2, # "4:1:1"
 
173
                            'quantization': [
 
174
                               [18, 14, 14, 21, 30, 35, 34, 17,
 
175
                                14, 16, 16, 19, 26, 23, 12, 12,
 
176
                                14, 16, 17, 21, 23, 12, 12, 12,
 
177
                                21, 19, 21, 23, 12, 12, 12, 12,
 
178
                                30, 26, 23, 12, 12, 12, 12, 12,
 
179
                                35, 23, 12, 12, 12, 12, 12, 12,
 
180
                                34, 12, 12, 12, 12, 12, 12, 12,
 
181
                                17, 12, 12, 12, 12, 12, 12, 12],
 
182
                               [20, 19, 22, 27, 20, 20, 17, 17,
 
183
                                19, 25, 23, 14, 14, 12, 12, 12,
 
184
                                22, 23, 14, 14, 12, 12, 12, 12,
 
185
                                27, 14, 14, 12, 12, 12, 12, 12,
 
186
                                20, 14, 12, 12, 12, 12, 12, 12,
 
187
                                20, 12, 12, 12, 12, 12, 12, 12,
 
188
                                17, 12, 12, 12, 12, 12, 12, 12,
 
189
                                17, 12, 12, 12, 12, 12, 12, 12]
 
190
                            ]},
 
191
            'medium':       {'subsampling':  2, # "4:1:1"
 
192
                            'quantization': [
 
193
                               [12,  8,  8, 12, 17, 21, 24, 17,
 
194
                                 8,  9,  9, 11, 15, 19, 12, 12,
 
195
                                 8,  9, 10, 12, 19, 12, 12, 12,
 
196
                                12, 11, 12, 21, 12, 12, 12, 12,
 
197
                                17, 15, 19, 12, 12, 12, 12, 12,
 
198
                                21, 19, 12, 12, 12, 12, 12, 12,
 
199
                                24, 12, 12, 12, 12, 12, 12, 12,
 
200
                                17, 12, 12, 12, 12, 12, 12, 12],
 
201
                               [13, 11, 13, 16, 20, 20, 17, 17,
 
202
                                11, 14, 14, 14, 14, 12, 12, 12,
 
203
                                13, 14, 14, 14, 12, 12, 12, 12,
 
204
                                16, 14, 14, 12, 12, 12, 12, 12,
 
205
                                20, 14, 12, 12, 12, 12, 12, 12,
 
206
                                20, 12, 12, 12, 12, 12, 12, 12,
 
207
                                17, 12, 12, 12, 12, 12, 12, 12,
 
208
                                17, 12, 12, 12, 12, 12, 12, 12]
 
209
                            ]},
 
210
                            
 
211
            'high':         {'subsampling':  0, # "4:4:4"
 
212
                            'quantization': [
 
213
                               [ 6,  4,  4,  6,  9, 11, 12, 16,
 
214
                                 4,  5,  5,  6,  8, 10, 12, 12, 
 
215
                                 4,  5,  5,  6, 10, 12, 12, 12,
 
216
                                 6,  6,  6, 11, 12, 12, 12, 12,
 
217
                                 9,  8, 10, 12, 12, 12, 12, 12,
 
218
                                11, 10, 12, 12, 12, 12, 12, 12,
 
219
                                12, 12, 12, 12, 12, 12, 12, 12,
 
220
                                16, 12, 12, 12, 12, 12, 12, 12],
 
221
                               [ 7,  7, 13, 24, 20, 20, 17, 17,
 
222
                                 7, 12, 16, 14, 14, 12, 12, 12,
 
223
                                13, 16, 14, 14, 12, 12, 12, 12,
 
224
                                24, 14, 14, 12, 12, 12, 12, 12,
 
225
                                20, 14, 12, 12, 12, 12, 12, 12,
 
226
                                20, 12, 12, 12, 12, 12, 12, 12,
 
227
                                17, 12, 12, 12, 12, 12, 12, 12,
 
228
                                17, 12, 12, 12, 12, 12, 12, 12]
 
229
                            ]},
 
230
                            
 
231
            'maximum':      {'subsampling':  0, # "4:4:4"
 
232
                            'quantization': [
 
233
                               [ 2,  2,  2,  2,  3,  4,  5,  6,
 
234
                                 2,  2,  2,  2,  3,  4,  5,  6,
 
235
                                 2,  2,  2,  2,  4,  5,  7,  9,
 
236
                                 2,  2,  2,  4,  5,  7,  9, 12,
 
237
                                 3,  3,  4,  5,  8, 10, 12, 12,
 
238
                                 4,  4,  5,  7, 10, 12, 12, 12,
 
239
                                 5,  5,  7,  9, 12, 12, 12, 12,
 
240
                                 6,  6,  9, 12, 12, 12, 12, 12],
 
241
                               [ 3,  3,  5,  9, 13, 15, 15, 15,
 
242
                                 3,  4,  6, 10, 14, 12, 12, 12,
 
243
                                 5,  6,  9, 14, 12, 12, 12, 12,
 
244
                                 9, 10, 14, 12, 12, 12, 12, 12,
 
245
                                13, 14, 12, 12, 12, 12, 12, 12,
 
246
                                15, 12, 12, 12, 12, 12, 12, 12,
 
247
                                15, 12, 12, 12, 12, 12, 12, 12,
 
248
                                15, 12, 12, 12, 12, 12, 12, 12]
 
249
                            ]},
 
250
            }
 
 
b'\\ No newline at end of file'