0

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
Essex
  • 6,042
  • 11
  • 67
  • 139

1 Answers1

0

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)
emcconville
  • 23,800
  • 4
  • 50
  • 66
  • @emcconvile I edited my script with your question, but How I can save my jpeg as blob binary string ? Because if I wrote : `blob.save`, I get an error : `AttributeError: 'str' object has no attribute 'save'` – Essex Jan 10 '17 at 08:21
  • The `blob` variable would be the binary string. – emcconville Jan 10 '17 at 14:07
  • And How I could save it and decode it after with OpenCV ? – Essex Jan 10 '17 at 14:08