Save a singleframe with Wand

1.7k views Asked by At

I'm getting a problem with my little script. I want to use wand in order to convert PDF file on jpeg file and I would like to save juste one particular frame.

My script makes 2 things :

  • If the PDF document makes one page : convert and save it into jpeg file (it works)

  • If the PDF document makes two pages or more : convert and save just the first page into jpeg file (it doesn't work)

My problem is : I want to save what I mean page[0] but I don't find a way to save just a single frame.

#-*- coding: utf-8 -*-

from wand.image import Image
import os

documents_path = "/Users/tiers/Desktop/documents/"

for PDF in os.listdir (documents_path) : #boucle sur tous les PDF du dossier

    convert = Image(filename=documents_path + PDF, resolution=200)  
    name = PDF.split('.') #Récupération du nom

    if len(convert.sequence) == 1 :  #Nombre de page = 1
            convert.compression_quality = 100 #Qualité en %
            convert.save(filename="/Users/tiers/Desktop/documents_jpg/" + name[0] + ".jpg") #Enregistrement en JPEG sous la forme nom.jpg

    elif len(convert.sequence) > 1 : #Nombre de page > 1

            for page in convert.sequence : #Pour chaque page 
                convert.compression_quality = 100 #Qualité en %
                page.save(filename="/Users/tiers/Desktop/documents_jpg/" + name[0] + ".jpg") #Enregistrement en JPEG sous la forme nom.jpg

Have you any idea ?

EDIT :

I edited my script. I break after the first loop in my last for. From this way, I just pick up the first page, but I don't like this kind of things ...

#-*- coding: utf-8 -*-

from wand.image import Image
import os
import matplotlib as plt

documents_path = "/Users/tiers/Desktop/documents/"

for PDF in os.listdir (documents_path) : #boucle sur tous les PDF du dossier

    convert = Image(filename=documents_path + PDF, resolution=200)  
    name = PDF.split('.') #Récupération du nom
    page = len(convert.sequence)

    if page == 1 :  #Nombre de page = 1
            convert.compression_quality = 100 #Qualité en %
            convert.save(filename="/Users/tiers/Desktop/documents_jpg/" + name[0] + ".jpg") #Enregistrement en JPEG sous la forme nom.jpg

    elif page > 1 : #Nombre de page > 1

        for frame in convert.sequence : #Pour chaque page 
                img_page = Image(image=frame)
                img_page.compression_quality = 100 #Qualité en %
                img_page.save(filename="/Users/tiers/Desktop/documents_jpg/" + name[0] + ".jpg") #Enregistrement en JPEG sous la forme nom.jpg
                break

It works, but if you have another way to do that, I take !

2

There are 2 answers

4
Shijo On

Corrected my answer just to include the first page

from wand.image import Image
import os
import matplotlib as plt

documents_path = "/Users/tiers/Desktop/documents/"

for PDF in os.listdir (documents_path) : #boucle sur tous les PDF du dossier

    convert = Image(filename=documents_path + PDF, resolution=200)  
    name = PDF.split('.') #Récupération du nom

    page=convert.sequence[0]
    convert.compression_quality = 100 #Qualité en %
    page.save(filename="/Users/tiers/Desktop/documents_jpg/" + name[0] +  ".jpg") #Enregistrement en JPEG sous
0
Jorge Ou On
from wand.image import Image

with Image(filename='yourfilename.pdf') as img:
    extractedimg = img.sequence[0]
    first_image = Image(image=extractedimg)
    first_image.format= 'jpeg'
    first_image.save(filename='001.jpg')

I think this is better.