PyMuPDF icon indicating copy to clipboard operation
PyMuPDF copied to clipboard

Question : Convert from raw pdf coordinate system to pymu's coordinate system

Open joctaTorres opened this issue 6 years ago • 4 comments

Hello :)

I got a pdf page with some table lines.

I am parsing this information by doing:

page.cleanContents()
content = page.readContents().decode()

And then detecting the m (moveto) and l (lineto) coordinates:

    [...]
    716 -1446 m
    1419 -1446 l
    S
    716 -1446 m
    716 -1553 l
    S
    1419 -1446 m
    2044 -1446 l
    S
    1419 -1446 m
    1419 -1553 l
    S
    [...]

I am parsing the coordinate values like this:

Point(
   move_x, # "left"
   page.metadata["height"] - move_y, # "top"
)

This works fine for some pdfs. But, as you can imagine, for this example the raw x y coordinates on the moveto and lineto don't match pymu's.

E.g: A Point object coordinates looks like this: Point(left=716.0, top=2288.0) while some text blocks from pymu have drastically different coordinate values (even though the point/line and the text block are nearby):

(Pdb) text_block.text
['BANCO']
(Pdb) text_block.position
Position(left=87.23999786376953, top=173.23211669921875, right=122.10000610351562, bottom=186.51876831054688)

How can I parse the raw value from the pdf to pymu's internal coordinate system?

Thx you for the support! ;)

joctaTorres avatar Oct 15 '20 15:10 joctaTorres

You must convert the PDF coordinate system to the one used in (Py-) MuPDF. You can use page.transformationMatrix to do this. Both multiplications, point * matrix and rect * matrix work.

But I have good news for you: You now longer need to parse all this yourself: in v1.18.0 we have a new method page.getDrawings(), which delivers all that stuff as a list of dictionaries! Each of these dictionaries (which each represent a path of the page) contains the complete information (see here) to e.g. reconstruct the drawing via class Shape methods or whatever.

JorjMcKie avatar Oct 15 '20 16:10 JorjMcKie

Example: Take this PDF draws.pdf and then do this:

>>> import fitz
>>> doc=fitz.open("draws.pdf")
>>> page=doc[0]
>>> from pprint import pprint
>>> for path in page.getDrawings():
	pprint(path)

	
{'closePath': False,
 'color': [0.0],
 'dashes': '[] 0',
 'even_odd': False,
 'fill': [1.0, 0.0, 0.0],
 'items': [('re', Rect(100.0, 100.0, 300.0, 250.0))],
 'lineCap': (0, 0, 0),
 'lineJoin': 0,
 'opacity': 1.0,
 'rect': Rect(100.0, 100.0, 300.0, 250.0),
 'width': 1.0}
{'closePath': True,
 'color': None,
 'dashes': '[] 0',
 'even_odd': False,
 'fill': [0.0, 0.0, 1.0],
 'items': [('c',
            Point(200.0, 250.0),
            Point(200.0, 305.22900390625),
            Point(244.77200317382812, 350.0),
            Point(300.0, 350.0)),
           ('c',
            Point(300.0, 350.0),
            Point(355.2279968261719, 350.0),
            Point(400.0, 305.22900390625),
            Point(400.0, 250.0)),
           ('c',
            Point(400.0, 250.0),
            Point(400.0, 194.77197265625),
            Point(355.2279968261719, 150.0),
            Point(300.0, 150.0)),
           ('c',
            Point(300.0, 150.0),
            Point(244.77200317382812, 150.0),
            Point(200.0, 194.77197265625),
            Point(200.0, 250.0))],
 'lineCap': (0, 0, 0),
 'lineJoin': 0,
 'opacity': 0.5,
 'rect': Rect(200.0, 150.0, 400.0, 350.0),
 'width': 1.0}
>>> 

JorjMcKie avatar Oct 15 '20 17:10 JorjMcKie

The big advantage of that new method is that it directly uses the parsing mechanism of MuPDF to arrive at all those coordinates. MuPDF should know best what to do with coordinate changes introduced by pesky matrices that change scaling up or down and which can also be nested by different levels of these stacking / unstacking commands "q" / "Q".

JorjMcKie avatar Oct 15 '20 17:10 JorjMcKie

@JorjMcKie Thank you for the support! I was able to solve this by updating to version 1.18 and parsing the coordinates.

joctaTorres avatar Oct 16 '20 18:10 joctaTorres