it confuses me that how we can extract the non-rectangular images #2339
-
pdf_font_garbled.pdf |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Looking closer at the images on page 2, you will see that a number of them has masks, these items have a second entry > 0, e.g. for item in page.get_images():
xref = item[0] # base image xref
mask = item[1] # mask xref
if mask == 0: continue # ignore if no masked image
pix0 = fitz.Pixmap(doc, xref) # pixmap of base image
if pix0.alpha: pix0 = fitz.Pixmap(pix0, 0) # remove alpha channel if present
pixm = fitz.Pixmap(doc, mask) # pixmap of mask
pix = fitz.Pixmap(pix0,pixm) # merge base and mask pixmap to one transparent pixmap
fname=f"{xref}.png" # output filename
pix.save(fname) # save recovered picture |
Beta Was this translation helpful? Give feedback.
-
thanks a lot!!! :> |
Beta Was this translation helpful? Give feedback.
Looking closer at the images on page 2, you will see that a number of them has masks, these items have a second entry > 0, e.g.
(53, 90, 173, 173, 8, 'DeviceRGB', '', 'Image53', 'DCTDecode')
has 90 there. This is an image mask which must be applied to get the full image. The following snippet only extracts images with a mask and recovers the full picture by applying the mask to the base image: