You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JpegImagePlugin.py 27KB

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