diff tests/test_imagebbox.py @ 1:1d09e1dec1d9 upstream

ADD: PyMuPDF v1.26.4: the original sdist. It does not yet contain MuPDF. This normally will be downloaded when building PyMuPDF.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:37:51 +0200
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test_imagebbox.py	Mon Sep 15 11:37:51 2025 +0200
@@ -0,0 +1,49 @@
+"""
+Ensure equality of bboxes computed via
+* page.get_image_bbox()
+* page.get_image_info()
+* page.get_bboxlog()
+
+"""
+import os
+
+import pymupdf
+
+scriptdir = os.path.abspath(os.path.dirname(__file__))
+filename = os.path.join(scriptdir, "resources", "image-file1.pdf")
+image = os.path.join(scriptdir, "resources", "img-transparent.png")
+doc = pymupdf.open(filename)
+
+
+def test_image_bbox():
+    page = doc[0]
+    imglist = page.get_images(True)
+    bbox_list = []
+    for item in imglist:
+        bbox_list.append(page.get_image_bbox(item, transform=False))
+    infos = page.get_image_info(xrefs=True)
+    match = False
+    for im in infos:
+        bbox1 = im["bbox"]
+        match = False
+        for bbox2 in bbox_list:
+            abs_bbox = (bbox2 - bbox1).norm()
+            if abs_bbox < 1e-4:
+                match = True
+                break
+    assert match
+
+
+def test_bboxlog():
+    doc = pymupdf.open()
+    page = doc.new_page()
+    xref = page.insert_image(page.rect, filename=image)
+    img_info = page.get_image_info(xrefs=True)
+    assert len(img_info) == 1
+    info = img_info[0]
+    assert info["xref"] == xref
+    bbox_log = page.get_bboxlog()
+    assert len(bbox_log) == 1
+    box_type, bbox = bbox_log[0]
+    assert box_type == "fill-image"
+    assert bbox == info["bbox"]