4

Am trying to see if I can identify possible table headers in a table inside PDF using background and foreground color of the text. With PyMuPDF text extraction, I was able to get the foreground color. Wondering if there is a way to get background color too.

Am using pymupdf 1.16.2 with python 3.7 I have checked the documentation but could find only one color field, which is associated with Text-color not background-color

if anyone knows how to get the background color using pyMuPDF or may be some other package, please let me know

Suvin K S
  • 229
  • 2
  • 8

1 Answers1

6

I needed a similar function but couldn't find it in PyMuPDF, so I write a function to get the color of the pixel in the top-left bbox containing the text.

def getText2(page: fitz.Page, zoom_f=3) -> dict:
    """
    Function similar to fitz.Page.getText("dict"). But the returned dict
    also contains a key "bg_color" with color tuple as value for each block in "blocks".
    """
    # Retrieves the content of the page
    all_words = page.getText("dict")

    # Transform page into PIL.Image
    mat = fitz.Matrix(zoom_f, zoom_f)
    pixmap = page.getPixmap(mat)
    img = Image.open(io.BytesIO(pixmap.getPNGData()))
    img_border = fitz.Rect(0, 0, img.width, img.height)
    for block in all_words['blocks']:
        # Retrieve only text block (type 0)
        if block['type'] == 0:
            rect = fitz.Rect(*tuple(xy * zoom_f for xy in block['bbox']))
            if img_border.contains(rect):
                color = img.getpixel((rect.x0, rect.y0))
                block['bg_color'] = tuple(c/255 for c in color)
    return all_words
Yohann L.
  • 1,262
  • 13
  • 27