From d49ea4fb3f567351c63816e703348d8a9fd49ff0 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Tue, 16 Jun 2020 17:27:51 -0700 Subject: pdf_thumbnail script: demonstrate PDF thumbnail generation --- python/scripts/pdf_thumbnail.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100755 python/scripts/pdf_thumbnail.py (limited to 'python') diff --git a/python/scripts/pdf_thumbnail.py b/python/scripts/pdf_thumbnail.py new file mode 100755 index 0000000..e093dc3 --- /dev/null +++ b/python/scripts/pdf_thumbnail.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 + +""" +Quick CLI script to convert a PDF to thumbnail (.png, jpeg, etc). + +Originally used to benchmark and compare file size/quality. +""" + +import sys +import poppler +from PIL import Image + + +def run(inpath, outpath): + + try: + pdf = poppler.load_from_file(inpath) + page = pdf.create_page(0) + except Exception as e: + print(str(e), file=sys.stderr) + sys.exit(0) + + renderer = poppler.PageRenderer() + full_page = renderer.render_page(page) + img = Image.frombuffer("RGBA", (full_page.width, full_page.height), full_page.data, 'raw', "RGBA", 0, 1) + img.thumbnail((180,300), Image.BICUBIC) + #img.thumbnail((360,600), Image.BICUBIC) + img.save(outpath) + #img.save(outpath, quality=95) + +if __name__ == '__main__': + if len(sys.argv) != 3: + print("expect two parameters: INPUT.png OUTPUT.png", file=sys.stderr) + sys.exit(-1) + run(sys.argv[1], sys.argv[2]) -- cgit v1.2.3