Allow asymmetric PDF export resolution

Allow PDF exporting resolution to be specified as a tuple too, to get asymmetric DPIs sometimes used by sticker/inkjet printers, e.g 600x1200dpi.
This commit is contained in:
tzjtan 2019-04-01 22:00:26 -04:00 committed by GitHub
parent 4f3b44960f
commit f8d46cc79c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -56,6 +56,11 @@ def _save(im, fp, filename, save_all=False):
existing_pdf = PdfParser.PdfParser(f=fp, filename=filename, mode="w+b") existing_pdf = PdfParser.PdfParser(f=fp, filename=filename, mode="w+b")
resolution = im.encoderinfo.get("resolution", 72.0) resolution = im.encoderinfo.get("resolution", 72.0)
# Allows the user to specify either a 1D resolution or 2D resolution.
if not isinstance(resolution,(list,tuple)):
resolution2D = (resolution,resolution)
else:
resolution2D = resolution
info = { info = {
"title": None if is_appending else os.path.splitext( "title": None if is_appending else os.path.splitext(
@ -211,8 +216,8 @@ def _save(im, fp, filename, save_all=False):
MediaBox=[ MediaBox=[
0, 0,
0, 0,
int(width * 72.0 / resolution), int(width * 72.0 / resolution2D[0]),
int(height * 72.0 / resolution) int(height * 72.0 / resolution2D[1])
], ],
Contents=contents_refs[pageNumber]) Contents=contents_refs[pageNumber])
@ -221,8 +226,8 @@ def _save(im, fp, filename, save_all=False):
page_contents = PdfParser.make_bytes( page_contents = PdfParser.make_bytes(
"q %d 0 0 %d 0 0 cm /image Do Q\n" % ( "q %d 0 0 %d 0 0 cm /image Do Q\n" % (
int(width * 72.0 / resolution), int(width * 72.0 / resolution2D[0]),
int(height * 72.0 / resolution))) int(height * 72.0 / resolution2D[1])))
existing_pdf.write_obj(contents_refs[pageNumber], existing_pdf.write_obj(contents_refs[pageNumber],
stream=page_contents) stream=page_contents)