Skip to content

flipping coordinates was removed for pymupdf #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/openparse/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,9 +590,12 @@ def reading_order(self) -> ReadingOrder:

if self.coordinate_system == "bottom-left":
y_position = -min(element.bbox.y0 for element in self.elements)
# Add support for top-left coordinate system for sorting
elif self.coordinate_system == "top-left":
y_position = min(element.bbox.y0 for element in self.elements)
else:
raise NotImplementedError(
"Only 'bottom-left' coordinate system is supported."
"Only 'top-left' and 'bottom-left' coordinate system is supported."
)

return ReadingOrder(min_page=min_page, y_position=y_position, min_x0=min_x0)
Expand Down
37 changes: 20 additions & 17 deletions src/openparse/tables/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,19 @@ def _ingest_with_pymupdf(
if verbose:
print(f"Page {page_num} - Table {i + 1}:\n{text}\n")

# Flip y-coordinates to match the top-left origin system
bbox = pymupdf.combine_header_and_table_bboxes(tab.bbox, tab.header.bbox)
fy0 = page.rect.height - bbox[3]
fy1 = page.rect.height - bbox[1]
# No need for flipping coordinates, pymupdf already returns coordinates in top-left origin system and bottom-left is handled while sorting
# # Flip y-coordinates to match the top-left origin system
# fy0 = page.rect.height - bbox[3]
# fy1 = page.rect.height - bbox[1]

table = TableElement(
bbox=Bbox(
page=page_num,
x0=bbox[0],
y0=fy0,
y0=bbox[1],
x1=bbox[2],
y1=fy1,
y1=bbox[3],
page_width=page.rect.width,
page_height=page.rect.height,
),
Expand Down Expand Up @@ -131,18 +132,19 @@ def _ingest_with_table_transformers(
elif args.table_output_format == "html":
table_text = table.to_html_str()

# Flip y-coordinates to match the top-left origin system
# FIXME: incorporate padding into bbox
fy0 = page.rect.height - table_bbox.bbox[3]
fy1 = page.rect.height - table_bbox.bbox[1]
# No need for flipping coordinates, pymupdf already returns coordinates in top-left origin system and bottom-left is handled while sorting
# # Flip y-coordinates to match the top-left origin system
# # FIXME: incorporate padding into bbox
# fy0 = page.rect.height - table_bbox.bbox[3]
# fy1 = page.rect.height - table_bbox.bbox[1]

table_elem = TableElement(
bbox=Bbox(
page=page_num,
x0=table_bbox.bbox[0],
y0=fy0,
y0=table_bbox.bbox[1],
x1=table_bbox.bbox[2],
y1=fy1,
y1=table_bbox.bbox[3],
page_width=page.rect.width,
page_height=page.rect.height,
),
Expand Down Expand Up @@ -193,18 +195,19 @@ def _ingest_with_unitable(
table_img = crop_img_with_padding(pdf_as_imgs[page_num], padded_bbox)

table_str = table_img_to_html(table_img)

# Flip y-coordinates to match the top-left origin system
fy0 = page.rect.height - padded_bbox[3]
fy1 = page.rect.height - padded_bbox[1]

# No need for flipping coordinates, pymupdf already returns coordinates in top-left origin system and bottom-left is handled while sorting
# # Flip y-coordinates to match the top-left origin system
# fy0 = page.rect.height - padded_bbox[3]
# fy1 = page.rect.height - padded_bbox[1]

table_elem = TableElement(
bbox=Bbox(
page=page_num,
x0=padded_bbox[0],
y0=fy0,
y0=padded_bbox[1],
x1=padded_bbox[2],
y1=fy1,
y1=padded_bbox[3],
page_width=page.rect.width,
page_height=page.rect.height,
),
Expand Down
11 changes: 6 additions & 5 deletions src/openparse/text/pymupdf/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,18 @@ def ingest(

lines = _lines_from_ocr_output(node["lines"])

# Flip y-coordinates to match the top-left origin system
fy0 = page.rect.height - node["bbox"][3]
fy1 = page.rect.height - node["bbox"][1]
# No need for flipping coordinates, pymupdf already returns coordinates in top-left origin system and bottom-left is handled while sorting
# # Flip y-coordinates to match the top-left origin system
# fy0 = page.rect.height - node["bbox"][3]
# fy1 = page.rect.height - node["bbox"][1]

elements.append(
TextElement(
bbox=Bbox(
x0=node["bbox"][0],
y0=fy0,
y0=node["bbox"][1],
x1=node["bbox"][2],
y1=fy1,
y1=node["bbox"][3],
page=page_num,
page_width=page.rect.width,
page_height=page.rect.height,
Expand Down