JpegImagePlugin.py 29 KB

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