pdf_image_tools.py 3.88 KB
Newer Older
赵小蒙's avatar
赵小蒙 committed
1

赵小蒙's avatar
赵小蒙 committed
2
from magic_pdf.libs.commons import fitz
赵小蒙's avatar
赵小蒙 committed
3
from loguru import logger
4
from magic_pdf.libs.commons import join_path
5
from magic_pdf.libs.hash_utils import compute_sha256
赵小蒙's avatar
赵小蒙 committed
6
7


8
def cut_image(bbox: tuple, page_num: int, page: fitz.Page, return_path, imageWriter, upload=True):
赵小蒙's avatar
赵小蒙 committed
9
10
11
12
13
    """
    从第page_num页的page中,根据bbox进行裁剪出一张jpg图片,返回图片路径
    save_path:需要同时支持s3和本地, 图片存放在save_path下,文件名是: {page_num}_{bbox[0]}_{bbox[1]}_{bbox[2]}_{bbox[3]}.jpg , bbox内数字取整。
    """
    # 拼接文件名
14
    filename = f"{page_num}_{int(bbox[0])}_{int(bbox[1])}_{int(bbox[2])}_{int(bbox[3])}"
15
16

    # 老版本返回不带bucket的路径
17
    img_path = join_path(return_path, filename) if return_path is not None else None
赵小蒙's avatar
赵小蒙 committed
18

19
20
    # 新版本生成平铺路径
    img_hash256_path = f"{compute_sha256(img_path)}.jpg"
赵小蒙's avatar
赵小蒙 committed
21

22
23
24
25
26
27
28
    if upload:
        # 将坐标转换为fitz.Rect对象
        rect = fitz.Rect(*bbox)
        # 配置缩放倍数为3倍
        zoom = fitz.Matrix(3, 3)
        # 截取图片
        pix = page.get_pixmap(clip=rect, matrix=zoom)
赵小蒙's avatar
赵小蒙 committed
29

30
        byte_data = pix.tobytes(output='jpeg', jpg_quality=95)
31

32
        imageWriter.write(data=byte_data, path=img_hash256_path, mode="binary")
33

34
    return img_hash256_path
赵小蒙's avatar
赵小蒙 committed
35
36


37
38
39
40
def save_images_by_bboxes(page_num: int, page: fitz.Page, pdf_bytes_md5: str,
                          image_bboxes: list, images_overlap_backup: list, table_bboxes: list,
                          equation_inline_bboxes: list,
                          equation_interline_bboxes: list, imageWriter) -> dict:
赵小蒙's avatar
赵小蒙 committed
41
42
43
44
45
46
47
48
49
50
51
    """
    返回一个dict, key为bbox, 值是图片地址
    """
    image_info = []
    image_backup_info = []
    table_info = []
    inline_eq_info = []
    interline_eq_info = []

    # 图片的保存路径组成是这样的: {s3_or_local_path}/{book_name}/{images|tables|equations}/{page_num}_{bbox[0]}_{bbox[1]}_{bbox[2]}_{bbox[3]}.jpg

52
53
    def return_path(type):
        return join_path(pdf_bytes_md5, type)
赵小蒙's avatar
赵小蒙 committed
54
55

    for bbox in image_bboxes:
56
        if any([bbox[0] >= bbox[2], bbox[1] >= bbox[3]]):
赵小蒙's avatar
赵小蒙 committed
57
58
            logger.warning(f"image_bboxes: 错误的box, {bbox}")
            continue
59
60

        image_path = cut_image(bbox, page_num, page, return_path("images"), imageWriter)
赵小蒙's avatar
赵小蒙 committed
61
        image_info.append({"bbox": bbox, "image_path": image_path})
62

赵小蒙's avatar
赵小蒙 committed
63
    for bbox in images_overlap_backup:
64
        if any([bbox[0] >= bbox[2], bbox[1] >= bbox[3]]):
赵小蒙's avatar
赵小蒙 committed
65
66
            logger.warning(f"images_overlap_backup: 错误的box, {bbox}")
            continue
67
        image_path = cut_image(bbox, page_num, page, return_path("images"), imageWriter)
赵小蒙's avatar
赵小蒙 committed
68
69
70
        image_backup_info.append({"bbox": bbox, "image_path": image_path})

    for bbox in table_bboxes:
71
        if any([bbox[0] >= bbox[2], bbox[1] >= bbox[3]]):
赵小蒙's avatar
赵小蒙 committed
72
73
            logger.warning(f"table_bboxes: 错误的box, {bbox}")
            continue
74
        image_path = cut_image(bbox, page_num, page, return_path("tables"), imageWriter)
赵小蒙's avatar
赵小蒙 committed
75
76
77
        table_info.append({"bbox": bbox, "image_path": image_path})

    for bbox in equation_inline_bboxes:
78
        if any([bbox[0] >= bbox[2], bbox[1] >= bbox[3]]):
赵小蒙's avatar
赵小蒙 committed
79
80
            logger.warning(f"equation_inline_bboxes: 错误的box, {bbox}")
            continue
81
82
        image_path = cut_image(bbox[:4], page_num, page, return_path("equations_inline"), imageWriter, upload=False)
        inline_eq_info.append({'bbox': bbox[:4], "image_path": image_path, "latex_text": bbox[4]})
赵小蒙's avatar
赵小蒙 committed
83
84

    for bbox in equation_interline_bboxes:
85
        if any([bbox[0] >= bbox[2], bbox[1] >= bbox[3]]):
赵小蒙's avatar
赵小蒙 committed
86
87
            logger.warning(f"equation_interline_bboxes: 错误的box, {bbox}")
            continue
88
89
        image_path = cut_image(bbox[:4], page_num, page, return_path("equation_interline"), imageWriter, upload=False)
        interline_eq_info.append({"bbox": bbox[:4], "image_path": image_path, "latex_text": bbox[4]})
赵小蒙's avatar
赵小蒙 committed
90

91
    return image_info, image_backup_info, table_info, inline_eq_info, interline_eq_info