Skip to content

Replace image in PDF #936

Mar 6, 2021 · 3 comments · 15 replies
Discussion options

You must be logged in to vote

code snippet to guide me?

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.

  1. set fitz.TOOLS.set_small_glyph_heights(True), to minimize the area each character occupies and any inter-span overlaps.
  2. iterate over the text span bboxes and add there areas
  3. then: span_areas / abs(page.rect).
>>> 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

Replies: 3 comments 15 replies

Comment options

You must be logged in to vote
9 replies
@redkad
Comment options

@redkad
Comment options

@redkad
Comment options

@JorjMcKie
Comment options

@JorjMcKie
Comment options

Comment options

You must be logged in to vote
5 replies
@redkad
Comment options

@JorjMcKie
Comment options

@redkad
Comment options

@JorjMcKie
Comment options

@redkad
Comment options

Comment options

You must be logged in to vote
1 reply
@redkad
Comment options

Answer selected by JorjMcKie
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #935 on March 06, 2021 20:37.