PdfImagePlugin.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # PDF (Acrobat) file handling
  6. #
  7. # History:
  8. # 1996-07-16 fl Created
  9. # 1997-01-18 fl Fixed header
  10. # 2004-02-21 fl Fixes for 1/L/CMYK images, etc.
  11. # 2004-02-24 fl Fixes for 1 and P images.
  12. #
  13. # Copyright (c) 1997-2004 by Secret Labs AB. All rights reserved.
  14. # Copyright (c) 1996-1997 by Fredrik Lundh.
  15. #
  16. # See the README file for information on usage and redistribution.
  17. #
  18. ##
  19. # Image plugin for PDF images (output only).
  20. ##
  21. import io
  22. import math
  23. import os
  24. import time
  25. from . import Image, ImageFile, ImageSequence, PdfParser, __version__, features
  26. #
  27. # --------------------------------------------------------------------
  28. # object ids:
  29. # 1. catalogue
  30. # 2. pages
  31. # 3. image
  32. # 4. page
  33. # 5. page contents
  34. def _save_all(im, fp, filename):
  35. _save(im, fp, filename, save_all=True)
  36. ##
  37. # (Internal) Image save plugin for the PDF format.
  38. def _write_image(im, filename, existing_pdf, image_refs):
  39. # FIXME: Should replace ASCIIHexDecode with RunLengthDecode
  40. # (packbits) or LZWDecode (tiff/lzw compression). Note that
  41. # PDF 1.2 also supports Flatedecode (zip compression).
  42. params = None
  43. decode = None
  44. #
  45. # Get image characteristics
  46. width, height = im.size
  47. dict_obj = {"BitsPerComponent": 8}
  48. if im.mode == "1":
  49. if features.check("libtiff"):
  50. filter = "CCITTFaxDecode"
  51. dict_obj["BitsPerComponent"] = 1
  52. params = PdfParser.PdfArray(
  53. [
  54. PdfParser.PdfDict(
  55. {
  56. "K": -1,
  57. "BlackIs1": True,
  58. "Columns": width,
  59. "Rows": height,
  60. }
  61. )
  62. ]
  63. )
  64. else:
  65. filter = "DCTDecode"
  66. dict_obj["ColorSpace"] = PdfParser.PdfName("DeviceGray")
  67. procset = "ImageB" # grayscale
  68. elif im.mode == "L":
  69. filter = "DCTDecode"
  70. # params = f"<< /Predictor 15 /Columns {width-2} >>"
  71. dict_obj["ColorSpace"] = PdfParser.PdfName("DeviceGray")
  72. procset = "ImageB" # grayscale
  73. elif im.mode == "LA":
  74. filter = "JPXDecode"
  75. # params = f"<< /Predictor 15 /Columns {width-2} >>"
  76. procset = "ImageB" # grayscale
  77. dict_obj["SMaskInData"] = 1
  78. elif im.mode == "P":
  79. filter = "ASCIIHexDecode"
  80. palette = im.getpalette()
  81. dict_obj["ColorSpace"] = [
  82. PdfParser.PdfName("Indexed"),
  83. PdfParser.PdfName("DeviceRGB"),
  84. 255,
  85. PdfParser.PdfBinary(palette),
  86. ]
  87. procset = "ImageI" # indexed color
  88. if "transparency" in im.info:
  89. smask = im.convert("LA").getchannel("A")
  90. smask.encoderinfo = {}
  91. image_ref = _write_image(smask, filename, existing_pdf, image_refs)[0]
  92. dict_obj["SMask"] = image_ref
  93. elif im.mode == "RGB":
  94. filter = "DCTDecode"
  95. dict_obj["ColorSpace"] = PdfParser.PdfName("DeviceRGB")
  96. procset = "ImageC" # color images
  97. elif im.mode == "RGBA":
  98. filter = "JPXDecode"
  99. procset = "ImageC" # color images
  100. dict_obj["SMaskInData"] = 1
  101. elif im.mode == "CMYK":
  102. filter = "DCTDecode"
  103. dict_obj["ColorSpace"] = PdfParser.PdfName("DeviceCMYK")
  104. procset = "ImageC" # color images
  105. decode = [1, 0, 1, 0, 1, 0, 1, 0]
  106. else:
  107. msg = f"cannot save mode {im.mode}"
  108. raise ValueError(msg)
  109. #
  110. # image
  111. op = io.BytesIO()
  112. if filter == "ASCIIHexDecode":
  113. ImageFile._save(im, op, [("hex", (0, 0) + im.size, 0, im.mode)])
  114. elif filter == "CCITTFaxDecode":
  115. im.save(
  116. op,
  117. "TIFF",
  118. compression="group4",
  119. # use a single strip
  120. strip_size=math.ceil(width / 8) * height,
  121. )
  122. elif filter == "DCTDecode":
  123. Image.SAVE["JPEG"](im, op, filename)
  124. elif filter == "JPXDecode":
  125. del dict_obj["BitsPerComponent"]
  126. Image.SAVE["JPEG2000"](im, op, filename)
  127. else:
  128. msg = f"unsupported PDF filter ({filter})"
  129. raise ValueError(msg)
  130. stream = op.getvalue()
  131. if filter == "CCITTFaxDecode":
  132. stream = stream[8:]
  133. filter = PdfParser.PdfArray([PdfParser.PdfName(filter)])
  134. else:
  135. filter = PdfParser.PdfName(filter)
  136. image_ref = image_refs.pop(0)
  137. existing_pdf.write_obj(
  138. image_ref,
  139. stream=stream,
  140. Type=PdfParser.PdfName("XObject"),
  141. Subtype=PdfParser.PdfName("Image"),
  142. Width=width, # * 72.0 / x_resolution,
  143. Height=height, # * 72.0 / y_resolution,
  144. Filter=filter,
  145. Decode=decode,
  146. DecodeParms=params,
  147. **dict_obj,
  148. )
  149. return image_ref, procset
  150. def _save(im, fp, filename, save_all=False):
  151. is_appending = im.encoderinfo.get("append", False)
  152. if is_appending:
  153. existing_pdf = PdfParser.PdfParser(f=fp, filename=filename, mode="r+b")
  154. else:
  155. existing_pdf = PdfParser.PdfParser(f=fp, filename=filename, mode="w+b")
  156. dpi = im.encoderinfo.get("dpi")
  157. if dpi:
  158. x_resolution = dpi[0]
  159. y_resolution = dpi[1]
  160. else:
  161. x_resolution = y_resolution = im.encoderinfo.get("resolution", 72.0)
  162. info = {
  163. "title": None
  164. if is_appending
  165. else os.path.splitext(os.path.basename(filename))[0],
  166. "author": None,
  167. "subject": None,
  168. "keywords": None,
  169. "creator": None,
  170. "producer": None,
  171. "creationDate": None if is_appending else time.gmtime(),
  172. "modDate": None if is_appending else time.gmtime(),
  173. }
  174. for k, default in info.items():
  175. v = im.encoderinfo.get(k) if k in im.encoderinfo else default
  176. if v:
  177. existing_pdf.info[k[0].upper() + k[1:]] = v
  178. #
  179. # make sure image data is available
  180. im.load()
  181. existing_pdf.start_writing()
  182. existing_pdf.write_header()
  183. existing_pdf.write_comment(f"created by Pillow {__version__} PDF driver")
  184. #
  185. # pages
  186. ims = [im]
  187. if save_all:
  188. append_images = im.encoderinfo.get("append_images", [])
  189. for append_im in append_images:
  190. append_im.encoderinfo = im.encoderinfo.copy()
  191. ims.append(append_im)
  192. number_of_pages = 0
  193. image_refs = []
  194. page_refs = []
  195. contents_refs = []
  196. for im in ims:
  197. im_number_of_pages = 1
  198. if save_all:
  199. try:
  200. im_number_of_pages = im.n_frames
  201. except AttributeError:
  202. # Image format does not have n_frames.
  203. # It is a single frame image
  204. pass
  205. number_of_pages += im_number_of_pages
  206. for i in range(im_number_of_pages):
  207. image_refs.append(existing_pdf.next_object_id(0))
  208. if im.mode == "P" and "transparency" in im.info:
  209. image_refs.append(existing_pdf.next_object_id(0))
  210. page_refs.append(existing_pdf.next_object_id(0))
  211. contents_refs.append(existing_pdf.next_object_id(0))
  212. existing_pdf.pages.append(page_refs[-1])
  213. #
  214. # catalog and list of pages
  215. existing_pdf.write_catalog()
  216. page_number = 0
  217. for im_sequence in ims:
  218. im_pages = ImageSequence.Iterator(im_sequence) if save_all else [im_sequence]
  219. for im in im_pages:
  220. image_ref, procset = _write_image(im, filename, existing_pdf, image_refs)
  221. #
  222. # page
  223. existing_pdf.write_page(
  224. page_refs[page_number],
  225. Resources=PdfParser.PdfDict(
  226. ProcSet=[PdfParser.PdfName("PDF"), PdfParser.PdfName(procset)],
  227. XObject=PdfParser.PdfDict(image=image_ref),
  228. ),
  229. MediaBox=[
  230. 0,
  231. 0,
  232. im.width * 72.0 / x_resolution,
  233. im.height * 72.0 / y_resolution,
  234. ],
  235. Contents=contents_refs[page_number],
  236. )
  237. #
  238. # page contents
  239. page_contents = b"q %f 0 0 %f 0 0 cm /image Do Q\n" % (
  240. im.width * 72.0 / x_resolution,
  241. im.height * 72.0 / y_resolution,
  242. )
  243. existing_pdf.write_obj(contents_refs[page_number], stream=page_contents)
  244. page_number += 1
  245. #
  246. # trailer
  247. existing_pdf.write_xref_and_trailer()
  248. if hasattr(fp, "flush"):
  249. fp.flush()
  250. existing_pdf.close()
  251. #
  252. # --------------------------------------------------------------------
  253. Image.register_save("PDF", _save)
  254. Image.register_save_all("PDF", _save_all)
  255. Image.register_extension("PDF", ".pdf")
  256. Image.register_mime("PDF", "application/pdf")