Question

Imagemagick is open source software suite for displaying, converting, and editing raster image files. Wand is a ctypes-based ImageMagick binding for Python.

How do i get the list of image files, which i got as a result of using Wand?

For example there is a 2-page PDF file file.pdf and i converted it to 2 JPEG files file-0.jpg and file-1.jpg. How do i get the list ['file-0.jpg', 'file-1.jpg']?

Currently i simply use glob:

with Image(filename='file.pdf') as original:
    with original.clone() as converted:
        converted.format = 'jpeg'
        converted.save(filename='file.jpg')
images = glob('*.jpg')

But maybe there is a more idiomatic way through Wand library itself.

Was it helpful?

Solution

You can use Image.sequence. Each sequence item has index.

from wand.image import Image

with Image(filename='file.pdf') as img:
    img.save(filename='file.jpg')
    if len(img.sequence) > 1:
        images = ['file-{0.index}.jpg'.format(x) for x in img.sequence]
    else:
        images = ['file.jpg']
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top