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 !
Corrected my answer just to include the first page