Python截取图片(fitz)

__author__ = 'Jeff.xie'
import fitz


sourcefile="D:\\Project\\xxx.pdf";

# inst是fitz.Rect包含找到的文本片段的左上角和右下角坐标的对象。所有信息都可以在docs中找到。
# 我设法使用下面的代码片段突出显示点并保存裁剪区域。我正在使用python 3.7.1,我的输出fitz.version是('1.14.13', '1.14.0', '20190407064320')。

doc = fitz.open(sourcefile)
inst_counter = 0
for pi in range(doc.pageCount):
    page = doc[pi]

    # text = "FT21106F7PGQ"
    text = "FT21097G4TGP"
    text_instances = page.searchFor(text)

    # 数字越小,截取的高度越小
    five_percent_height = (page.rect.br.y - page.rect.tl.y)*0.02

    for inst in text_instances:
        inst_counter += 1
        highlight = page.addHighlightAnnot(inst)

        # define a suitable cropping box which spans the whole page
        # and adds padding around the highlighted text
        tl_pt = fitz.Point(page.rect.tl.x, max(page.rect.tl.y, inst.tl.y - five_percent_height))
        br_pt = fitz.Point(page.rect.br.x, min(page.rect.br.y, inst.br.y + five_percent_height))
        hl_clip = fitz.Rect(tl_pt, br_pt)

        zoom_mat = fitz.Matrix(4, 4)   #数字越大,截取图片的清晰度越高
        pix = page.getPixmap(matrix=zoom_mat, clip = hl_clip)
        # pix.writePNG(f"pg{pi}-hl{inst_counter}.png")
        pix.writePNG(r"D:\\shot55.png")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值