Last active
January 3, 2021 21:14
-
-
Save dat-boris/0b1d06ef95b67a50b43acdb5b24500cd to your computer and use it in GitHub Desktop.
A script I use to export PnP file into JPG for Tabletop Simulator import
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
"""Script for joining PDF into image output. | |
Prerequisite: | |
# This takes a bit of time! | |
brew install poppler | |
pipenv install pillow pdf2image | |
I use this for converting PnP pdf into image to be uploaded to TTS. | |
""" | |
import os | |
import sys | |
from pdf2image import convert_from_path | |
from PIL import Image | |
IMG_FOLDER = 'genfile' | |
# How many last pages to miss from PDF | |
PAGES_TRUNCATE = 2 | |
MARGIN_TOP = 50 | |
MARGIN_LEFT = 100 | |
# Only print every mod 2 pages | |
EVERY_OTHER_PAGE = 2 | |
# TTS only allows maximum height of (10, 7) | |
MAX_PAGES = 3 # Allow 3 pages since we can align horizontally | |
START_PAGE = 0 | |
def convert_pages_to_image(pdf_file, pages_truncate=PAGES_TRUNCATE): | |
print(f"Reading pdf file: {pdf_file}") | |
pages = convert_from_path(pdf_file) | |
try: | |
os.mkdir(IMG_FOLDER) | |
except OSError: | |
pass | |
imgs = [] | |
for i, page in enumerate(pages[START_PAGE:-pages_truncate]): | |
if (i % EVERY_OTHER_PAGE) != 0: | |
continue | |
img_name = '{}/out_{}.jpg'.format(IMG_FOLDER, i) | |
print(f"Writing tmp image: {img_name}") | |
page.save(img_name, 'JPEG') | |
imgs.append(img_name) | |
return imgs | |
def join_images(img_files, out_prefix): | |
images = [Image.open(x) for x in img_files] | |
widths, heights = zip(*(i.size for i in images)) | |
# Remember to exclude the margin! | |
each_img_len = min(MAX_PAGES, len(img_files)) | |
total_width = (widths[0] - (MARGIN_LEFT*2)) * each_img_len | |
total_height = heights[0] - (MARGIN_TOP*2) | |
new_im = Image.new('RGB', (total_width, total_height)) | |
new_im_idx = 0 | |
new_im_img_count = 0 | |
x_offset = 0 | |
for im in images: | |
w0, h0 = im.size | |
cropped_img = im.crop(( | |
MARGIN_LEFT, MARGIN_TOP, | |
w0-MARGIN_LEFT, h0-MARGIN_TOP | |
)) | |
w, h = cropped_img.size | |
assert w + MARGIN_LEFT * 2 == w0, f"Image cropped {w0} -> {w}" | |
new_im.paste(cropped_img, (x_offset, 0)) | |
x_offset += w | |
new_im_img_count += 1 | |
# Now decide pagination | |
if new_im_img_count > MAX_PAGES: | |
out_img_name = f"{out_prefix}_{new_im_idx}.jpg" | |
print(f"Saving to: {out_img_name}") | |
new_im.save(out_img_name) | |
x_offset = 0 | |
new_im_img_count = 0 | |
new_im_idx += 1 | |
out_img_name = f"{out_prefix}_{new_im_idx}.jpg" | |
print(f"Saving final img to: {out_img_name}") | |
new_im.save(out_img_name) | |
if __name__ == "__main__": | |
pdf_file = sys.argv[1] | |
imgs = convert_pages_to_image(pdf_file) | |
out_img_prefix = 'added' | |
join_images(imgs, out_img_prefix) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment