Repository navigation
Extract an entire image, without splitting it into smaller ones #5128
Replies: 3 comments
|
As the displayed text on the page suggests: there is no such thing as the "entire image" - it is a cognitive artifact created by the mind of the viewer - i.e. by you. |
|
Moving this |
|
Adding a practical workaround to Jorj's explanation: since the "entire image" is really a mix of several images, vector graphics and text, you can render that region of the page instead of extracting the embedded objects. You get exactly what you see: import pymupdf
doc = pymupdf.open("Externalization.pdf")
page = doc[3] # page 4
# bounding boxes of all images drawn on the page
rects = [pymupdf.Rect(info["bbox"]) for info in page.get_image_info()]
# keep only the pieces of the area you want (here: upper half of the page)
pieces = [r for r in rects if r.y1 < page.rect.height / 2]
area = pymupdf.Rect(pieces[0])
for r in pieces[1:]:
area |= r # union of all pieces
pix = page.get_pixmap(clip=area, dpi=200) # raise dpi for more detail
pix.save("entire-image.png")The result is a single PNG of that region, including the icons, rectangles and the text drawn over it. If you already know the coordinates, you can skip the image boxes and pass |
Uh oh!
There was an error while loading. Please reload this page.
Hello,
To extract images from a PDF, I use the code according to your readme (see Recipes -> Extract all images from a PDF). But instead the entire upper image (see page 4 of the attached file), I receive 15 smaller ones, with various icons and black rectangles being treated as separate images.
In the other attachment, I showed the entire image I would like to extract. Note that the image area has some text layer bboxes. I partially highlighted the text layer to make the situation clear.
Is it possible in PyMuPDF to extract this image entirely and not to add smaller images included in the entire image area to the result? If yes, what code / which options should I use? If not, why?
Externalization.pdf
All reactions