0

I am trying to convert all the pages of a PDF to images and save them to a specific working directory. 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. 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. How can I save them in the working directory?

1 Answers1

0

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.

emcconville
  • 23,800
  • 4
  • 50
  • 66