JpegImagePlugin.py 31 KB

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