Mercurial > hgrepos > Python2 > PyMuPDF
comparison tests/test_insertimage.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 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 1:1d09e1dec1d9 |
|---|---|
| 1 """ | |
| 2 * Insert same image with different rotations in two places of a page. | |
| 3 * Extract bboxes and transformation matrices | |
| 4 * Assert image locations are inside given rectangles | |
| 5 """ | |
| 6 import json | |
| 7 import os | |
| 8 | |
| 9 import pymupdf | |
| 10 | |
| 11 scriptdir = os.path.abspath(os.path.dirname(__file__)) | |
| 12 imgfile = os.path.join(scriptdir, "resources", "nur-ruhig.jpg") | |
| 13 | |
| 14 | |
| 15 def test_insert(): | |
| 16 doc = pymupdf.open() | |
| 17 page = doc.new_page() | |
| 18 r1 = pymupdf.Rect(50, 50, 100, 100) | |
| 19 r2 = pymupdf.Rect(50, 150, 200, 400) | |
| 20 page.insert_image(r1, filename=imgfile) | |
| 21 page.insert_image(r2, filename=imgfile, rotate=270) | |
| 22 info_list = page.get_image_info() | |
| 23 assert len(info_list) == 2 | |
| 24 bbox1 = pymupdf.Rect(info_list[0]["bbox"]) | |
| 25 bbox2 = pymupdf.Rect(info_list[1]["bbox"]) | |
| 26 assert bbox1 in r1 | |
| 27 assert bbox2 in r2 | |
| 28 | |
| 29 def test_compress(): | |
| 30 document = pymupdf.open(f'{scriptdir}/resources/2.pdf') | |
| 31 document_new = pymupdf.open() | |
| 32 for page in document: | |
| 33 pixmap = page.get_pixmap( | |
| 34 colorspace=pymupdf.csRGB, | |
| 35 dpi=72, | |
| 36 annots=False, | |
| 37 ) | |
| 38 page_new = document_new.new_page(-1) | |
| 39 page_new.insert_image(rect=page_new.bound(), pixmap=pixmap) | |
| 40 document_new.save( | |
| 41 f'{scriptdir}/resources/2.pdf.compress.pdf', | |
| 42 garbage=3, | |
| 43 deflate=True, | |
| 44 deflate_images=True, | |
| 45 deflate_fonts=True, | |
| 46 pretty=True, | |
| 47 ) | |
| 48 | |
| 49 def test_3087(): | |
| 50 path = os.path.abspath(f'{__file__}/../../tests/resources/test_3087.pdf') | |
| 51 | |
| 52 doc = pymupdf.open(path) | |
| 53 page = doc[0] | |
| 54 print(page.get_images()) | |
| 55 base = doc.extract_image(5)["image"] | |
| 56 mask = doc.extract_image(5)["image"] | |
| 57 page = doc.new_page() | |
| 58 page.insert_image(page.rect, stream=base, mask=mask) | |
| 59 | |
| 60 doc = pymupdf.open(path) | |
| 61 page = doc[0] | |
| 62 print(page.get_images()) | |
| 63 base = doc.extract_image(5)["image"] | |
| 64 mask = doc.extract_image(6)["image"] | |
| 65 page = doc.new_page() | |
| 66 page.insert_image(page.rect, stream=base, mask=mask) |
