-
Notifications
You must be signed in to change notification settings - Fork 678
Description
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:
- 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)
- 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 :)