-
import fitz
doc = fitz.Document("example.pdf")
page = doc[0] # take page 1
modis_xref = page.get_images()[0][0]
page.delete_image(modis_xref) # it's working but it does not delete images on selected page, it does random, sometimes it delete the right image on every page.
# so I cant working with my seleceted page.
doc.save("exampe_out.pdf")
doc.close()C:\Users\user\Desktop\istakademisonuc\example.pdf it does but my document has 4 same image on every 4 pages, I want to delete images that on page 1 but it does every page or random pages. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
This behaviour is documented and works as designed. The documentation mentions that you should use redaction annotations if you want to delete an image just on one page. This approach will however delete everything else that happens to exist within the image's bbox (text and hyperlinks). |
Beta Was this translation helpful? Give feedback.
-
The following code snippet removes the red top-left image on the first page only: import fitz
doc=fitz.open("example.pdf")
page=doc[0]
from pprint import pprint
pprint(page.get_images())
[(17, 0, 137, 55, 8, 'DeviceRGB', '', 'Im0', 'DCTDecode'),
(18, 0, 405, 411, 8, 'DeviceRGB', '', 'Im1', 'DCTDecode')]
# check locations of the 2 images:
rects=page.get_image_rects(17)
rects # this one is top-right
[Rect(492.7799987792969, 15.1199951171875, 580.3200073242188, 87.1199951171875)]
rects2=page.get_image_rects(18)
rects2 # this one is top-left:
[Rect(27.0, 16.55999755859375, 99.0, 88.55999755859375)]
page.add_redact_annot(rects2[0]) # cover top-left image with aredaction
'Redact' annotation on page 0 of example.pdf
# remove the image
page.apply_redactions(images=fitz.PDF_REDACT_IMAGE_REMOVE)
True
# save with garbage option that also physically removes old image
doc.ez_save("example-0-removed.pdf") |
Beta Was this translation helpful? Give feedback.
The following code snippet removes the red top-left image on the first page only: