import fitz
if tuple(map(int, fitz.VersionBind.split("."))) < (1, 19, 5):
raise ValueError("Need v1.19.5+")
# 用完全透明的pixmap替换实现删除
doc = fitz.open("original.pdf")
page = doc[0]
page.clean_contents() # unify page's /Contents into one
images = page.get_images() # we only are interested in first image here
item = images[0]
old_xref = item[0] # old image xref 旧图像参照
# 制作一个小的100%透明的pixmap(任意尺寸)
pix = fitz.Pixmap(fitz.csGRAY, (0, 0, 1, 1), 1)
print(pix)
pix.clear_with() # clear all samples bytes to 0x00 将所有样本字节清除为0x00
print(page.rect)
# insert new image just anywhere
new_xref = page.insert_image(page.rect, pixmap=pix)
# 替换图片copy
doc.xref_copy(new_xref, old_xref)
# there now is a second /Contents object, showing new image
cont_xrefs = page.get_contents()
# make sure that new /Contents is forgotten again
page.set_contents(cont_xrefs[0])
page.clean_contents()
doc.ez_save("no-image1.pdf", garbage=4)
04-07
989
09-19
831
04-09
903