I want a function for delete image from Document.Page #874
Answered
by
JorjMcKie
shredderzwj
asked this question in
Looking for help
-
Beta Was this translation helpful? Give feedback.
Answered by
JorjMcKie
Feb 1, 2021
Replies: 1 comment
-
There already exists a GUI script that lets you delete images: look at the repo home page. But this is actually not what you want apparently: you want to remove watermarks! I don't intend to make a general function that covers all these different cases.
# for each page do the following
for page in doc:
page.clean_contents()
xref = page.get_contents()[0]
cont = bytearray(doc.xref_stream(xref))
pos1 = cont.find(b"/Artifact ") # start of potential watermark code
if pos1 < 0: # then there exists no watermark on this page
continue
pos2 = cont.find(b"EMC", pos1)
# now make sure that the string ``b"/Watermark"`` is contained in the located substring
if b"/Watermark" in cont[pos1:pos2]:
cont[pos1 : pos2+3] = b"" # remove watermark code
doc.update_stream(xref, cont)
continue # done with this page |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
JorjMcKie
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There already exists a GUI script that lets you delete images: look at the repo home page.
But this is actually not what you want apparently: you want to remove watermarks!
A watermark can be multiple different things: an image, normal text (like it seems in your case), or a special annotation.
I don't intend to make a general function that covers all these different cases.
But you have enough options to do that yourself with PyMuPDF right now:
page.deleteAnnot(annot)