JpegImagePlugin.py 29 KB

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