import fitz # PyMuPDFdef extract_images_from_pdf(pdf_path, output_folder):# 打开PDF文件doc = fitz.open(pdf_path)# 遍历PDF的每一页for page_number in range(len(doc)):# 获取当前页page = doc[page_number]# 遍历页面中的所有图片for img_index, img in enumerate(page.get_images(full=True), start=1):xref = img[0] # xref是图片的引用base_image = doc.extract_image(xref) # 提取图片image_bytes = base_image["image"] # 获取图片的字节数据# 保存图片image_name = f"image_page{page_number + 1}_{img_index}.png"image_path = output_folder + image_namewith open(image_path, "wb") as image_file:image_file.write(image_bytes)print(f"Image saved: {image_path}")# 关闭文档doc.close()# PDF文件路径 pdf_path = '1.pdf' # 输出图片的文件夹路径 output_folder = 'output_images'# 调用函数 extract_images_from_pdf(pdf_path, output_folder)