import pdfplumber import os import time import subprocess import config from PIL import ImageFont def draw_boxes(page, boxes): im = debug_im(page) for i in boxes: i["y0"] = page.height - i["y0"] i["y1"] = page.height - i["y1"] #i["x0"] *= im.original.height / page.height #i["x1"] *= im.original.height / page.height #i["y0"] *= im.original.height / page.height #i["y1"] *= im.original.height / page.height for box in boxes: im.draw_line(((box["x0"], box["y0"]), (box["x1"], box["y0"]))) im.draw_line(((box["x1"], box["y0"]), (box["x1"], box["y1"]))) im.draw_line(((box["x1"], box["y1"]), (box["x0"], box["y1"]))) im.draw_line(((box["x0"], box["y1"]), (box["x0"], box["y0"]))) if "debug_label" in box: font = ImageFont.truetype("arial.ttf", 24) for offset in [-2, -1, 1, 2]: im.draw.text( xy=(offset + box["x0"] * im.original.height / page.height, offset + box["y0"] * im.original.height / page.height), text=str(box["debug_label"]), fill=(255,255,255), font=font, ) im.draw.text( xy=(box["x0"] * im.original.height / page.height, box["y0"] * im.original.height / page.height), text=str(box["debug_label"]), fill=(0,255,0), font=font, ) debug_show(im) def debug_im(page): return page.to_image(height=config.DEBUG_HEIGHT) def debug_show(im, name=None): im.show() #im.save(f"/tmp/dnd-pdf-to-txt{'' if not name else '-'+name}.jpg") #if not config.DEBUG_NO_SHOW: # go(f"qlmanage -p /tmp/dnd-pdf-to-txt{'' if not name else '-'+name}.jpg &> /dev/null") __subprocesses__ = [] def go(cmd): global __subprocesses__ __subprocesses__.append(subprocess.Popen(cmd, shell=True))