You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

241 lines
12 KiB

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