Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pillow-10.4.0-py3.8-linux-x86_64.egg/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

2 statements  

1""" 

2JPEG quality settings equivalent to the Photoshop settings. 

3Can be used when saving JPEG files. 

4 

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. 

9 

10To apply the preset, specify:: 

11 

12 quality="preset_name" 

13 

14To apply only the quantization table:: 

15 

16 qtables="preset_name" 

17 

18To apply only the subsampling setting:: 

19 

20 subsampling="preset_name" 

21 

22Example:: 

23 

24 im.save("image_name.jpg", quality="web_high") 

25 

26Subsampling 

27----------- 

28 

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) 

32 

33Possible subsampling values are 0, 1 and 2 that correspond to 4:4:4, 4:2:2 and 

344:2:0. 

35 

36You can get the subsampling of a JPEG with the 

37:func:`.JpegImagePlugin.get_sampling` function. 

38 

39In JPEG compressed data a JPEG marker is used instead of an EXIF tag. 

40(ref.: https://web.archive.org/web/20240227115053/https://exiv2.org/tags.html) 

41 

42 

43Quantization tables 

44------------------- 

45 

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) 

50 

51You can get the quantization tables of a JPEG with:: 

52 

53 im.quantization 

54 

55This will return a dict with a number of lists. You can pass this dict 

56directly as the qtables argument when saving a JPEG. 

57 

58The quantization table format in presets is a list with sublists. These formats 

59are interchangeable. 

60 

61Libjpeg ref.: 

62https://web.archive.org/web/20120328125543/http://www.jpegcameras.com/libjpeg/libjpeg-3.html 

63 

64""" 

65 

66from __future__ import annotations 

67 

68# fmt: off 

69presets = { 

70 'web_low': {'subsampling': 2, # "4:2:0" 

71 'quantization': [ 

72 [20, 16, 25, 39, 50, 46, 62, 68, 

73 16, 18, 23, 38, 38, 53, 65, 68, 

74 25, 23, 31, 38, 53, 65, 68, 68, 

75 39, 38, 38, 53, 65, 68, 68, 68, 

76 50, 38, 53, 65, 68, 68, 68, 68, 

77 46, 53, 65, 68, 68, 68, 68, 68, 

78 62, 65, 68, 68, 68, 68, 68, 68, 

79 68, 68, 68, 68, 68, 68, 68, 68], 

80 [21, 25, 32, 38, 54, 68, 68, 68, 

81 25, 28, 24, 38, 54, 68, 68, 68, 

82 32, 24, 32, 43, 66, 68, 68, 68, 

83 38, 38, 43, 53, 68, 68, 68, 68, 

84 54, 54, 66, 68, 68, 68, 68, 68, 

85 68, 68, 68, 68, 68, 68, 68, 68, 

86 68, 68, 68, 68, 68, 68, 68, 68, 

87 68, 68, 68, 68, 68, 68, 68, 68] 

88 ]}, 

89 'web_medium': {'subsampling': 2, # "4:2:0" 

90 'quantization': [ 

91 [16, 11, 11, 16, 23, 27, 31, 30, 

92 11, 12, 12, 15, 20, 23, 23, 30, 

93 11, 12, 13, 16, 23, 26, 35, 47, 

94 16, 15, 16, 23, 26, 37, 47, 64, 

95 23, 20, 23, 26, 39, 51, 64, 64, 

96 27, 23, 26, 37, 51, 64, 64, 64, 

97 31, 23, 35, 47, 64, 64, 64, 64, 

98 30, 30, 47, 64, 64, 64, 64, 64], 

99 [17, 15, 17, 21, 20, 26, 38, 48, 

100 15, 19, 18, 17, 20, 26, 35, 43, 

101 17, 18, 20, 22, 26, 30, 46, 53, 

102 21, 17, 22, 28, 30, 39, 53, 64, 

103 20, 20, 26, 30, 39, 48, 64, 64, 

104 26, 26, 30, 39, 48, 63, 64, 64, 

105 38, 35, 46, 53, 64, 64, 64, 64, 

106 48, 43, 53, 64, 64, 64, 64, 64] 

107 ]}, 

108 'web_high': {'subsampling': 0, # "4:4:4" 

109 'quantization': [ 

110 [6, 4, 4, 6, 9, 11, 12, 16, 

111 4, 5, 5, 6, 8, 10, 12, 12, 

112 4, 5, 5, 6, 10, 12, 14, 19, 

113 6, 6, 6, 11, 12, 15, 19, 28, 

114 9, 8, 10, 12, 16, 20, 27, 31, 

115 11, 10, 12, 15, 20, 27, 31, 31, 

116 12, 12, 14, 19, 27, 31, 31, 31, 

117 16, 12, 19, 28, 31, 31, 31, 31], 

118 [7, 7, 13, 24, 26, 31, 31, 31, 

119 7, 12, 16, 21, 31, 31, 31, 31, 

120 13, 16, 17, 31, 31, 31, 31, 31, 

121 24, 21, 31, 31, 31, 31, 31, 31, 

122 26, 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 31, 31, 31, 31, 31, 31, 31, 31] 

126 ]}, 

127 'web_very_high': {'subsampling': 0, # "4:4:4" 

128 'quantization': [ 

129 [2, 2, 2, 2, 3, 4, 5, 6, 

130 2, 2, 2, 2, 3, 4, 5, 6, 

131 2, 2, 2, 2, 4, 5, 7, 9, 

132 2, 2, 2, 4, 5, 7, 9, 12, 

133 3, 3, 4, 5, 8, 10, 12, 12, 

134 4, 4, 5, 7, 10, 12, 12, 12, 

135 5, 5, 7, 9, 12, 12, 12, 12, 

136 6, 6, 9, 12, 12, 12, 12, 12], 

137 [3, 3, 5, 9, 13, 15, 15, 15, 

138 3, 4, 6, 11, 14, 12, 12, 12, 

139 5, 6, 9, 14, 12, 12, 12, 12, 

140 9, 11, 14, 12, 12, 12, 12, 12, 

141 13, 14, 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 15, 12, 12, 12, 12, 12, 12, 12] 

145 ]}, 

146 'web_maximum': {'subsampling': 0, # "4:4:4" 

147 'quantization': [ 

148 [1, 1, 1, 1, 1, 1, 1, 1, 

149 1, 1, 1, 1, 1, 1, 1, 1, 

150 1, 1, 1, 1, 1, 1, 1, 2, 

151 1, 1, 1, 1, 1, 1, 2, 2, 

152 1, 1, 1, 1, 1, 2, 2, 3, 

153 1, 1, 1, 1, 2, 2, 3, 3, 

154 1, 1, 1, 2, 2, 3, 3, 3, 

155 1, 1, 2, 2, 3, 3, 3, 3], 

156 [1, 1, 1, 2, 2, 3, 3, 3, 

157 1, 1, 1, 2, 3, 3, 3, 3, 

158 1, 1, 1, 3, 3, 3, 3, 3, 

159 2, 2, 3, 3, 3, 3, 3, 3, 

160 2, 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 3, 3, 3, 3, 3, 3, 3, 3] 

164 ]}, 

165 'low': {'subsampling': 2, # "4:2:0" 

166 'quantization': [ 

167 [18, 14, 14, 21, 30, 35, 34, 17, 

168 14, 16, 16, 19, 26, 23, 12, 12, 

169 14, 16, 17, 21, 23, 12, 12, 12, 

170 21, 19, 21, 23, 12, 12, 12, 12, 

171 30, 26, 23, 12, 12, 12, 12, 12, 

172 35, 23, 12, 12, 12, 12, 12, 12, 

173 34, 12, 12, 12, 12, 12, 12, 12, 

174 17, 12, 12, 12, 12, 12, 12, 12], 

175 [20, 19, 22, 27, 20, 20, 17, 17, 

176 19, 25, 23, 14, 14, 12, 12, 12, 

177 22, 23, 14, 14, 12, 12, 12, 12, 

178 27, 14, 14, 12, 12, 12, 12, 12, 

179 20, 14, 12, 12, 12, 12, 12, 12, 

180 20, 12, 12, 12, 12, 12, 12, 12, 

181 17, 12, 12, 12, 12, 12, 12, 12, 

182 17, 12, 12, 12, 12, 12, 12, 12] 

183 ]}, 

184 'medium': {'subsampling': 2, # "4:2:0" 

185 'quantization': [ 

186 [12, 8, 8, 12, 17, 21, 24, 17, 

187 8, 9, 9, 11, 15, 19, 12, 12, 

188 8, 9, 10, 12, 19, 12, 12, 12, 

189 12, 11, 12, 21, 12, 12, 12, 12, 

190 17, 15, 19, 12, 12, 12, 12, 12, 

191 21, 19, 12, 12, 12, 12, 12, 12, 

192 24, 12, 12, 12, 12, 12, 12, 12, 

193 17, 12, 12, 12, 12, 12, 12, 12], 

194 [13, 11, 13, 16, 20, 20, 17, 17, 

195 11, 14, 14, 14, 14, 12, 12, 12, 

196 13, 14, 14, 14, 12, 12, 12, 12, 

197 16, 14, 14, 12, 12, 12, 12, 12, 

198 20, 14, 12, 12, 12, 12, 12, 12, 

199 20, 12, 12, 12, 12, 12, 12, 12, 

200 17, 12, 12, 12, 12, 12, 12, 12, 

201 17, 12, 12, 12, 12, 12, 12, 12] 

202 ]}, 

203 'high': {'subsampling': 0, # "4:4:4" 

204 'quantization': [ 

205 [6, 4, 4, 6, 9, 11, 12, 16, 

206 4, 5, 5, 6, 8, 10, 12, 12, 

207 4, 5, 5, 6, 10, 12, 12, 12, 

208 6, 6, 6, 11, 12, 12, 12, 12, 

209 9, 8, 10, 12, 12, 12, 12, 12, 

210 11, 10, 12, 12, 12, 12, 12, 12, 

211 12, 12, 12, 12, 12, 12, 12, 12, 

212 16, 12, 12, 12, 12, 12, 12, 12], 

213 [7, 7, 13, 24, 20, 20, 17, 17, 

214 7, 12, 16, 14, 14, 12, 12, 12, 

215 13, 16, 14, 14, 12, 12, 12, 12, 

216 24, 14, 14, 12, 12, 12, 12, 12, 

217 20, 14, 12, 12, 12, 12, 12, 12, 

218 20, 12, 12, 12, 12, 12, 12, 12, 

219 17, 12, 12, 12, 12, 12, 12, 12, 

220 17, 12, 12, 12, 12, 12, 12, 12] 

221 ]}, 

222 'maximum': {'subsampling': 0, # "4:4:4" 

223 'quantization': [ 

224 [2, 2, 2, 2, 3, 4, 5, 6, 

225 2, 2, 2, 2, 3, 4, 5, 6, 

226 2, 2, 2, 2, 4, 5, 7, 9, 

227 2, 2, 2, 4, 5, 7, 9, 12, 

228 3, 3, 4, 5, 8, 10, 12, 12, 

229 4, 4, 5, 7, 10, 12, 12, 12, 

230 5, 5, 7, 9, 12, 12, 12, 12, 

231 6, 6, 9, 12, 12, 12, 12, 12], 

232 [3, 3, 5, 9, 13, 15, 15, 15, 

233 3, 4, 6, 10, 14, 12, 12, 12, 

234 5, 6, 9, 14, 12, 12, 12, 12, 

235 9, 10, 14, 12, 12, 12, 12, 12, 

236 13, 14, 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 15, 12, 12, 12, 12, 12, 12, 12] 

240 ]}, 

241} 

242# fmt: on