Skip to content

Commit

Permalink
Merge d08bc5a into 0132771
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Jun 20, 2016
2 parents 0132771 + d08bc5a commit 7c1cb36
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions PIL/PdfImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
from PIL import Image, ImageFile
from PIL._binary import i8
import io
import sys
if sys.platform == "darwin":
from PIL._util import isPath
import os
import tempfile
import subprocess

__version__ = "0.4"

Expand Down Expand Up @@ -247,9 +253,37 @@ def write(self, value):
if hasattr(fp, "flush"):
fp.flush()

def _accept(prefix):
return prefix[:5] == "%PDF-"

def _convertToPng(fp, filename):
fh, filepath = tempfile.mkstemp('.png')
os.close(fh)
if isPath(fp):
# filename
filename = fp
else:
# stream
fh, filename = tempfile.mkstemp('.pdf')
os.write(fh, fp.read())
os.close(fh)
with open(os.devnull, 'w') as fp:
subprocess.call(['sips', '-s', 'format', 'png', filename, '--out', filepath], stdout=fp)

if os.stat(filepath).st_size == 0:
raise SyntaxError("PDF file could not be converted")

im = Image.open(filepath)
im.load()
os.unlink(filepath)
return im

#
# --------------------------------------------------------------------

if sys.platform == "darwin":
Image.register_open("PDF", _convertToPng, _accept)

Image.register_save("PDF", _save)
Image.register_save_all("PDF", _save_all)

Expand Down

0 comments on commit 7c1cb36

Please sign in to comment.