Skip to content

Pixmap constructor fails #4724

@traits

Description

@traits

Description of the bug

I get exceptions when trying to convert a pixmap into one with new color space. See below.

How to reproduce the bug

      image_list = page.get_images()

      for img_index, img in enumerate(image_list):
        xref = img[0]
        pix = pdf.extract_image(xref)
        ext = pix.get("ext")
        
        ## 3 cases, only the first one is working
        # pix = pix.get("image") # works
        # pix = pymupdf.Pixmap(pymupdf.csRGB, pix) # <---- exception 1
        # pix = pymupdf.Pixmap(pymupdf.csRGB, pix.get("image")) # <---- exception 2

        info = page.get_image_rects(img, transform=True)
        info = [list(info[0][0]), list(info[0][1])]
        image_filename = page_dir / f"img_{page_num}_{img_index}.{ext}"
        info_filename = image_filename.with_suffix(".json")

        try:
          with open(image_filename, "wb") as f:
            f.write(pix)
          with open(info_filename, "w", encoding="utf-8") as f:
            json.dump(info, f, indent=2, ensure_ascii=False)

        except Exception as e:
          print(f"{e}")
        pix = None

throws:

Exception 1:

Exception: Unrecognised args for constructing Pixmap:
    <class 'pymupdf.Colorspace'>: Colorspace(CS_RGB) - DeviceRGB
    <class 'dict'>: {'width': 148, 'height': 107, 'ext': 'jpeg', 'colorspace': 4, 'xres': 96, 'yres': 96, 'bpc': 8, 'size': 14244, 'image': b'\xff\xd8\xff\

Exception 2:

Exception: Unrecognised args for constructing Pixmap:
    <class 'pymupdf.Colorspace'>: Colorspace(CS_RGB) - DeviceRGB
    <class 'bytes'>: b'\xff\xd8\xff\xee....

The provided data format for the pixmap seems to be the culprit. What am I doing wrong?

PyMuPDF version

1.26.4

Operating system

Windows

Python version

3.12

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions