Skip to content

Commit

Permalink
Added tests/test_insertimage.py:test_compress() and fix in rebased.
Browse files Browse the repository at this point in the history
src/__init__.py: fixed _insert_image() in do_process_pixmap. Also made `log()`
show file:line:function().
  • Loading branch information
julian-smith-artifex-com committed Sep 26, 2023
1 parent 9d28789 commit a74fc9d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@
import zipfile


def log( text):
print( f'mupdfpy: {text}', file=sys.stdout)
def log( text, caller=1):
import inspect
frame_record = inspect.stack( context=0)[ caller]
filename = frame_record.filename
line = frame_record.lineno
function = frame_record.function
print( f'{filename}:{line}:{function}: {text}', file=sys.stdout)
sys.stdout.flush()

# Try to detect if we are being used with current directory set to a mupdfpy/
Expand Down Expand Up @@ -7524,8 +7529,8 @@ def _insert_image(self,
#log( 'do_process_pixmap')
# process pixmap ---------------------------------
arg_pix = pixmap.this
w = arg_pix.w
h = arg_pix.h
w = arg_pix.w()
h = arg_pix.h()
digest = mupdf.fz_md5_pixmap2(arg_pix)
md5_py = digest
temp = digests.get(md5_py, None)
Expand All @@ -7538,7 +7543,7 @@ def _insert_image(self,
do_have_image = 0
else:
if arg_pix.alpha() == 0:
image = mupdf.fz_new_image_from_pixmap(arg_pix, mupdf.FzImage(0))
image = mupdf.fz_new_image_from_pixmap(arg_pix, mupdf.FzImage())
else:
pm = mupdf.fz_convert_pixmap(
arg_pix,
Expand Down
20 changes: 20 additions & 0 deletions tests/test_insertimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,23 @@ def test_insert():
bbox2 = fitz.Rect(info_list[1]["bbox"])
assert bbox1 in r1
assert bbox2 in r2

def test_compress():
document = fitz.open(f'{scriptdir}/resources/2.pdf')
document_new = fitz.open()
for page in document:
pixmap = page.get_pixmap(
colorspace=fitz.csRGB,
dpi=72,
annots=False,
)
page_new = document_new.new_page(-1)
page_new.insert_image(rect=page_new.bound(), pixmap=pixmap)
document_new.save(
f'{scriptdir}/resources/2.pdf.compress.pdf',
garbage=3,
deflate=True,
deflate_images=True,
deflate_fonts=True,
pretty=True,
)

0 comments on commit a74fc9d

Please sign in to comment.