Skip to content

enh: applying redactions with image cropping for currently unsupported colorspaces #4943

@mahlzahn

Description

@mahlzahn

Description of the bug

Applying redactions with image cropping (images=pymupdf.PDF_REDACT_IMAGE_PIXELS) sometimes fail with the message only Gray, RGB, and CMYK colorspaces supported.

/usr/lib/python3.14/site-packages/pymupdf/__init__.py in ?(page, images, graphics, text)
  10766 
  10767         if redact_annots == []:  # any redactions on this page?
  10768             return False  # no redactions
  10769 
> 10770         rc = page._apply_redactions(text, images, graphics)  # call MuPDF
  10771         if not rc:  # should not happen really
  10772             raise ValueError("Error applying redactions.")
  10773 

/usr/lib/python3.14/site-packages/pymupdf/__init__.py in ?(self, text, images, graphics)
   9925         opts.black_boxes = 0  # no black boxes
   9926         opts.text = text  # how to treat text
   9927         opts.image_method = images  # how to treat images
   9928         opts.line_art = graphics  # how to treat vector graphics
-> 9929         success = mupdf.pdf_redact_page(page.doc(), page, opts)
   9930         return success

/usr/lib/python3.14/site-packages/mupdf/__init__.py in ?(doc, page, opts)
  57660 def pdf_redact_page(doc, page, opts):
  57661     r"""Class-aware wrapper for `::pdf_redact_page()`."""
> 57662     return _mupdf.pdf_redact_page(doc, page, opts)

FzErrorArgument: code=4: only Gray, RGB, and CMYK colorspaces supported

The error message is unexpected but clear and workarounds exist, e.g., redacting the image with total removal (images=pymupdf.PDF_REDACT_IMAGE_REMOVE). Thus, feel free to classify it as feature request or close it. Thanks.

How to reproduce the bug

  1. Get sample from: https://github.com/ArtifexSoftware/tests/blob/master/pdf/foo0-ge-64k.pdf
  2. Run following python code snippet:
document = pymupdf.Document("foo0-ge-64k.pdf")
page = document[0]
page.add_redact_annot(page.rect)
page.apply_redactions(images=pymupdf.PDF_REDACT_IMAGE_PIXELS)

PyMuPDF version

1.27.1

Operating system

Linux

Python version

3.14

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions