JpegImagePlugin.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # JPEG (JFIF) file handling
  6. #
  7. # See "Digital Compression and Coding of Continuous-Tone Still Images,
  8. # Part 1, Requirements and Guidelines" (CCITT T.81 / ISO 10918-1)
  9. #
  10. # History:
  11. # 1995-09-09 fl Created
  12. # 1995-09-13 fl Added full parser
  13. # 1996-03-25 fl Added hack to use the IJG command line utilities
  14. # 1996-05-05 fl Workaround Photoshop 2.5 CMYK polarity bug
  15. # 1996-05-28 fl Added draft support, JFIF version (0.1)
  16. # 1996-12-30 fl Added encoder options, added progression property (0.2)
  17. # 1997-08-27 fl Save mode 1 images as BW (0.3)
  18. # 1998-07-12 fl Added YCbCr to draft and save methods (0.4)
  19. # 1998-10-19 fl Don't hang on files using 16-bit DQT's (0.4.1)
  20. # 2001-04-16 fl Extract DPI settings from JFIF files (0.4.2)
  21. # 2002-07-01 fl Skip pad bytes before markers; identify Exif files (0.4.3)
  22. # 2003-04-25 fl Added experimental EXIF decoder (0.5)
  23. # 2003-06-06 fl Added experimental EXIF GPSinfo decoder
  24. # 2003-09-13 fl Extract COM markers
  25. # 2009-09-06 fl Added icc_profile support (from Florian Hoech)
  26. # 2009-03-06 fl Changed CMYK handling; always use Adobe polarity (0.6)
  27. # 2009-03-08 fl Added subsampling support (from Justin Huff).
  28. #
  29. # Copyright (c) 1997-2003 by Secret Labs AB.
  30. # Copyright (c) 1995-1996 by Fredrik Lundh.
  31. #
  32. # See the README file for information on usage and redistribution.
  33. #
  34. import array
  35. import io
  36. import os
  37. import struct
  38. import subprocess
  39. import sys
  40. import tempfile
  41. import warnings
  42. from . import Image, ImageFile, TiffImagePlugin
  43. from ._binary import i8
  44. from ._binary import i16be as i16
  45. from ._binary import i32be as i32
  46. from ._binary import o8
  47. from .JpegPresets import presets
  48. #
  49. # Parser
  50. def Skip(self, marker):
  51. n = i16(self.fp.read(2)) - 2
  52. ImageFile._safe_read(self.fp, n)
  53. def APP(self, marker):
  54. #
  55. # Application marker. Store these in the APP dictionary.
  56. # Also look for well-known application markers.
  57. n = i16(self.fp.read(2)) - 2
  58. s = ImageFile._safe_read(self.fp, n)
  59. app = "APP%d" % (marker & 15)
  60. self.app[app] = s # compatibility
  61. self.applist.append((app, s))
  62. if marker == 0xFFE0 and s[:4] == b"JFIF":
  63. # extract JFIF information
  64. self.info["jfif"] = version = i16(s, 5) # version
  65. self.info["jfif_version"] = divmod(version, 256)
  66. # extract JFIF properties
  67. try:
  68. jfif_unit = i8(s[7])
  69. jfif_density = i16(s, 8), i16(s, 10)
  70. except Exception:
  71. pass
  72. else:
  73. if jfif_unit == 1:
  74. self.info["dpi"] = jfif_density
  75. self.info["jfif_unit"] = jfif_unit
  76. self.info["jfif_density"] = jfif_density
  77. elif marker == 0xFFE1 and s[:5] == b"Exif\0":
  78. if "exif" not in self.info:
  79. # extract EXIF information (incomplete)
  80. self.info["exif"] = s # FIXME: value will change
  81. elif marker == 0xFFE2 and s[:5] == b"FPXR\0":
  82. # extract FlashPix information (incomplete)
  83. self.info["flashpix"] = s # FIXME: value will change
  84. elif marker == 0xFFE2 and s[:12] == b"ICC_PROFILE\0":
  85. # Since an ICC profile can be larger than the maximum size of
  86. # a JPEG marker (64K), we need provisions to split it into
  87. # multiple markers. The format defined by the ICC specifies
  88. # one or more APP2 markers containing the following data:
  89. # Identifying string ASCII "ICC_PROFILE\0" (12 bytes)
  90. # Marker sequence number 1, 2, etc (1 byte)
  91. # Number of markers Total of APP2's used (1 byte)
  92. # Profile data (remainder of APP2 data)
  93. # Decoders should use the marker sequence numbers to
  94. # reassemble the profile, rather than assuming that the APP2
  95. # markers appear in the correct sequence.
  96. self.icclist.append(s)
  97. elif marker == 0xFFED and s[:14] == b"Photoshop 3.0\x00":
  98. # parse the image resource block
  99. offset = 14
  100. photoshop = self.info.setdefault("photoshop", {})
  101. while s[offset : offset + 4] == b"8BIM":
  102. try:
  103. offset += 4
  104. # resource code
  105. code = i16(s, offset)
  106. offset += 2
  107. # resource name (usually empty)
  108. name_len = i8(s[offset])
  109. # name = s[offset+1:offset+1+name_len]
  110. offset += 1 + name_len
  111. offset += offset & 1 # align
  112. # resource data block
  113. size = i32(s, offset)
  114. offset += 4
  115. data = s[offset : offset + size]
  116. if code == 0x03ED: # ResolutionInfo
  117. data = {
  118. "XResolution": i32(data[:4]) / 65536,
  119. "DisplayedUnitsX": i16(data[4:8]),
  120. "YResolution": i32(data[8:12]) / 65536,
  121. "DisplayedUnitsY": i16(data[12:]),
  122. }
  123. photoshop[code] = data
  124. offset += size
  125. offset += offset & 1 # align
  126. except struct.error:
  127. break # insufficient data
  128. elif marker == 0xFFEE and s[:5] == b"Adobe":
  129. self.info["adobe"] = i16(s, 5)
  130. # extract Adobe custom properties
  131. try:
  132. adobe_transform = i8(s[1])
  133. except Exception:
  134. pass
  135. else:
  136. self.info["adobe_transform"] = adobe_transform
  137. elif marker == 0xFFE2 and s[:4] == b"MPF\0":
  138. # extract MPO information
  139. self.info["mp"] = s[4:]
  140. # offset is current location minus buffer size
  141. # plus constant header size
  142. self.info["mpoffset"] = self.fp.tell() - n + 4
  143. # If DPI isn't in JPEG header, fetch from EXIF
  144. if "dpi" not in self.info and "exif" in self.info:
  145. try:
  146. exif = self.getexif()
  147. resolution_unit = exif[0x0128]
  148. x_resolution = exif[0x011A]
  149. try:
  150. dpi = float(x_resolution[0]) / x_resolution[1]
  151. except TypeError:
  152. dpi = x_resolution
  153. if resolution_unit == 3: # cm
  154. # 1 dpcm = 2.54 dpi
  155. dpi *= 2.54
  156. self.info["dpi"] = int(dpi + 0.5), int(dpi + 0.5)
  157. except (KeyError, SyntaxError, ValueError, ZeroDivisionError):
  158. # SyntaxError for invalid/unreadable EXIF
  159. # KeyError for dpi not included
  160. # ZeroDivisionError for invalid dpi rational value
  161. # ValueError for x_resolution[0] being an invalid float
  162. self.info["dpi"] = 72, 72
  163. def COM(self, marker):
  164. #
  165. # Comment marker. Store these in the APP dictionary.
  166. n = i16(self.fp.read(2)) - 2
  167. s = ImageFile._safe_read(self.fp, n)
  168. self.info["comment"] = s
  169. self.app["COM"] = s # compatibility
  170. self.applist.append(("COM", s))
  171. def SOF(self, marker):
  172. #
  173. # Start of frame marker. Defines the size and mode of the
  174. # image. JPEG is colour blind, so we use some simple
  175. # heuristics to map the number of layers to an appropriate
  176. # mode. Note that this could be made a bit brighter, by
  177. # looking for JFIF and Adobe APP markers.
  178. n = i16(self.fp.read(2)) - 2
  179. s = ImageFile._safe_read(self.fp, n)
  180. self._size = i16(s[3:]), i16(s[1:])
  181. self.bits = i8(s[0])
  182. if self.bits != 8:
  183. raise SyntaxError(f"cannot handle {self.bits}-bit layers")
  184. self.layers = i8(s[5])
  185. if self.layers == 1:
  186. self.mode = "L"
  187. elif self.layers == 3:
  188. self.mode = "RGB"
  189. elif self.layers == 4:
  190. self.mode = "CMYK"
  191. else:
  192. raise SyntaxError(f"cannot handle {self.layers}-layer images")
  193. if marker in [0xFFC2, 0xFFC6, 0xFFCA, 0xFFCE]:
  194. self.info["progressive"] = self.info["progression"] = 1
  195. if self.icclist:
  196. # fixup icc profile
  197. self.icclist.sort() # sort by sequence number
  198. if i8(self.icclist[0][13]) == len(self.icclist):
  199. profile = []
  200. for p in self.icclist:
  201. profile.append(p[14:])
  202. icc_profile = b"".join(profile)
  203. else:
  204. icc_profile = None # wrong number of fragments
  205. self.info["icc_profile"] = icc_profile
  206. self.icclist = []
  207. for i in range(6, len(s), 3):
  208. t = s[i : i + 3]
  209. # 4-tuples: id, vsamp, hsamp, qtable
  210. self.layer.append((t[0], i8(t[1]) // 16, i8(t[1]) & 15, i8(t[2])))
  211. def DQT(self, marker):
  212. #
  213. # Define quantization table. Note that there might be more
  214. # than one table in each marker.
  215. # FIXME: The quantization tables can be used to estimate the
  216. # compression quality.
  217. n = i16(self.fp.read(2)) - 2
  218. s = ImageFile._safe_read(self.fp, n)
  219. while len(s):
  220. v = i8(s[0])
  221. precision = 1 if (v // 16 == 0) else 2 # in bytes
  222. qt_length = 1 + precision * 64
  223. if len(s) < qt_length:
  224. raise SyntaxError("bad quantization table marker")
  225. data = array.array("B" if precision == 1 else "H", s[1:qt_length])
  226. if sys.byteorder == "little" and precision > 1:
  227. data.byteswap() # the values are always big-endian
  228. self.quantization[v & 15] = data
  229. s = s[qt_length:]
  230. #
  231. # JPEG marker table
  232. MARKER = {
  233. 0xFFC0: ("SOF0", "Baseline DCT", SOF),
  234. 0xFFC1: ("SOF1", "Extended Sequential DCT", SOF),
  235. 0xFFC2: ("SOF2", "Progressive DCT", SOF),
  236. 0xFFC3: ("SOF3", "Spatial lossless", SOF),
  237. 0xFFC4: ("DHT", "Define Huffman table", Skip),
  238. 0xFFC5: ("SOF5", "Differential sequential DCT", SOF),
  239. 0xFFC6: ("SOF6", "Differential progressive DCT", SOF),
  240. 0xFFC7: ("SOF7", "Differential spatial", SOF),
  241. 0xFFC8: ("JPG", "Extension", None),
  242. 0xFFC9: ("SOF9", "Extended sequential DCT (AC)", SOF),
  243. 0xFFCA: ("SOF10", "Progressive DCT (AC)", SOF),
  244. 0xFFCB: ("SOF11", "Spatial lossless DCT (AC)", SOF),
  245. 0xFFCC: ("DAC", "Define arithmetic coding conditioning", Skip),
  246. 0xFFCD: ("SOF13", "Differential sequential DCT (AC)", SOF),
  247. 0xFFCE: ("SOF14", "Differential progressive DCT (AC)", SOF),
  248. 0xFFCF: ("SOF15", "Differential spatial (AC)", SOF),
  249. 0xFFD0: ("RST0", "Restart 0", None),
  250. 0xFFD1: ("RST1", "Restart 1", None),
  251. 0xFFD2: ("RST2", "Restart 2", None),
  252. 0xFFD3: ("RST3", "Restart 3", None),
  253. 0xFFD4: ("RST4", "Restart 4", None),
  254. 0xFFD5: ("RST5", "Restart 5", None),
  255. 0xFFD6: ("RST6", "Restart 6", None),
  256. 0xFFD7: ("RST7", "Restart 7", None),
  257. 0xFFD8: ("SOI", "Start of image", None),
  258. 0xFFD9: ("EOI", "End of image", None),
  259. 0xFFDA: ("SOS", "Start of scan", Skip),
  260. 0xFFDB: ("DQT", "Define quantization table", DQT),
  261. 0xFFDC: ("DNL", "Define number of lines", Skip),
  262. 0xFFDD: ("DRI", "Define restart interval", Skip),
  263. 0xFFDE: ("DHP", "Define hierarchical progression", SOF),
  264. 0xFFDF: ("EXP", "Expand reference component", Skip),
  265. 0xFFE0: ("APP0", "Application segment 0", APP),
  266. 0xFFE1: ("APP1", "Application segment 1", APP),
  267. 0xFFE2: ("APP2", "Application segment 2", APP),
  268. 0xFFE3: ("APP3", "Application segment 3", APP),
  269. 0xFFE4: ("APP4", "Application segment 4", APP),
  270. 0xFFE5: ("APP5", "Application segment 5", APP),
  271. 0xFFE6: ("APP6", "Application segment 6", APP),
  272. 0xFFE7: ("APP7", "Application segment 7", APP),
  273. 0xFFE8: ("APP8", "Application segment 8", APP),
  274. 0xFFE9: ("APP9", "Application segment 9", APP),
  275. 0xFFEA: ("APP10", "Application segment 10", APP),
  276. 0xFFEB: ("APP11", "Application segment 11", APP),
  277. 0xFFEC: ("APP12", "Application segment 12", APP),
  278. 0xFFED: ("APP13", "Application segment 13", APP),
  279. 0xFFEE: ("APP14", "Application segment 14", APP),
  280. 0xFFEF: ("APP15", "Application segment 15", APP),
  281. 0xFFF0: ("JPG0", "Extension 0", None),
  282. 0xFFF1: ("JPG1", "Extension 1", None),
  283. 0xFFF2: ("JPG2", "Extension 2", None),
  284. 0xFFF3: ("JPG3", "Extension 3", None),
  285. 0xFFF4: ("JPG4", "Extension 4", None),
  286. 0xFFF5: ("JPG5", "Extension 5", None),
  287. 0xFFF6: ("JPG6", "Extension 6", None),
  288. 0xFFF7: ("JPG7", "Extension 7", None),
  289. 0xFFF8: ("JPG8", "Extension 8", None),
  290. 0xFFF9: ("JPG9", "Extension 9", None),
  291. 0xFFFA: ("JPG10", "Extension 10", None),
  292. 0xFFFB: ("JPG11", "Extension 11", None),
  293. 0xFFFC: ("JPG12", "Extension 12", None),
  294. 0xFFFD: ("JPG13", "Extension 13", None),
  295. 0xFFFE: ("COM", "Comment", COM),
  296. }
  297. def _accept(prefix):
  298. # Magic number was taken from https://en.wikipedia.org/wiki/JPEG
  299. return prefix[0:3] == b"\xFF\xD8\xFF"
  300. ##
  301. # Image plugin for JPEG and JFIF images.
  302. class JpegImageFile(ImageFile.ImageFile):
  303. format = "JPEG"
  304. format_description = "JPEG (ISO 10918)"
  305. def _open(self):
  306. s = self.fp.read(3)
  307. if not _accept(s):
  308. raise SyntaxError("not a JPEG file")
  309. s = b"\xFF"
  310. # Create attributes
  311. self.bits = self.layers = 0
  312. # JPEG specifics (internal)
  313. self.layer = []
  314. self.huffman_dc = {}
  315. self.huffman_ac = {}
  316. self.quantization = {}
  317. self.app = {} # compatibility
  318. self.applist = []
  319. self.icclist = []
  320. while True:
  321. i = i8(s)
  322. if i == 0xFF:
  323. s = s + self.fp.read(1)
  324. i = i16(s)
  325. else:
  326. # Skip non-0xFF junk
  327. s = self.fp.read(1)
  328. continue
  329. if i in MARKER:
  330. name, description, handler = MARKER[i]
  331. if handler is not None:
  332. handler(self, i)
  333. if i == 0xFFDA: # start of scan
  334. rawmode = self.mode
  335. if self.mode == "CMYK":
  336. rawmode = "CMYK;I" # assume adobe conventions
  337. self.tile = [("jpeg", (0, 0) + self.size, 0, (rawmode, ""))]
  338. # self.__offset = self.fp.tell()
  339. break
  340. s = self.fp.read(1)
  341. elif i == 0 or i == 0xFFFF:
  342. # padded marker or junk; move on
  343. s = b"\xff"
  344. elif i == 0xFF00: # Skip extraneous data (escaped 0xFF)
  345. s = self.fp.read(1)
  346. else:
  347. raise SyntaxError("no marker found")
  348. def load_read(self, read_bytes):
  349. """
  350. internal: read more image data
  351. For premature EOF and LOAD_TRUNCATED_IMAGES adds EOI marker
  352. so libjpeg can finish decoding
  353. """
  354. s = self.fp.read(read_bytes)
  355. if not s and ImageFile.LOAD_TRUNCATED_IMAGES:
  356. # Premature EOF.
  357. # Pretend file is finished adding EOI marker
  358. return b"\xFF\xD9"
  359. return s
  360. def draft(self, mode, size):
  361. if len(self.tile) != 1:
  362. return
  363. # Protect from second call
  364. if self.decoderconfig:
  365. return
  366. d, e, o, a = self.tile[0]
  367. scale = 1
  368. original_size = self.size
  369. if a[0] == "RGB" and mode in ["L", "YCbCr"]:
  370. self.mode = mode
  371. a = mode, ""
  372. if size:
  373. scale = min(self.size[0] // size[0], self.size[1] // size[1])
  374. for s in [8, 4, 2, 1]:
  375. if scale >= s:
  376. break
  377. e = (
  378. e[0],
  379. e[1],
  380. (e[2] - e[0] + s - 1) // s + e[0],
  381. (e[3] - e[1] + s - 1) // s + e[1],
  382. )
  383. self._size = ((self.size[0] + s - 1) // s, (self.size[1] + s - 1) // s)
  384. scale = s
  385. self.tile = [(d, e, o, a)]
  386. self.decoderconfig = (scale, 0)
  387. box = (0, 0, original_size[0] / scale, original_size[1] / scale)
  388. return (self.mode, box)
  389. def load_djpeg(self):
  390. # ALTERNATIVE: handle JPEGs via the IJG command line utilities
  391. f, path = tempfile.mkstemp()
  392. os.close(f)
  393. if os.path.exists(self.filename):
  394. subprocess.check_call(["djpeg", "-outfile", path, self.filename])
  395. else:
  396. raise ValueError("Invalid Filename")
  397. try:
  398. with Image.open(path) as _im:
  399. _im.load()
  400. self.im = _im.im
  401. finally:
  402. try:
  403. os.unlink(path)
  404. except OSError:
  405. pass
  406. self.mode = self.im.mode
  407. self._size = self.im.size
  408. self.tile = []
  409. def _getexif(self):
  410. return _getexif(self)
  411. def _getmp(self):
  412. return _getmp(self)
  413. def _getexif(self):
  414. if "exif" not in self.info:
  415. return None
  416. return dict(self.getexif())
  417. def _getmp(self):
  418. # Extract MP information. This method was inspired by the "highly
  419. # experimental" _getexif version that's been in use for years now,
  420. # itself based on the ImageFileDirectory class in the TIFF plugin.
  421. # The MP record essentially consists of a TIFF file embedded in a JPEG
  422. # application marker.
  423. try:
  424. data = self.info["mp"]
  425. except KeyError:
  426. return None
  427. file_contents = io.BytesIO(data)
  428. head = file_contents.read(8)
  429. endianness = ">" if head[:4] == b"\x4d\x4d\x00\x2a" else "<"
  430. # process dictionary
  431. try:
  432. info = TiffImagePlugin.ImageFileDirectory_v2(head)
  433. file_contents.seek(info.next)
  434. info.load(file_contents)
  435. mp = dict(info)
  436. except Exception as e:
  437. raise SyntaxError("malformed MP Index (unreadable directory)") from e
  438. # it's an error not to have a number of images
  439. try:
  440. quant = mp[0xB001]
  441. except KeyError as e:
  442. raise SyntaxError("malformed MP Index (no number of images)") from e
  443. # get MP entries
  444. mpentries = []
  445. try:
  446. rawmpentries = mp[0xB002]
  447. for entrynum in range(0, quant):
  448. unpackedentry = struct.unpack_from(
  449. f"{endianness}LLLHH", rawmpentries, entrynum * 16
  450. )
  451. labels = ("Attribute", "Size", "DataOffset", "EntryNo1", "EntryNo2")
  452. mpentry = dict(zip(labels, unpackedentry))
  453. mpentryattr = {
  454. "DependentParentImageFlag": bool(mpentry["Attribute"] & (1 << 31)),
  455. "DependentChildImageFlag": bool(mpentry["Attribute"] & (1 << 30)),
  456. "RepresentativeImageFlag": bool(mpentry["Attribute"] & (1 << 29)),
  457. "Reserved": (mpentry["Attribute"] & (3 << 27)) >> 27,
  458. "ImageDataFormat": (mpentry["Attribute"] & (7 << 24)) >> 24,
  459. "MPType": mpentry["Attribute"] & 0x00FFFFFF,
  460. }
  461. if mpentryattr["ImageDataFormat"] == 0:
  462. mpentryattr["ImageDataFormat"] = "JPEG"
  463. else:
  464. raise SyntaxError("unsupported picture format in MPO")
  465. mptypemap = {
  466. 0x000000: "Undefined",
  467. 0x010001: "Large Thumbnail (VGA Equivalent)",
  468. 0x010002: "Large Thumbnail (Full HD Equivalent)",
  469. 0x020001: "Multi-Frame Image (Panorama)",
  470. 0x020002: "Multi-Frame Image: (Disparity)",
  471. 0x020003: "Multi-Frame Image: (Multi-Angle)",
  472. 0x030000: "Baseline MP Primary Image",
  473. }
  474. mpentryattr["MPType"] = mptypemap.get(mpentryattr["MPType"], "Unknown")
  475. mpentry["Attribute"] = mpentryattr
  476. mpentries.append(mpentry)
  477. mp[0xB002] = mpentries
  478. except KeyError as e:
  479. raise SyntaxError("malformed MP Index (bad MP Entry)") from e
  480. # Next we should try and parse the individual image unique ID list;
  481. # we don't because I've never seen this actually used in a real MPO
  482. # file and so can't test it.
  483. return mp
  484. # --------------------------------------------------------------------
  485. # stuff to save JPEG files
  486. RAWMODE = {
  487. "1": "L",
  488. "L": "L",
  489. "RGB": "RGB",
  490. "RGBX": "RGB",
  491. "CMYK": "CMYK;I", # assume adobe conventions
  492. "YCbCr": "YCbCr",
  493. }
  494. # fmt: off
  495. zigzag_index = (
  496. 0, 1, 5, 6, 14, 15, 27, 28,
  497. 2, 4, 7, 13, 16, 26, 29, 42,
  498. 3, 8, 12, 17, 25, 30, 41, 43,
  499. 9, 11, 18, 24, 31, 40, 44, 53,
  500. 10, 19, 23, 32, 39, 45, 52, 54,
  501. 20, 22, 33, 38, 46, 51, 55, 60,
  502. 21, 34, 37, 47, 50, 56, 59, 61,
  503. 35, 36, 48, 49, 57, 58, 62, 63,
  504. )
  505. samplings = {
  506. (1, 1, 1, 1, 1, 1): 0,
  507. (2, 1, 1, 1, 1, 1): 1,
  508. (2, 2, 1, 1, 1, 1): 2,
  509. }
  510. # fmt: on
  511. def convert_dict_qtables(qtables):
  512. qtables = [qtables[key] for key in range(len(qtables)) if key in qtables]
  513. for idx, table in enumerate(qtables):
  514. qtables[idx] = [table[i] for i in zigzag_index]
  515. return qtables
  516. def get_sampling(im):
  517. # There's no subsampling when images have only 1 layer
  518. # (grayscale images) or when they are CMYK (4 layers),
  519. # so set subsampling to the default value.
  520. #
  521. # NOTE: currently Pillow can't encode JPEG to YCCK format.
  522. # If YCCK support is added in the future, subsampling code will have
  523. # to be updated (here and in JpegEncode.c) to deal with 4 layers.
  524. if not hasattr(im, "layers") or im.layers in (1, 4):
  525. return -1
  526. sampling = im.layer[0][1:3] + im.layer[1][1:3] + im.layer[2][1:3]
  527. return samplings.get(sampling, -1)
  528. def _save(im, fp, filename):
  529. try:
  530. rawmode = RAWMODE[im.mode]
  531. except KeyError as e:
  532. raise OSError(f"cannot write mode {im.mode} as JPEG") from e
  533. info = im.encoderinfo
  534. dpi = [round(x) for x in info.get("dpi", (0, 0))]
  535. quality = info.get("quality", -1)
  536. subsampling = info.get("subsampling", -1)
  537. qtables = info.get("qtables")
  538. if quality == "keep":
  539. quality = -1
  540. subsampling = "keep"
  541. qtables = "keep"
  542. elif quality in presets:
  543. preset = presets[quality]
  544. quality = -1
  545. subsampling = preset.get("subsampling", -1)
  546. qtables = preset.get("quantization")
  547. elif not isinstance(quality, int):
  548. raise ValueError("Invalid quality setting")
  549. else:
  550. if subsampling in presets:
  551. subsampling = presets[subsampling].get("subsampling", -1)
  552. if isinstance(qtables, str) and qtables in presets:
  553. qtables = presets[qtables].get("quantization")
  554. if subsampling == "4:4:4":
  555. subsampling = 0
  556. elif subsampling == "4:2:2":
  557. subsampling = 1
  558. elif subsampling == "4:2:0":
  559. subsampling = 2
  560. elif subsampling == "4:1:1":
  561. # For compatibility. Before Pillow 4.3, 4:1:1 actually meant 4:2:0.
  562. # Set 4:2:0 if someone is still using that value.
  563. subsampling = 2
  564. elif subsampling == "keep":
  565. if im.format != "JPEG":
  566. raise ValueError("Cannot use 'keep' when original image is not a JPEG")
  567. subsampling = get_sampling(im)
  568. def validate_qtables(qtables):
  569. if qtables is None:
  570. return qtables
  571. if isinstance(qtables, str):
  572. try:
  573. lines = [
  574. int(num)
  575. for line in qtables.splitlines()
  576. for num in line.split("#", 1)[0].split()
  577. ]
  578. except ValueError as e:
  579. raise ValueError("Invalid quantization table") from e
  580. else:
  581. qtables = [lines[s : s + 64] for s in range(0, len(lines), 64)]
  582. if isinstance(qtables, (tuple, list, dict)):
  583. if isinstance(qtables, dict):
  584. qtables = convert_dict_qtables(qtables)
  585. elif isinstance(qtables, tuple):
  586. qtables = list(qtables)
  587. if not (0 < len(qtables) < 5):
  588. raise ValueError("None or too many quantization tables")
  589. for idx, table in enumerate(qtables):
  590. try:
  591. if len(table) != 64:
  592. raise TypeError
  593. table = array.array("H", table)
  594. except TypeError as e:
  595. raise ValueError("Invalid quantization table") from e
  596. else:
  597. qtables[idx] = list(table)
  598. return qtables
  599. if qtables == "keep":
  600. if im.format != "JPEG":
  601. raise ValueError("Cannot use 'keep' when original image is not a JPEG")
  602. qtables = getattr(im, "quantization", None)
  603. qtables = validate_qtables(qtables)
  604. extra = b""
  605. icc_profile = info.get("icc_profile")
  606. if icc_profile:
  607. ICC_OVERHEAD_LEN = 14
  608. MAX_BYTES_IN_MARKER = 65533
  609. MAX_DATA_BYTES_IN_MARKER = MAX_BYTES_IN_MARKER - ICC_OVERHEAD_LEN
  610. markers = []
  611. while icc_profile:
  612. markers.append(icc_profile[:MAX_DATA_BYTES_IN_MARKER])
  613. icc_profile = icc_profile[MAX_DATA_BYTES_IN_MARKER:]
  614. i = 1
  615. for marker in markers:
  616. size = struct.pack(">H", 2 + ICC_OVERHEAD_LEN + len(marker))
  617. extra += (
  618. b"\xFF\xE2"
  619. + size
  620. + b"ICC_PROFILE\0"
  621. + o8(i)
  622. + o8(len(markers))
  623. + marker
  624. )
  625. i += 1
  626. # "progressive" is the official name, but older documentation
  627. # says "progression"
  628. # FIXME: issue a warning if the wrong form is used (post-1.1.7)
  629. progressive = info.get("progressive", False) or info.get("progression", False)
  630. optimize = info.get("optimize", False)
  631. exif = info.get("exif", b"")
  632. if isinstance(exif, Image.Exif):
  633. exif = exif.tobytes()
  634. # get keyword arguments
  635. im.encoderconfig = (
  636. quality,
  637. progressive,
  638. info.get("smooth", 0),
  639. optimize,
  640. info.get("streamtype", 0),
  641. dpi[0],
  642. dpi[1],
  643. subsampling,
  644. qtables,
  645. extra,
  646. exif,
  647. )
  648. # if we optimize, libjpeg needs a buffer big enough to hold the whole image
  649. # in a shot. Guessing on the size, at im.size bytes. (raw pixel size is
  650. # channels*size, this is a value that's been used in a django patch.
  651. # https://github.com/matthewwithanm/django-imagekit/issues/50
  652. bufsize = 0
  653. if optimize or progressive:
  654. # CMYK can be bigger
  655. if im.mode == "CMYK":
  656. bufsize = 4 * im.size[0] * im.size[1]
  657. # keep sets quality to -1, but the actual value may be high.
  658. elif quality >= 95 or quality == -1:
  659. bufsize = 2 * im.size[0] * im.size[1]
  660. else:
  661. bufsize = im.size[0] * im.size[1]
  662. # The EXIF info needs to be written as one block, + APP1, + one spare byte.
  663. # Ensure that our buffer is big enough. Same with the icc_profile block.
  664. bufsize = max(ImageFile.MAXBLOCK, bufsize, len(exif) + 5, len(extra) + 1)
  665. ImageFile._save(im, fp, [("jpeg", (0, 0) + im.size, 0, rawmode)], bufsize)
  666. def _save_cjpeg(im, fp, filename):
  667. # ALTERNATIVE: handle JPEGs via the IJG command line utilities.
  668. tempfile = im._dump()
  669. subprocess.check_call(["cjpeg", "-outfile", filename, tempfile])
  670. try:
  671. os.unlink(tempfile)
  672. except OSError:
  673. pass
  674. ##
  675. # Factory for making JPEG and MPO instances
  676. def jpeg_factory(fp=None, filename=None):
  677. im = JpegImageFile(fp, filename)
  678. try:
  679. mpheader = im._getmp()
  680. if mpheader[45057] > 1:
  681. # It's actually an MPO
  682. from .MpoImagePlugin import MpoImageFile
  683. # Don't reload everything, just convert it.
  684. im = MpoImageFile.adopt(im, mpheader)
  685. except (TypeError, IndexError):
  686. # It is really a JPEG
  687. pass
  688. except SyntaxError:
  689. warnings.warn(
  690. "Image appears to be a malformed MPO file, it will be "
  691. "interpreted as a base JPEG file"
  692. )
  693. return im
  694. # ---------------------------------------------------------------------
  695. # Registry stuff
  696. Image.register_open(JpegImageFile.format, jpeg_factory, _accept)
  697. Image.register_save(JpegImageFile.format, _save)
  698. Image.register_extensions(JpegImageFile.format, [".jfif", ".jpe", ".jpg", ".jpeg"])
  699. Image.register_mime(JpegImageFile.format, "image/jpeg")