简体   繁体   中英

'jpeg' is unsupported format error

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.

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.

You misunderstood the function of Image.convert. It does not convert between file formats, but pixel formats, eg "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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM