Getting confused with transformations #2363
-
Hi, I have a PDF that contains images, The images here are transformed and only part of these images are displayed. I want to develop a viewer that when I double click on any of the sub images shows the underlying image, and marks the section that's displayed on the PDF page. I have already managed to launch the correct image in a viewer class, that looks like this, My ImageViewer2 looks like this, `class ImageViewer2(Gtk.Window):
However, I can't manage to display the rectangle that should mark the part of the image that's displayed in the original PDF. My thought process is as follows,
Hoping I'm missing something small here and someone can help. Thanks in advance, |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
I'm sure I am missing something here. Still the following suggestion:
Does that help? BTW, you can also do this with the image instead of making a pixmap for cairo: imgdoc = fitz.open(doc.extract_image(xref)["image"]) # open image as MuPDF document (also possible!)
pdfbytes = imgdoc.convert_to_pdf() # convert this to a PDF
imgpdf = fitz.open("pdf", pdfbytes) # the PDF document version of the image
imgpage = imgpdf[0] # document page corresponding to the image
matrix = bbox.torect(imgpage.rect) # transformation matrix bbox -> imgage page
# take a sub rectangle of the image's bbox on original page,
# and draw its location on the image full page
imgpage.draw_rect(subrect * matrix, color=fitz.pdfcolor["red"])
imgpdf.save(...)
# alternatively make a pixmap from that page again and save as PNG / JPEG |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Sure you need the image's bbox part on the page, that is actually visible. There is no way to find that sub-rectangle by looking at the image properties. |
Beta Was this translation helpful? Give feedback.
Sure you need the image's bbox part on the page, that is actually visible. There is no way to find that sub-rectangle by looking at the image properties.
The best you can have is
bbox & page.rect
: I noticed that some of these images are not fully contained inpage.rect
- their bboxes have negative coordinate values.But whether other stuff on the page is partly covering cannot be determined that way. Also note that this coverage need not at all leave behind a visible sub-rectangle of bbox in the general case: it could just be a corner or a hole inside the bbox and what not. So just assuming the visible part of any image is a rectangle will lead to nowhere.
There is
page.get_bboxlog()
whic…