Replacing images - refresh issue #2150
-
I have a GUI tool based on GTK 3.0, where I load a PDF page in the UI, I want to allow users to drag and drop images from a different panel onto a particular image on the PDF displayed, and we will replace the image that is underlying in the pdf page. So here is what I do
The idea of
However, I can't get the correct image. if I save this image to disk, using pil_save, I end up getting only the original. I am not sure what I need to get the correct image? Also if I save the full pdf using ez_save, I can see that the correct image is replaced as expected. Wondering what am I missing when it comes to refreshing the image on the GUI? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
And this is the definition of get_img_from_pdf_page
|
Beta Was this translation helpful? Give feedback.
-
Hi, a few comments. You mentioned that the new image seems to be successfully inserted - because the PDF looks correct when saved. So we only need to focus on refreshing the page in such a way that your GUI takes note of it.
For debugging purposes you can also store that image to disk to confirm the page looks alright with the new image inserted, via You already are controlling how I don't understand your second post about |
Beta Was this translation helpful? Give feedback.
-
Ah, interesting: I found the reason I think. import fitz
doc = fitz.open("sample3.pdf")
page = doc[0]
pix = page.get_pixmap()
pix.save("0before.png")
page.replace_image(11, filename="newimg.png")
fitz.Tools().store_shrink(100) # empty 100% of the cache
pix = page.get_pixmap()
pix.save("1after.png") |
Beta Was this translation helpful? Give feedback.
Ah, interesting: I found the reason I think.
MuPDF caches images as much as it can and in the course of this remembers page pixmaps. So to force making a new pixmap, the cache must be emptied.
Then MuPDF will actually re-render the page. The following snippet produces correct before and after pixmaps: