Python ';jpeg&x27;是不支持的格式错误

Python ';jpeg&x27;是不支持的格式错误,python,imagemagick,wand,magickwand,Python,Imagemagick,Wand,Magickwand,这是我写的方法: 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 =

这是我写的方法:

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
我得到

“jpeg”是不受支持的格式

错误

奥斯卡皮坦酒店 python 2.7.10
同样的代码也适用于其他使用debian的计算机。

您误解了Image.convert的功能。它不在文件格式之间转换,而是在像素格式之间转换,例如,RGB像素的“RGB”或CMYK数据的“CMYK”。要以特定文件格式实际输出图像,请使用
image.save

jpeg_bytes = io.BytesIO()
img.save(jpeg_bytes, "jpeg")
缓冲区
jpeg\u字节
然后包含jpeg数据


编辑:如果我没记错的话,PDF是用PIL写的。因此,您无法从PDF原始数据加载图像。但这是另一个问题…

您可能需要安装“jpeg”和/或“ghostscript

对于mac:

brew install jpeg
brew install ghostscript
对于linux:

JPEG

重影脚本

下载并安装最新版本


它为我解决了类似的问题。

对不起,我假设您使用的是Python图像库。我应该读一下标签。。。我没有使用魔杖的经验。你用的是Python2还是pytho3?似乎库正在使用python 2.7.10检查format.im的错误类型
brew install jpeg
brew install ghostscript