Python应用—从pdf中保存图片

avatar
作者
猴君
阅读量:1
import fitz  # PyMuPDF   def 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_name             with 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)

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!