NoneType output files with Wand

383 views Asked by At

I would like to save PDF file as JPEG file (more precisely, only the first page) with ImageMagick and Wand.

But, when the file is saved, I print the file type and I get NoneType. I need to get uint8 because I have to use each JPEG image with OpenCV after.

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

from wand.image import Image
import os, shutil, glob


for PDF in os.listdir ("/Users/valentinjungbluth/Desktop/NAPS_PDF/") : #boucle sur tous les PDF du dossier

        if PDF.endswith('.pdf'):

                with Image(filename="/Users/valentinjungbluth/Desktop/NAPS_PDF/" + PDF, resolution=200) as convert :
                        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 %
                                saved = convert.save(filename="/Users/valentinjungbluth/Desktop/PDF_to_JPG/" + name[0] + ".jpg") #Enregistrement en JPEG sous la forme nom.jpg
                                print type(saved)


                        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/valentinjungbluth/Desktop/PDF_to_JPG/" + name[0] + ".jpg") #Enregistrement en JPEG sous la forme nom.jpg
                                        break

I don't find a way to save correctly to jpeg file and not NoneType.

Do you have any idea to do that ?

EDIT :

Script which is updated from answer with make_blob :

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

import os
from wand.image import Image


for PDF in os.listdir("/Users/valentinjungbluth/Desktop/NAPS_PDF/"): #boucle sur tous les PDF

    if PDF.endswith('.pdf'):

        with Image(filename="/Users/valentinjungbluth/Desktop/NAPS_PDF/" + PDF, resolution=200) as convert :
            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 %
                blob = convert.make_blob("JPEG")
                #saved = convert.save(filename="/Users/valentinjungbluth/Desktop/PDF_to_JPG/" + name[0] + ".jpg") #Enregistrement en JPEG sous la forme nom.jpg
                print type(saved)


            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 %
                    blob = img_page.make_blob("JPEG")
                    #convert.save(filename="/Users/valentinjungbluth/Desktop/PDF_to_JPG/" + name[0] + ".jpg") #Enregistrement en JPEG sous la forme nom.jpg
                    print type(blob)
                    break
1

There are 1 answers

3
emcconville On

I believe wand's wand.image.Image.save will not return any data. There's a wand.image.Image.make_blob method for this.

with Image(filename="...") as img:
  blob = img.make_blob("JPG")

The above will return the jpeg data for OpenCV's cv2.imdecode.

byte_vector = numpy.fromstring(blob, numpy.uint8)
mat = cv2.imdecode(byte_vector, cv2.CV_LOAD_IMAGE_COLOR)

If you need to save the blob data to a file, and no longer have the wand.Image instance, then it's as simple as.

with open('myFile.jpg', 'w') as fd:
  fd.write(blob)