Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception in insert_image with mask specified #3087

Closed
cmyers009 opened this issue Jan 24, 2024 · 5 comments
Closed

Exception in insert_image with mask specified #3087

cmyers009 opened this issue Jan 24, 2024 · 5 comments
Labels
bug fix developed release schedule to be determined

Comments

@cmyers009
Copy link

Description of the bug

This is the code block that produces the error:

create new page

width, height = page.mediabox_size
new_page = new_pdf.new_page(width=width, height=height)

get bounding box info and extract the position of the images

page_bounding_box_info = doc[index].get_bboxlog()

image_bounding_box_info = []
for bbx in page_bounding_box_info:
if(bbx[0] == 'fill-image'):
image_bounding_box_info.append(bbx[1])

insert these images into the new page

for index, bbx in enumerate(image_bounding_box_info):
positional_info = bbx

# Ensure positional_info has four elements
if len(positional_info) == 4:
    xref = img_list[index][0]
    base_image = doc.extract_image(xref)
    image_bytes = base_image["image"]
    smask_xref = base_image.get('smask')

    if smask_xref:
        # place smask in pdf

        # make image reference

        # Handling images with SMask
        smask_img = doc.extract_image(smask_xref)["image"]
        smask_pil = io.BytesIO(smask_img)
        print(smask_xref)
        print(smask_pil)
        new_image_xref = new_page.insert_image(positional_info, stream=image_bytes, mask=smask_pil)
    else:
        new_image_xref = new_page.insert_image(positional_info, stream=image_bytes)

Error happens on this line:

new_page.insert_image(positional_info, stream=image_bytes, mask=smask_pil)

Error happens in PyMuPDF/src/init.py
zimg = mupdf.fz_new_image_from_compressed_buffer(
w, h,
bpc, colorspace, xres, yres, 1, 0, None,
None, cbuf1, mask
)

How to reproduce the bug

Traceback (most recent call last): File "C:\Users\{user}\Desktop\{my_file}.py", line 375, in extract_image_from_pdf new_image_xref = new_page.insert_image(positional_info, stream=image_bytes, mask=smask_pil) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\{user}\AppData\Local\Programs\Python\Python311\Lib\site-packages\fitz\utils.py", line 364, in insert_image xref, digests = page._insert_image( ^^^^^^^^^^^^^^^^^^^ File "C:\Users\{user}\AppData\Local\Programs\Python\Python311\Lib\site-packages\fitz\__init__.py", line 7823, in _insert_image zimg = mupdf.fz_new_image_from_compressed_buffer( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: fz_new_image_from_compressed_buffer_outparams_fn() takes 10 positional arguments but 12 were given Failed to extract image(s) from page, ran into error fz_new_image_from_compressed_buffer_outparams_fn() takes 10 positional arguments but 12 were given

PyMuPDF version

1.23.15

Operating system

Windows

Python version

3.11

@JorjMcKie JorjMcKie changed the title File "C:\Users\{user}\AppData\Local\Programs\Python\Python311\Lib\site-packages\fitz\__init__.py", line 7823, in _insert_image zimg = mupdf.fz_new_image_from_compressed_buffer( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: fz_new_image_from_compressed_buffer_outparams_fn() takes 10 positional arguments but 12 were given Failed to extract image(s) from page, ran into error fz_new_image_from_compressed_buffer_outparams_fn() takes 10 positional arguments but 12 were given` Exception in insert_image with mask specified Jan 24, 2024
@JorjMcKie
Copy link
Collaborator

I have changed the unusable title of this post to something more meaningful.
Otherwise, let me check.
You are aware, that you do not need to use the mask parameter? You can pass a pixmap with alpha channel without problems.

@JorjMcKie JorjMcKie added the bug label Jan 24, 2024
@JorjMcKie
Copy link
Collaborator

Confirming the problem.
We will check it out now.

@cmyers009
Copy link
Author

@JorjMcKie I am trying to not use pixmap if possible, because it seems to increase file size, and reduce image quality for raster images.

Am I incorrect about this?

@JorjMcKie
Copy link
Collaborator

JorjMcKie commented Jan 24, 2024

Larger file sizes can be coped with by using proper save() arguments: deflate=True,garbage=3 or, preferably, doc.ez_save().
Quality reduction going over the pixmap may happen because internal conversion to PNG is done - inevitably. However, depending on the colorspace, this may have an overall beneficial effect when b/w images are stake: then the generated mask uses the best-in-class compression method CCITTFaxDecode.

julian-smith-artifex-com added a commit that referenced this issue Jan 25, 2024
src/__init__.py:
    Page._insert_image():
        avoid problems caused by mupdf.fz_new_image_from_compressed_buffer()
        being unusable from Python, due to decode and colorkey args being
        incorrectly treated as out-params.

        Also avoid double free of fz_compressed_buffer due to not being
        ref-counted, but used by two images. The fix follows classic, and
        probably results in a small memory leak.

src/extra.i:
    Added fz_new_image_from_compressed_buffer(), wrapper for
    mupdf::fz_new_image_from_compressed_buffer(), without problematic decode
    and colorkey args.

tests/test_insertimage.py:
    Added test_3087().

tests/resources/test_3087.pdf:
    New, for use by test_3087().
@julian-smith-artifex-com julian-smith-artifex-com added the fix developed release schedule to be determined label Jan 25, 2024
julian-smith-artifex-com added a commit that referenced this issue Jan 25, 2024
src/__init__.py:
    Page._insert_image():
        avoid problems caused by mupdf.fz_new_image_from_compressed_buffer()
        being unusable from Python, due to decode and colorkey args being
        incorrectly treated as out-params.

        Also avoid double free of fz_compressed_buffer due to not being
        ref-counted, but used by two images. The fix follows classic, and
        probably results in a small memory leak.

src/extra.i:
    Added fz_new_image_from_compressed_buffer(), wrapper for
    mupdf::fz_new_image_from_compressed_buffer(), without problematic decode
    and colorkey args.

tests/test_insertimage.py:
    Added test_3087().

tests/resources/test_3087.pdf:
    New, for use by test_3087().
julian-smith-artifex-com added a commit that referenced this issue Jan 25, 2024
src/__init__.py:
    Page._insert_image():
        avoid problems caused by mupdf.fz_new_image_from_compressed_buffer()
        being unusable from Python, due to decode and colorkey args being
        incorrectly treated as out-params.

        Also avoid double free of fz_compressed_buffer due to not being
        ref-counted, but used by two images. The fix follows classic, and
        probably results in a small memory leak.

src/extra.i:
    Added fz_new_image_from_compressed_buffer(), wrapper for
    mupdf::fz_new_image_from_compressed_buffer(), without problematic decode
    and colorkey args.

tests/test_insertimage.py:
    Added test_3087().

tests/resources/test_3087.pdf:
    New, for use by test_3087().
@julian-smith-artifex-com
Copy link
Collaborator

Fixed in 1.23.19.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug fix developed release schedule to be determined
Projects
None yet
Development

No branches or pull requests

3 participants