Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import PIL.Image
import PIL.ImageDraw
import wand.image
import sys, os
from io import BytesIO
from pdfplumber import utils
from pdfplumber.table import TableFinder
class COLORS(object):
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
TRANSPARENT = (0, 0, 0, 0)
DEFAULT_FILL = COLORS.BLUE + (50,)
DEFAULT_STROKE = COLORS.RED + (200,)
DEFAULT_STROKE_WIDTH = 1
DEFAULT_RESOLUTION = 72
def get_page_image(pdf_path, page_no, resolution):
"""
For kwargs, see http://docs.wand-py.org/en/latest/wand/image.html#wand.image.Image
"""
page_path = "{0}[{1}]".format(pdf_path, page_no)
with wand.image.Image(filename=page_path, resolution=resolution) as img:
if img.alpha_channel:
img.background_color = wand.image.Color('white')
img.alpha_channel = 'background'
with img.convert("png") as png:
im = PIL.Image.open(BytesIO(png.make_blob()))
if "transparency" in im.info:
converted = im.convert("RGBA").convert("RGB")
bbox = bbox_or_obj
else:
obj = bbox_or_obj
bbox = (obj["x0"], obj["top"], obj["x1"], obj["bottom"])
x0, top, x1, bottom = bbox
half = self.decimalize(stroke_width / 2)
x0 += half
top += half
x1 -= half
bottom -= half
self.draw.rectangle(
self._reproject_bbox((x0, top, x1, bottom)),
fill,
COLORS.TRANSPARENT
)
if stroke_width > 0:
segments = [
((x0, top), (x1, top)), # top
((x0, bottom), (x1, bottom)), # bottom
((x0, top), (x0, bottom)), # left
((x1, top), (x1, bottom)), # right
]
self.draw_lines(
segments,
stroke=stroke,
stroke_width=stroke_width
)
return self
def debug_tablefinder(self, tf={}):
if isinstance(tf, TableFinder):
pass
elif isinstance(tf, dict):
tf = self.page.debug_tablefinder(tf)
else:
raise ValueError("Argument must be instance of TableFinder or a TableFinder settings dict.")
for table in tf.tables:
self.debug_table(table)
self.draw_lines(tf.edges, stroke_width=1)
self.draw_circles(tf.intersections.keys(),
fill=COLORS.TRANSPARENT,
stroke=COLORS.BLUE + (200,),
radius=3)
return self