mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-27 10:26:19 +03:00
Moved writing of object into separate function
This commit is contained in:
parent
a5b0256290
commit
c9147c9c85
|
@ -46,81 +46,7 @@ def _save_all(im, fp, filename):
|
||||||
# (Internal) Image save plugin for the PDF format.
|
# (Internal) Image save plugin for the PDF format.
|
||||||
|
|
||||||
|
|
||||||
def _save(im, fp, filename, save_all=False):
|
def _write_image(im, filename, existing_pdf, image_refs):
|
||||||
is_appending = im.encoderinfo.get("append", False)
|
|
||||||
if is_appending:
|
|
||||||
existing_pdf = PdfParser.PdfParser(f=fp, filename=filename, mode="r+b")
|
|
||||||
else:
|
|
||||||
existing_pdf = PdfParser.PdfParser(f=fp, filename=filename, mode="w+b")
|
|
||||||
|
|
||||||
dpi = im.encoderinfo.get("dpi")
|
|
||||||
if dpi:
|
|
||||||
x_resolution = dpi[0]
|
|
||||||
y_resolution = dpi[1]
|
|
||||||
else:
|
|
||||||
x_resolution = y_resolution = im.encoderinfo.get("resolution", 72.0)
|
|
||||||
|
|
||||||
info = {
|
|
||||||
"title": None
|
|
||||||
if is_appending
|
|
||||||
else os.path.splitext(os.path.basename(filename))[0],
|
|
||||||
"author": None,
|
|
||||||
"subject": None,
|
|
||||||
"keywords": None,
|
|
||||||
"creator": None,
|
|
||||||
"producer": None,
|
|
||||||
"creationDate": None if is_appending else time.gmtime(),
|
|
||||||
"modDate": None if is_appending else time.gmtime(),
|
|
||||||
}
|
|
||||||
for k, default in info.items():
|
|
||||||
v = im.encoderinfo.get(k) if k in im.encoderinfo else default
|
|
||||||
if v:
|
|
||||||
existing_pdf.info[k[0].upper() + k[1:]] = v
|
|
||||||
|
|
||||||
#
|
|
||||||
# make sure image data is available
|
|
||||||
im.load()
|
|
||||||
|
|
||||||
existing_pdf.start_writing()
|
|
||||||
existing_pdf.write_header()
|
|
||||||
existing_pdf.write_comment(f"created by Pillow {__version__} PDF driver")
|
|
||||||
|
|
||||||
#
|
|
||||||
# pages
|
|
||||||
ims = [im]
|
|
||||||
if save_all:
|
|
||||||
append_images = im.encoderinfo.get("append_images", [])
|
|
||||||
for append_im in append_images:
|
|
||||||
append_im.encoderinfo = im.encoderinfo.copy()
|
|
||||||
ims.append(append_im)
|
|
||||||
number_of_pages = 0
|
|
||||||
image_refs = []
|
|
||||||
page_refs = []
|
|
||||||
contents_refs = []
|
|
||||||
for im in ims:
|
|
||||||
im_number_of_pages = 1
|
|
||||||
if save_all:
|
|
||||||
try:
|
|
||||||
im_number_of_pages = im.n_frames
|
|
||||||
except AttributeError:
|
|
||||||
# Image format does not have n_frames.
|
|
||||||
# It is a single frame image
|
|
||||||
pass
|
|
||||||
number_of_pages += im_number_of_pages
|
|
||||||
for i in range(im_number_of_pages):
|
|
||||||
image_refs.append(existing_pdf.next_object_id(0))
|
|
||||||
page_refs.append(existing_pdf.next_object_id(0))
|
|
||||||
contents_refs.append(existing_pdf.next_object_id(0))
|
|
||||||
existing_pdf.pages.append(page_refs[-1])
|
|
||||||
|
|
||||||
#
|
|
||||||
# catalog and list of pages
|
|
||||||
existing_pdf.write_catalog()
|
|
||||||
|
|
||||||
page_number = 0
|
|
||||||
for im_sequence in ims:
|
|
||||||
im_pages = ImageSequence.Iterator(im_sequence) if save_all else [im_sequence]
|
|
||||||
for im in im_pages:
|
|
||||||
# FIXME: Should replace ASCIIHexDecode with RunLengthDecode
|
# FIXME: Should replace ASCIIHexDecode with RunLengthDecode
|
||||||
# (packbits) or LZWDecode (tiff/lzw compression). Note that
|
# (packbits) or LZWDecode (tiff/lzw compression). Note that
|
||||||
# PDF 1.2 also supports Flatedecode (zip compression).
|
# PDF 1.2 also supports Flatedecode (zip compression).
|
||||||
|
@ -239,6 +165,86 @@ def _save(im, fp, filename, save_all=False):
|
||||||
**dict_obj,
|
**dict_obj,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
return procset
|
||||||
|
|
||||||
|
|
||||||
|
def _save(im, fp, filename, save_all=False):
|
||||||
|
is_appending = im.encoderinfo.get("append", False)
|
||||||
|
if is_appending:
|
||||||
|
existing_pdf = PdfParser.PdfParser(f=fp, filename=filename, mode="r+b")
|
||||||
|
else:
|
||||||
|
existing_pdf = PdfParser.PdfParser(f=fp, filename=filename, mode="w+b")
|
||||||
|
|
||||||
|
dpi = im.encoderinfo.get("dpi")
|
||||||
|
if dpi:
|
||||||
|
x_resolution = dpi[0]
|
||||||
|
y_resolution = dpi[1]
|
||||||
|
else:
|
||||||
|
x_resolution = y_resolution = im.encoderinfo.get("resolution", 72.0)
|
||||||
|
|
||||||
|
info = {
|
||||||
|
"title": None
|
||||||
|
if is_appending
|
||||||
|
else os.path.splitext(os.path.basename(filename))[0],
|
||||||
|
"author": None,
|
||||||
|
"subject": None,
|
||||||
|
"keywords": None,
|
||||||
|
"creator": None,
|
||||||
|
"producer": None,
|
||||||
|
"creationDate": None if is_appending else time.gmtime(),
|
||||||
|
"modDate": None if is_appending else time.gmtime(),
|
||||||
|
}
|
||||||
|
for k, default in info.items():
|
||||||
|
v = im.encoderinfo.get(k) if k in im.encoderinfo else default
|
||||||
|
if v:
|
||||||
|
existing_pdf.info[k[0].upper() + k[1:]] = v
|
||||||
|
|
||||||
|
#
|
||||||
|
# make sure image data is available
|
||||||
|
im.load()
|
||||||
|
|
||||||
|
existing_pdf.start_writing()
|
||||||
|
existing_pdf.write_header()
|
||||||
|
existing_pdf.write_comment(f"created by Pillow {__version__} PDF driver")
|
||||||
|
|
||||||
|
#
|
||||||
|
# pages
|
||||||
|
ims = [im]
|
||||||
|
if save_all:
|
||||||
|
append_images = im.encoderinfo.get("append_images", [])
|
||||||
|
for append_im in append_images:
|
||||||
|
append_im.encoderinfo = im.encoderinfo.copy()
|
||||||
|
ims.append(append_im)
|
||||||
|
number_of_pages = 0
|
||||||
|
image_refs = []
|
||||||
|
page_refs = []
|
||||||
|
contents_refs = []
|
||||||
|
for im in ims:
|
||||||
|
im_number_of_pages = 1
|
||||||
|
if save_all:
|
||||||
|
try:
|
||||||
|
im_number_of_pages = im.n_frames
|
||||||
|
except AttributeError:
|
||||||
|
# Image format does not have n_frames.
|
||||||
|
# It is a single frame image
|
||||||
|
pass
|
||||||
|
number_of_pages += im_number_of_pages
|
||||||
|
for i in range(im_number_of_pages):
|
||||||
|
image_refs.append(existing_pdf.next_object_id(0))
|
||||||
|
page_refs.append(existing_pdf.next_object_id(0))
|
||||||
|
contents_refs.append(existing_pdf.next_object_id(0))
|
||||||
|
existing_pdf.pages.append(page_refs[-1])
|
||||||
|
|
||||||
|
#
|
||||||
|
# catalog and list of pages
|
||||||
|
existing_pdf.write_catalog()
|
||||||
|
|
||||||
|
page_number = 0
|
||||||
|
for im_sequence in ims:
|
||||||
|
im_pages = ImageSequence.Iterator(im_sequence) if save_all else [im_sequence]
|
||||||
|
for im in im_pages:
|
||||||
|
procset = _write_image(im, filename, existing_pdfs, image_refs)
|
||||||
|
|
||||||
#
|
#
|
||||||
# page
|
# page
|
||||||
|
|
||||||
|
@ -251,8 +257,8 @@ def _save(im, fp, filename, save_all=False):
|
||||||
MediaBox=[
|
MediaBox=[
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
width * 72.0 / x_resolution,
|
im.width * 72.0 / x_resolution,
|
||||||
height * 72.0 / y_resolution,
|
im.height * 72.0 / y_resolution,
|
||||||
],
|
],
|
||||||
Contents=contents_refs[page_number],
|
Contents=contents_refs[page_number],
|
||||||
)
|
)
|
||||||
|
@ -261,8 +267,8 @@ def _save(im, fp, filename, save_all=False):
|
||||||
# page contents
|
# page contents
|
||||||
|
|
||||||
page_contents = b"q %f 0 0 %f 0 0 cm /image Do Q\n" % (
|
page_contents = b"q %f 0 0 %f 0 0 cm /image Do Q\n" % (
|
||||||
width * 72.0 / x_resolution,
|
im.width * 72.0 / x_resolution,
|
||||||
height * 72.0 / y_resolution,
|
im.height * 72.0 / y_resolution,
|
||||||
)
|
)
|
||||||
|
|
||||||
existing_pdf.write_obj(contents_refs[page_number], stream=page_contents)
|
existing_pdf.write_obj(contents_refs[page_number], stream=page_contents)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user