Color-Key Mask #1259
Replies: 3 comments
-
Not 100% sure if this meets your requirements: doc.xref_set_key(xref, "Mask", "[1 1]") Editing across PDF dictionary key hierarchies also works. The following edits a page object such that some image reference name points to a different xref: doc.xref_set_key(page.xref, "Resources/XObject/Im0", "4711 0 R") |
Beta Was this translation helpful? Give feedback.
-
If you insert an image, the resulting xref is returned. You can inspect its details via |
Beta Was this translation helpful? Give feedback.
-
For example: >>> import fitz
>>> doc=fitz.open()
>>> page=doc.new_page()
>>> xref=page.insert_image((100,100,200,200),filename="nur-ruhig.jpg")
>>> print(doc.xref_object(xref))
<<
/Filter /DCTDecode
/Type /XObject
/Subtype /Image
/BitsPerComponent 8
/Width 439
/Height 501
/ColorSpace /DeviceRGB
/Length 50784
>>
>>> for key in doc.xref_get_keys(xref):
print(key, doc.xref_get_key(xref, key))
Filter ('name', '/DCTDecode')
Type ('name', '/XObject')
Subtype ('name', '/Image')
BitsPerComponent ('int', '8')
Width ('int', '439')
Height ('int', '501')
ColorSpace ('name', '/DeviceRGB')
Length ('int', '50784')
>>> |
Beta Was this translation helpful? Give feedback.
-
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:
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 :)
Beta Was this translation helpful? Give feedback.
All reactions