报错:AttributeError: 'Pixmap' object has no attribute 'writePNG'. Did you mean: '_writeIMG'?
import fitz
import re
import os
file_path = r'img.pdf'
dir_path = r'pdf'
def pdf2image1(path, pic_path):
checkIM = r"/Subtype(?= */Image)"
pdf = fitz.open(path)
lenXREF = pdf.xref_length()
count = 1
for i in range(1, lenXREF):
text = pdf.xref_object(i)
isImage = re.search(checkIM, text)
if not isImage:
continue
pix = fitz.Pixmap(pdf, i)
new_name = f"img_{count}.png"
pix.writePNG(os.path.join(pic_path, new_name))
count += 1
pix = None
pdf2image1(file_path, dir_path)
由于pdf文件的结构比较复杂,我们需要使用一些第三方库来帮助我们提取图片。下面是一个示例代码,使用了PyMuPDF库来读取pdf文件并提取图片:
import fitz
# 打开pdf文件
pdf_file = fitz.open('example.pdf')
# 遍历pdf中的所有页面
for page in pdf_file:
# 查找页面中的图片
for image in page.get_image_list():
# 提取图片并保存
img_bytes = image[0]
img_ext = image[1]
with open(f"image.{img_ext}", "wb") as f:
f.write(img_bytes)