Skip to content

Color-Key Mask #1243

@jasonkylejacobs

Description

@jasonkylejacobs

I'm currently trying to apply a simple color-key mask to a foreground JBIG2 image, so that I can make any white pixel transparent, however I can't seem to find a way to do this using PyMuPDF. I can achieve the desired result by creating a separate image mask, however this is a little overkill for what I need.

As a proof of concept for myself, these are the steps I've taken:

  1. Create the PDF using a background and foreground image
doc = fitz.open()
page = doc.newPage()
bg_stream = open("wtr_0009_bg.jpg", 'rb').read()
fg_stream = open("wtr_0009_fg.jb2", 'rb').read()
page.insertImage(page.rect, stream=bg_stream, mask=None)
page.insertImage(page.rect, stream=fg_stream, mask=None)
doc.save("mask_test.pdf", deflate=True)
  1. Edit the PDF using a text editor, to manually insert the color-key mask:
<<
/Filter 				/JBIG2Decode
/Type 					/XObject
/Subtype 				/Image
/BitsPerComponent 		1
/Width 					3421
/Height 				4904
/ColorSpace 			/DeviceGray
/Mask  					[1 1]
/Length 				6339
>>

This successfully makes all the white pixels in the foreground image transparent, the only downside is that the background image ends up distorted, although I'm assuming this is due to me editing the pdf with a text editor.

Is it currently possible to create the color-key mask directly using PyMuPDF?

If not, is this something that could be considered for a future release?
It would be great to be able to do something like:
page.insertImage(page.rect, stream=fg_stream, mask=[1, 1])

Thanks :)

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