49 lines
1.6 KiB
Python
49 lines
1.6 KiB
Python
from itertools import chain, starmap
|
|
from operator import itemgetter
|
|
|
|
import fitz
|
|
from funcy import rcompose
|
|
|
|
from image_prediction.image_extractor.extractor import ImageExtractor, ImageMetadataPair
|
|
|
|
|
|
class ParsablePDFImageExtractor(ImageExtractor):
|
|
def __init__(self):
|
|
self.doc: fitz.fitz.Document = None
|
|
|
|
def __process_images_on_page(self, page: fitz.fitz.Page):
|
|
def load_image_from_xref(xref):
|
|
return self.doc.extract_image(xref)["image"]
|
|
|
|
def format_metadata(image_info):
|
|
x1, y1, x2, y2 = map(rounder, image_info["bbox"])
|
|
width, height = itemgetter("width", "height")(image_info)
|
|
return {
|
|
"page_width": page_width,
|
|
"page_height": page_height,
|
|
"page_idx": page.number,
|
|
"width": width,
|
|
"height": height,
|
|
"x1": x1,
|
|
"x2": x2,
|
|
"y1": y1,
|
|
"y2": y2
|
|
}
|
|
|
|
rounder = rcompose(round, int)
|
|
|
|
page_width, page_height = map(rounder, page.mediabox_size)
|
|
|
|
image_handles = page.get_images(full=True)
|
|
xrefs = map(itemgetter(0), image_handles)
|
|
images = map(load_image_from_xref, xrefs)
|
|
image_infos = page.get_image_info()
|
|
metadata = map(format_metadata, image_infos)
|
|
|
|
return starmap(ImageMetadataPair, zip(images, metadata))
|
|
|
|
def extract(self, pdf: bytes):
|
|
self.doc = fitz.Document(stream=pdf)
|
|
image_metadata_pairs = chain(*map(self.__process_images_on_page, self.doc))
|
|
return image_metadata_pairs
|