2

This is method i wrote:

def pdf_page_to_png(src_pdf, pagenum=0, resolution=300, slug=''):
    dst_pdf = PyPDF2.PdfFileWriter()
    dst_pdf.addPage(src_pdf.getPage(pagenum))

    pdf_bytes = io.BytesIO()
    dst_pdf.write(pdf_bytes)
    pdf_bytes.seek(0)

    img = Image(file=pdf_bytes, resolution=resolution)
    img.convert("jpeg")

    if pagenum == 0:
        os.makedirs('media/einsert/%s' % slug)

    img.save(filename='media/einsert/%s/page_%s.jpeg' % (slug, pagenum))

    return img

and i get

'jpeg' is unsupported format

error

/Users/daro/praca/polsha24/lib/python2.7/site-packages/wand/image.py in format
    def format(self, fmt):
        if not isinstance(fmt, string_type):
            raise TypeError("format must be a string like 'png' or 'jpeg'"
                            ', not ' + repr(fmt))
        fmt = fmt.strip()
        r = library.MagickSetImageFormat(self.wand, binary(fmt.upper()))
        if not r:
                        raise ValueError(repr(fmt) + ' is unsupported format') ...
        r = library.MagickSetFilename(self.wand,
                                      b'buffer.' + binary(fmt.lower()))
        if not r:
            self.raise_exception()
    @property

osx el capitan python 2.7.10 same code works on other computer with debian.

KylieY
  • 21
  • 2

2 Answers2

1

You may need to install 'jpeg' and/or 'ghostscript'

For mac:

brew install jpeg
brew install ghostscript

For linux :

JPEG: http://www.ijg.org/files/

Ghostscript: http://ghostscript.com/download/

Download and install latest versions.

It solved similar problem for me.

RAVI
  • 3,143
  • 4
  • 25
  • 38
0

You misunderstood the function of Image.convert. It does not convert between file formats, but pixel formats, e.g. "RGB" for RGB pixels or "CMYK" for CMYK data. To actually output the image in a specific file format, use Image.save:

jpeg_bytes = io.BytesIO()
img.save(jpeg_bytes, "jpeg")

The buffer jpeg_bytes then contains the JPEG data.

Edit: if I remember correctly, PDF is write-only in PIL. Thus you can't load an image from PDF raw data. But that's another issue...

Sven Rusch
  • 1,357
  • 11
  • 17
  • http://docs.wand-py.org/en/0.4.1/wand/image.html?highlight=convert#wand.image.Image.convert – KylieY Oct 25 '15 at 23:44
  • I'm sorry, I assumed you were using the Python Imaging Library. I should have read the tags... I have no experience with wand. Are you using Python2 or 3? It seems like the library is checking for a wrong type for the format. – Sven Rusch Oct 25 '15 at 23:50
  • im using python 2.7.10 – KylieY Oct 26 '15 at 01:08