5

I'm trying to convert a PDF's first page to an image. However, the PDF is coming straight from the database in a base64 format. I then convert it to a blob. I want to know if it's possible to convert the first page of the PDF to an image within my Python code.

I'm familiar with being able to use filename in the Image object:

Image(filename="test.pdf[0]") as img:

The issue I'm facing is there is not an actual filename, just a blob. This is what I have so far, any suggestions would be appreciated.

x = object['file']
fileBlob = base64.b64decode('x')
with Image(**what do I put here for pdf blob?**) as img:
    more code
rachiebytes
  • 542
  • 1
  • 5
  • 20
  • HOPE THIS HELPS - [http://stackoverflow.com/questions/34129995/pdf-to-raw-image](http://stackoverflow.com/questions/34129995/pdf-to-raw-image) – SystemOverflow LLC Dec 07 '15 at 16:10

2 Answers2

2

It works for me

all_pages = Image(blob=blob_pdf)        # PDF will have several pages.
single_image = all_pages.sequence[0]    # Just work on first page
with Image(single_image) as i:
    ...
Vadym
  • 1,444
  • 21
  • 37
0

Documentation says something about blobs. So it should be:

with Image(blob=fileBlob):
    #etc etc

I didn't test that but I think this is what you are after.

George TG
  • 180
  • 1
  • 9
  • I've tried that previously and get the message "wand.exceptions.WandError: wand contains no images `MagickWand-1' @ error/magick-image.c/MagickWriteImage/13115" – rachiebytes Oct 09 '15 at 17:36
  • Are you sure then that this is the correct way to get your fileBlob? try this, it may help: http://stackoverflow.com/a/26233785/2876209 Also I notice in your code you are giving 'x'(a string) instead of x(which is the object) as an argument to the decode function. That could be the error if this is the way you do it in your code too. – George TG Oct 09 '15 at 18:13