Replace image in PDF #936
-
Is there a way to replace images in PDF using PyMuPDF? I tried but I was only able to extract the images from the PDF. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 15 replies
-
duplicate of #924. |
Beta Was this translation helpful? Give feedback.
-
The image will always be inserted such that its center and the center of the rectangle coincide. Then the image size is adjusted that its left-right or top-bottom sides coincide with the resp. rect sides. |
Beta Was this translation helpful? Give feedback.
-
The rect area in PyMuPDF is coded as the builtin
>>> fitz.TOOLS.set_small_glyph_heights(True)
True
>>> blks=page.get_text("dict",flags=0)["blocks"]
>>> spans=[]
>>> for b in blks:
for l in b["lines"]:
for s in l["spans"]:
spans.append(fitz.Rect(s["bbox"]))
>>> sum([abs(r) for r in spans]) / abs(page.rect)
0.23933628131017004
>>> So in this example, text should cover almost 24% of the page. In this case, there also were no span overlaps, so a precise result. |
Beta Was this translation helpful? Give feedback.
The rect area in PyMuPDF is coded as the builtin
abs()
function. It gives "square of points". There also are variants for square inches and centimeters, but for relative sizes,abs()
is sufficient.fitz.TOOLS.set_small_glyph_heights(True)
, to minimize the area each character occupies and any inter-span overlaps.span_areas / abs(page.rect)
.