-
Notifications
You must be signed in to change notification settings - Fork 595
/
Copy pathtest_imagebbox.py
49 lines (42 loc) · 1.26 KB
/
test_imagebbox.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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"]