JpegPresets.py 12 KB

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