简体   繁体   English

'jpeg' 是不支持的格式错误

[英]'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 “jpeg”是不受支持的格式

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. osx el capan python 2.7.10 相同的代码适用于其他带有 debian 的计算机。

You may need to install ' jpeg ' and/or ' ghostscript '您可能需要安装“ jpeg ”和/或“ ghostscript

For mac:对于 Mac:

brew install jpeg
brew install ghostscript

For linux :对于 linux :

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

Ghostscript : http://ghostscript.com/download/幽灵脚本http : //ghostscript.com/download/

Download and install latest versions.下载并安装最新版本。

It solved similar problem for me.它为我解决了类似的问题。

You misunderstood the function of Image.convert.你误解了 Image.convert 的功能。 It does not convert between file formats, but pixel formats, eg "RGB" for RGB pixels or "CMYK" for CMYK data.它不会在文件格式之间转换,而是在像素格式之间进行转换,例如“RGB”代表 RGB 像素或“CMYK”代表 CMYK 数据。 To actually output the image in a specific file format, use Image.save :要以特定文件格式实际输出图像,请使用Image.save

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

The buffer jpeg_bytes then contains the JPEG data.然后缓冲区jpeg_bytes包含 JPEG 数据。

Edit: if I remember correctly, PDF is write-only in PIL.编辑:如果我没记错的话,PDF 在 PIL 中是只写的。 Thus you can't load an image from PDF raw data.因此,您无法从 PDF 原始数据加载图像。 But that's another issue...但这是另一个问题...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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