5

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 Answers2

7
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.

Jorge Ou
  • 71
  • 1
  • 2
-1

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
Shijo
  • 9,313
  • 3
  • 19
  • 31
  • Yes, but I want only save the first page to jpeg from each PDF file. With your method, I save each page with good number right ? –  Jan 06 '17 at 17:19
  • i have corrected the answer ..http://docs.wand-py.org/en/0.4.4/guide/sequence.html – Shijo Jan 06 '17 at 17:27
  • And you get the same problem as my question : `AttributeError: 'SingleImage' object has no attribute 'save'` –  Jan 06 '17 at 17:41
  • I don't see this issue in your question, can you please update your question – Shijo Jan 06 '17 at 18:31