Skip to content

Exception in insert_image with mask specified #3087

Closed
@cmyers009

Description

@cmyers009

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugfix developedrelease schedule to be determined

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions