Skip to content

Question / Comment: bbox doesn't exactly match the word #739

@victor-ab

Description

@victor-ab

For some documents (just a few), the bbox doesn't exactly match the word.

doc = fitz.open(pdf_dir)
pages = [ doc[ i ] for i in range( doc.pageCount ) ]
page = pages[0]

blocks = page.getText("dict")['blocks']
text_blocks = [block for block in blocks if block['type']==0]
color='red'
width=2
TARGET_DPI = 300
mat = fitz.Matrix(TARGET_DPI/ 72, TARGET_DPI/ 72)
pix_map = doc.getPagePixmap(0,matrix=mat)
image = Image.open(BytesIO(pix_map.getImageData()))
draw = ImageDraw.Draw(image)

for block in text_blocks:
    for lines in text_blocks:
        for line in lines['lines']:
            for span in line['spans']:
                x0, y0, x1, y1 = span['bbox']
                x0, y0, x1, y1 = x0*TARGET_DPI/ 72, y0*TARGET_DPI/ 72, x1*TARGET_DPI/ 72, y1*TARGET_DPI/ 72
                draw.rectangle((x0, y0, x1, y1), fill=None, outline='green', width=width)

image

If I add y0 = y1-span['size'], then it works:
image
Why does it happens? Is it safer to always use the size?

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions