700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 使用PyMuPdf删除及替换PDF中的图片

使用PyMuPdf删除及替换PDF中的图片

时间:2021-11-08 10:55:14

相关推荐

使用PyMuPdf删除及替换PDF中的图片

import fitzif 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 oneimages = page.get_images() # we only are interested in first image hereitem = 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 将所有样本字节清除为0x00print(page.rect)# insert new image just anywherenew_xref = page.insert_image(page.rect, pixmap=pix)# 替换图片copydoc.xref_copy(new_xref, old_xref)# there now is a second /Contents object, showing new imagecont_xrefs = page.get_contents()# make sure that new /Contents is forgotten againpage.set_contents(cont_xrefs[0])page.clean_contents()doc.ez_save("no-image1.pdf", garbage=4)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。