简体   繁体   English

PDF 到 Image 并使用 Wand Python 将其下载到特定文件夹

[英]PDF to Image and downloading it to a specific folder using Wand Python

I am trying to convert all the pages of a PDF to images and save them to a specific working directory.我正在尝试将 PDF 的所有页面转换为图像并将它们保存到特定的工作目录。 The code is:代码是:

from wand.image import Image

from wand.image import Image as wi
pdf = wi(filename="work.pdf", resolution=300)
pdfimage = pdf.convert("jpeg")
i=1
for img in pdfimage.sequence:
    page = wi(image=img)
    page.save(filename=r"C:\Users\...\work" + str(i) +  ".jpg")
    i +=1

As you can see, I am converting each page to jpg format and then am trying to save them in the folder.如您所见,我将每个页面转换为 jpg 格式,然后尝试将它们保存在文件夹中。 But due to some reason, it is not working.但由于某种原因,它不起作用。 If instead of the second last line, I try:如果不是倒数第二行,我尝试:

from wand.image import Image as wi
pdf = wi(filename="work.pdf", resolution=300)
pdfimage = pdf.convert("jpeg")
i=1
for img in pdfimage.sequence:
    page = wi(image=img)
    #page.save(filename=r"C:\Users\...\work" + str(i) +  ".jpg")
    page.save(filename=str(i)+".jpg")
    i +=1

then it saves successfully but in the folder C:\Users\Me.然后它成功保存但在文件夹 C:\Users\Me 中。 How can I save them in the working directory?如何将它们保存在工作目录中?

Try this...尝试这个...

import os
from wand.image import Image as wi
with wi(filename="work.pdf", resolution=300) as pdf:
    pdf.scene = 1
    pdf.save(filename=os.path.join(os.getcwd(),"work%02d.jpg")

Wand should also support pathlib , or other classes that implement __fspath__() itereface. Wand 还应该支持pathlib或其他实现__fspath__() itereface 的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM