Reputation: 21
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.
Upvotes: 2
Views: 2361
Reputation: 3153
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.
Upvotes: 1
Reputation: 1377
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...
Upvotes: 0