JpegPresets.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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://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 lists. You can pass this dict
  36. directly as the qtables argument when saving a JPEG.
  37. The quantization table format in presets is a list with sublists. These formats
  38. are interchangeable.
  39. Libjpeg ref.:
  40. https://web.archive.org/web/20120328125543/http://www.jpegcameras.com/libjpeg/libjpeg-3.html
  41. """
  42. # fmt: off
  43. presets = {
  44. 'web_low': {'subsampling': 2, # "4:2:0"
  45. 'quantization': [
  46. [20, 16, 25, 39, 50, 46, 62, 68,
  47. 16, 18, 23, 38, 38, 53, 65, 68,
  48. 25, 23, 31, 38, 53, 65, 68, 68,
  49. 39, 38, 38, 53, 65, 68, 68, 68,
  50. 50, 38, 53, 65, 68, 68, 68, 68,
  51. 46, 53, 65, 68, 68, 68, 68, 68,
  52. 62, 65, 68, 68, 68, 68, 68, 68,
  53. 68, 68, 68, 68, 68, 68, 68, 68],
  54. [21, 25, 32, 38, 54, 68, 68, 68,
  55. 25, 28, 24, 38, 54, 68, 68, 68,
  56. 32, 24, 32, 43, 66, 68, 68, 68,
  57. 38, 38, 43, 53, 68, 68, 68, 68,
  58. 54, 54, 66, 68, 68, 68, 68, 68,
  59. 68, 68, 68, 68, 68, 68, 68, 68,
  60. 68, 68, 68, 68, 68, 68, 68, 68,
  61. 68, 68, 68, 68, 68, 68, 68, 68]
  62. ]},
  63. 'web_medium': {'subsampling': 2, # "4:2:0"
  64. 'quantization': [
  65. [16, 11, 11, 16, 23, 27, 31, 30,
  66. 11, 12, 12, 15, 20, 23, 23, 30,
  67. 11, 12, 13, 16, 23, 26, 35, 47,
  68. 16, 15, 16, 23, 26, 37, 47, 64,
  69. 23, 20, 23, 26, 39, 51, 64, 64,
  70. 27, 23, 26, 37, 51, 64, 64, 64,
  71. 31, 23, 35, 47, 64, 64, 64, 64,
  72. 30, 30, 47, 64, 64, 64, 64, 64],
  73. [17, 15, 17, 21, 20, 26, 38, 48,
  74. 15, 19, 18, 17, 20, 26, 35, 43,
  75. 17, 18, 20, 22, 26, 30, 46, 53,
  76. 21, 17, 22, 28, 30, 39, 53, 64,
  77. 20, 20, 26, 30, 39, 48, 64, 64,
  78. 26, 26, 30, 39, 48, 63, 64, 64,
  79. 38, 35, 46, 53, 64, 64, 64, 64,
  80. 48, 43, 53, 64, 64, 64, 64, 64]
  81. ]},
  82. 'web_high': {'subsampling': 0, # "4:4:4"
  83. 'quantization': [
  84. [6, 4, 4, 6, 9, 11, 12, 16,
  85. 4, 5, 5, 6, 8, 10, 12, 12,
  86. 4, 5, 5, 6, 10, 12, 14, 19,
  87. 6, 6, 6, 11, 12, 15, 19, 28,
  88. 9, 8, 10, 12, 16, 20, 27, 31,
  89. 11, 10, 12, 15, 20, 27, 31, 31,
  90. 12, 12, 14, 19, 27, 31, 31, 31,
  91. 16, 12, 19, 28, 31, 31, 31, 31],
  92. [7, 7, 13, 24, 26, 31, 31, 31,
  93. 7, 12, 16, 21, 31, 31, 31, 31,
  94. 13, 16, 17, 31, 31, 31, 31, 31,
  95. 24, 21, 31, 31, 31, 31, 31, 31,
  96. 26, 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. 31, 31, 31, 31, 31, 31, 31, 31]
  100. ]},
  101. 'web_very_high': {'subsampling': 0, # "4:4:4"
  102. 'quantization': [
  103. [2, 2, 2, 2, 3, 4, 5, 6,
  104. 2, 2, 2, 2, 3, 4, 5, 6,
  105. 2, 2, 2, 2, 4, 5, 7, 9,
  106. 2, 2, 2, 4, 5, 7, 9, 12,
  107. 3, 3, 4, 5, 8, 10, 12, 12,
  108. 4, 4, 5, 7, 10, 12, 12, 12,
  109. 5, 5, 7, 9, 12, 12, 12, 12,
  110. 6, 6, 9, 12, 12, 12, 12, 12],
  111. [3, 3, 5, 9, 13, 15, 15, 15,
  112. 3, 4, 6, 11, 14, 12, 12, 12,
  113. 5, 6, 9, 14, 12, 12, 12, 12,
  114. 9, 11, 14, 12, 12, 12, 12, 12,
  115. 13, 14, 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. 15, 12, 12, 12, 12, 12, 12, 12]
  119. ]},
  120. 'web_maximum': {'subsampling': 0, # "4:4:4"
  121. 'quantization': [
  122. [1, 1, 1, 1, 1, 1, 1, 1,
  123. 1, 1, 1, 1, 1, 1, 1, 1,
  124. 1, 1, 1, 1, 1, 1, 1, 2,
  125. 1, 1, 1, 1, 1, 1, 2, 2,
  126. 1, 1, 1, 1, 1, 2, 2, 3,
  127. 1, 1, 1, 1, 2, 2, 3, 3,
  128. 1, 1, 1, 2, 2, 3, 3, 3,
  129. 1, 1, 2, 2, 3, 3, 3, 3],
  130. [1, 1, 1, 2, 2, 3, 3, 3,
  131. 1, 1, 1, 2, 3, 3, 3, 3,
  132. 1, 1, 1, 3, 3, 3, 3, 3,
  133. 2, 2, 3, 3, 3, 3, 3, 3,
  134. 2, 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. 3, 3, 3, 3, 3, 3, 3, 3]
  138. ]},
  139. 'low': {'subsampling': 2, # "4:2:0"
  140. 'quantization': [
  141. [18, 14, 14, 21, 30, 35, 34, 17,
  142. 14, 16, 16, 19, 26, 23, 12, 12,
  143. 14, 16, 17, 21, 23, 12, 12, 12,
  144. 21, 19, 21, 23, 12, 12, 12, 12,
  145. 30, 26, 23, 12, 12, 12, 12, 12,
  146. 35, 23, 12, 12, 12, 12, 12, 12,
  147. 34, 12, 12, 12, 12, 12, 12, 12,
  148. 17, 12, 12, 12, 12, 12, 12, 12],
  149. [20, 19, 22, 27, 20, 20, 17, 17,
  150. 19, 25, 23, 14, 14, 12, 12, 12,
  151. 22, 23, 14, 14, 12, 12, 12, 12,
  152. 27, 14, 14, 12, 12, 12, 12, 12,
  153. 20, 14, 12, 12, 12, 12, 12, 12,
  154. 20, 12, 12, 12, 12, 12, 12, 12,
  155. 17, 12, 12, 12, 12, 12, 12, 12,
  156. 17, 12, 12, 12, 12, 12, 12, 12]
  157. ]},
  158. 'medium': {'subsampling': 2, # "4:2:0"
  159. 'quantization': [
  160. [12, 8, 8, 12, 17, 21, 24, 17,
  161. 8, 9, 9, 11, 15, 19, 12, 12,
  162. 8, 9, 10, 12, 19, 12, 12, 12,
  163. 12, 11, 12, 21, 12, 12, 12, 12,
  164. 17, 15, 19, 12, 12, 12, 12, 12,
  165. 21, 19, 12, 12, 12, 12, 12, 12,
  166. 24, 12, 12, 12, 12, 12, 12, 12,
  167. 17, 12, 12, 12, 12, 12, 12, 12],
  168. [13, 11, 13, 16, 20, 20, 17, 17,
  169. 11, 14, 14, 14, 14, 12, 12, 12,
  170. 13, 14, 14, 14, 12, 12, 12, 12,
  171. 16, 14, 14, 12, 12, 12, 12, 12,
  172. 20, 14, 12, 12, 12, 12, 12, 12,
  173. 20, 12, 12, 12, 12, 12, 12, 12,
  174. 17, 12, 12, 12, 12, 12, 12, 12,
  175. 17, 12, 12, 12, 12, 12, 12, 12]
  176. ]},
  177. 'high': {'subsampling': 0, # "4:4:4"
  178. 'quantization': [
  179. [6, 4, 4, 6, 9, 11, 12, 16,
  180. 4, 5, 5, 6, 8, 10, 12, 12,
  181. 4, 5, 5, 6, 10, 12, 12, 12,
  182. 6, 6, 6, 11, 12, 12, 12, 12,
  183. 9, 8, 10, 12, 12, 12, 12, 12,
  184. 11, 10, 12, 12, 12, 12, 12, 12,
  185. 12, 12, 12, 12, 12, 12, 12, 12,
  186. 16, 12, 12, 12, 12, 12, 12, 12],
  187. [7, 7, 13, 24, 20, 20, 17, 17,
  188. 7, 12, 16, 14, 14, 12, 12, 12,
  189. 13, 16, 14, 14, 12, 12, 12, 12,
  190. 24, 14, 14, 12, 12, 12, 12, 12,
  191. 20, 14, 12, 12, 12, 12, 12, 12,
  192. 20, 12, 12, 12, 12, 12, 12, 12,
  193. 17, 12, 12, 12, 12, 12, 12, 12,
  194. 17, 12, 12, 12, 12, 12, 12, 12]
  195. ]},
  196. 'maximum': {'subsampling': 0, # "4:4:4"
  197. 'quantization': [
  198. [2, 2, 2, 2, 3, 4, 5, 6,
  199. 2, 2, 2, 2, 3, 4, 5, 6,
  200. 2, 2, 2, 2, 4, 5, 7, 9,
  201. 2, 2, 2, 4, 5, 7, 9, 12,
  202. 3, 3, 4, 5, 8, 10, 12, 12,
  203. 4, 4, 5, 7, 10, 12, 12, 12,
  204. 5, 5, 7, 9, 12, 12, 12, 12,
  205. 6, 6, 9, 12, 12, 12, 12, 12],
  206. [3, 3, 5, 9, 13, 15, 15, 15,
  207. 3, 4, 6, 10, 14, 12, 12, 12,
  208. 5, 6, 9, 14, 12, 12, 12, 12,
  209. 9, 10, 14, 12, 12, 12, 12, 12,
  210. 13, 14, 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. 15, 12, 12, 12, 12, 12, 12, 12]
  214. ]},
  215. }
  216. # fmt: on