Skip to content

Commit a74fc9d

Browse files
Added tests/test_insertimage.py:test_compress() and fix in rebased.
src/__init__.py: fixed _insert_image() in do_process_pixmap. Also made `log()` show file:line:function().
1 parent 9d28789 commit a74fc9d

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

src/__init__.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@
1414
import zipfile
1515

1616

17-
def log( text):
18-
print( f'mupdfpy: {text}', file=sys.stdout)
17+
def log( text, caller=1):
18+
import inspect
19+
frame_record = inspect.stack( context=0)[ caller]
20+
filename = frame_record.filename
21+
line = frame_record.lineno
22+
function = frame_record.function
23+
print( f'{filename}:{line}:{function}: {text}', file=sys.stdout)
1924
sys.stdout.flush()
2025

2126
# Try to detect if we are being used with current directory set to a mupdfpy/
@@ -7524,8 +7529,8 @@ def _insert_image(self,
75247529
#log( 'do_process_pixmap')
75257530
# process pixmap ---------------------------------
75267531
arg_pix = pixmap.this
7527-
w = arg_pix.w
7528-
h = arg_pix.h
7532+
w = arg_pix.w()
7533+
h = arg_pix.h()
75297534
digest = mupdf.fz_md5_pixmap2(arg_pix)
75307535
md5_py = digest
75317536
temp = digests.get(md5_py, None)
@@ -7538,7 +7543,7 @@ def _insert_image(self,
75387543
do_have_image = 0
75397544
else:
75407545
if arg_pix.alpha() == 0:
7541-
image = mupdf.fz_new_image_from_pixmap(arg_pix, mupdf.FzImage(0))
7546+
image = mupdf.fz_new_image_from_pixmap(arg_pix, mupdf.FzImage())
75427547
else:
75437548
pm = mupdf.fz_convert_pixmap(
75447549
arg_pix,

tests/test_insertimage.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,23 @@ def test_insert():
2525
bbox2 = fitz.Rect(info_list[1]["bbox"])
2626
assert bbox1 in r1
2727
assert bbox2 in r2
28+
29+
def test_compress():
30+
document = fitz.open(f'{scriptdir}/resources/2.pdf')
31+
document_new = fitz.open()
32+
for page in document:
33+
pixmap = page.get_pixmap(
34+
colorspace=fitz.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+
)

0 commit comments

Comments
 (0)