How to extract images from a pdf using the poppler library in Python?
Asked Answered
F

2

9

I've got a pdf from which I want to extract some images using Python. I can easily extract images from the Linux command line using the pdfimages from the poppler-utils library like this:

pdfimages my_file.pdf /tmp/image

Next I found a Python binding for it here, and installed it using the usual sudo apt-get install python-poppler. In the python interpreter I can now do this:

>>> import poppler
>>> dir(poppler)
['ACTION_GOTO_DEST', 'ACTION_GOTO_REMOTE', 'ACTION_JAVASCRIPT', 'ACTION_LAUNCH', 'ACTION_MOVIE', 'ACTION_NAMED', 'ACTION_NONE', 'ACTION_OCG_STATE', 'ACTION_RENDITION', 'ACTION_UNKNOWN', 'ACTION_URI', 'ANNOT_3D', 'ANNOT_CARET', 'ANNOT_CIRCLE', 'ANNOT_EXTERNAL_DATA_MARKUP_3D', 'ANNOT_EXTERNAL_DATA_MARKUP_UNKNOWN', 'ANNOT_FILE_ATTACHMENT', 'ANNOT_FLAG_HIDDEN', 'ANNOT_FLAG_INVISIBLE', 'ANNOT_FLAG_LOCKED', 'ANNOT_FLAG_LOCKED_CONTENTS', 'ANNOT_FLAG_NO_ROTATE', 'ANNOT_FLAG_NO_VIEW', 'ANNOT_FLAG_NO_ZOOM', 'ANNOT_FLAG_PRINT', 'ANNOT_FLAG_READ_ONLY', 'ANNOT_FLAG_TOGGLE_NO_VIEW', 'ANNOT_FLAG_UNKNOWN', 'ANNOT_FREE_TEXT', 'ANNOT_FREE_TEXT_QUADDING_CENTERED', 'ANNOT_FREE_TEXT_QUADDING_LEFT_JUSTIFIED', 'ANNOT_FREE_TEXT_QUADDING_RIGHT_JUSTIFIED', 'ANNOT_HIGHLIGHT', 'ANNOT_INK', 'ANNOT_LINE', 'ANNOT_LINK', 'ANNOT_MARKUP_REPLY_TYPE_GROUP', 'ANNOT_MARKUP_REPLY_TYPE_R', 'ANNOT_MOVIE', 'ANNOT_POLYGON', 'ANNOT_POLY_LINE', 'ANNOT_POPUP', 'ANNOT_PRINTER_MARK', 'ANNOT_SCREEN', 'ANNOT_SOUND', 'ANNOT_SQUARE', 'ANNOT_SQUIGGLY', 'ANNOT_STAMP', 'ANNOT_STRIKE_OUT', 'ANNOT_TEXT', 'ANNOT_TEXT_STATE_ACCEPTED', 'ANNOT_TEXT_STATE_CANCELLED', 'ANNOT_TEXT_STATE_COMPLETED', 'ANNOT_TEXT_STATE_MARKED', 'ANNOT_TEXT_STATE_NONE', 'ANNOT_TEXT_STATE_REJECTED', 'ANNOT_TEXT_STATE_UNKNOWN', 'ANNOT_TEXT_STATE_UNMARKED', 'ANNOT_TRAP_NET', 'ANNOT_UNDERLINE', 'ANNOT_UNKNOWN', 'ANNOT_WATERMARK', 'ANNOT_WIDGET', 'Action', 'ActionAny', 'ActionGotoDest', 'ActionGotoRemote', 'ActionLaunch', 'ActionMovie', 'ActionNamed', 'ActionType', 'ActionUri', 'Annot', 'AnnotCalloutLine', 'AnnotExternalDataType', 'AnnotFlag', 'AnnotFreeText', 'AnnotFreeTextQuadding', 'AnnotMapping', 'AnnotMarkup', 'AnnotMarkupReplyType', 'AnnotText', 'AnnotTextState', 'AnnotType', 'Attachment', 'BACKEND_CAIRO', 'BACKEND_SPLASH', 'BACKEND_UNKNOWN', 'Backend', 'Color', 'DEST_FIT', 'DEST_FITB', 'DEST_FITBH', 'DEST_FITBV', 'DEST_FITH', 'DEST_FITR', 'DEST_FITV', 'DEST_NAMED', 'DEST_UNKNOWN', 'DEST_XYZ', 'Dest', 'DestType', 'Document', 'ERROR_BAD_CATALOG', 'ERROR_DAMAGED', 'ERROR_ENCRYPTED', 'ERROR_INVALID', 'ERROR_OPEN_FILE', 'Error', 'FONT_TYPE_CID_TYPE0', 'FONT_TYPE_CID_TYPE0C', 'FONT_TYPE_CID_TYPE0COT', 'FONT_TYPE_CID_TYPE2', 'FONT_TYPE_CID_TYPE2OT', 'FONT_TYPE_TRUETYPE', 'FONT_TYPE_TRUETYPEOT', 'FONT_TYPE_TYPE1', 'FONT_TYPE_TYPE1C', 'FONT_TYPE_TYPE1COT', 'FONT_TYPE_TYPE3', 'FONT_TYPE_UNKNOWN', 'FORM_BUTTON_CHECK', 'FORM_BUTTON_PUSH', 'FORM_BUTTON_RADIO', 'FORM_CHOICE_COMBO', 'FORM_CHOICE_LIST', 'FORM_FIELD_BUTTON', 'FORM_FIELD_CHOICE', 'FORM_FIELD_SIGNATURE', 'FORM_FIELD_TEXT', 'FORM_FIELD_UNKNOWN', 'FORM_TEXT_FILE_SELECT', 'FORM_TEXT_MULTILINE', 'FORM_TEXT_NORMAL', 'FontInfo', 'FontType', 'FontsIter', 'FormButtonType', 'FormChoiceType', 'FormField', 'FormFieldMapping', 'FormFieldType', 'FormTextType', 'ImageMapping', 'IndexIter', 'Layer', 'LayersIter', 'LinkMapping', 'ORIENTATION_LANDSCAPE', 'ORIENTATION_PORTRAIT', 'ORIENTATION_SEASCAPE', 'ORIENTATION_UPSIDEDOWN', 'Orientation', 'PAGE_LAYOUT_ONE_COLUMN', 'PAGE_LAYOUT_SINGLE_PAGE', 'PAGE_LAYOUT_TWO_COLUMN_LEFT', 'PAGE_LAYOUT_TWO_COLUMN_RIGHT', 'PAGE_LAYOUT_TWO_PAGE_LEFT', 'PAGE_LAYOUT_TWO_PAGE_RIGHT', 'PAGE_LAYOUT_UNSET', 'PAGE_MODE_FULL_SCREEN', 'PAGE_MODE_NONE', 'PAGE_MODE_UNSET', 'PAGE_MODE_USE_ATTACHMENTS', 'PAGE_MODE_USE_OC', 'PAGE_MODE_USE_OUTLINES', 'PAGE_MODE_USE_THUMBS', 'PAGE_TRANSITION_BLINDS', 'PAGE_TRANSITION_BOX', 'PAGE_TRANSITION_COVER', 'PAGE_TRANSITION_DISSOLVE', 'PAGE_TRANSITION_FADE', 'PAGE_TRANSITION_FLY', 'PAGE_TRANSITION_GLITTER', 'PAGE_TRANSITION_HORIZONTAL', 'PAGE_TRANSITION_INWARD', 'PAGE_TRANSITION_OUTWARD', 'PAGE_TRANSITION_PUSH', 'PAGE_TRANSITION_REPLACE', 'PAGE_TRANSITION_SPLIT', 'PAGE_TRANSITION_UNCOVER', 'PAGE_TRANSITION_VERTICAL', 'PAGE_TRANSITION_WIPE', 'PERMISSIONS_FULL', 'PERMISSIONS_OK_TO_ADD_NOTES', 'PERMISSIONS_OK_TO_ASSEMBLE', 'PERMISSIONS_OK_TO_COPY', 'PERMISSIONS_OK_TO_EXTRACT_CONTENTS', 'PERMISSIONS_OK_TO_FILL_FORM', 'PERMISSIONS_OK_TO_MODIFY', 'PERMISSIONS_OK_TO_PRINT', 'PERMISSIONS_OK_TO_PRINT_HIGH_RESOLUTION', 'PSFile', 'Page', 'PageLayout', 'PageMode', 'PageTransition', 'PageTransitionAlignment', 'PageTransitionDirection', 'PageTransitionType', 'Permissions', 'Rectangle', 'SELECTION_GLYPH', 'SELECTION_LINE', 'SELECTION_WORD', 'SelectionStyle', 'VIEWER_PREFERENCES_CENTER_WINDOW', 'VIEWER_PREFERENCES_DIRECTION_RTL', 'VIEWER_PREFERENCES_DISPLAY_DOC_TITLE', 'VIEWER_PREFERENCES_FIT_WINDOW', 'VIEWER_PREFERENCES_HIDE_MENUBAR', 'VIEWER_PREFERENCES_HIDE_TOOLBAR', 'VIEWER_PREFERENCES_HIDE_WINDOWUI', 'VIEWER_PREFERENCES_UNSET', 'ViewerPreferences', '__doc__', '__file__', '__name__', '__package__', '__version__', 'document_new_from_data', 'document_new_from_file', 'get_backend', 'get_version', 'pypoppler_version']
>>> 

From here I'm kinda lost though. In this list I can't find anything resembling the pdfimages name, or anything related. In the source files of python-poppler I can't even find a .py file.

Does anybody know how I could use the poppler pdfimages tool from within Python? All tips are welcome!

Fecit answered 22/10, 2014 at 13:47 Comment(3)
Did you ever figure this out? Any pointer in the right direction would be very useful.Cyclopropane
@Cyclopropane - In the end I simply used subprocess.Popen() to call the Linux pdfimages command to extract the images to a temporary folder, after which I loop over the images and read them out in Python. Maybe that can be a solution for you as well. Good luck!Fecit
This is my question too, I am using poppler-qt5.Rugger
R
1

Explore ImageMapping and there you would find what you are looking for. An ImageMapping object contains an identifier(integer) for the image which is required for image retrieval plus the area of the image in paper coordinates.
This is something borrowed from the glib backend of poppler. I have tested the C version(poppler-glib) version of theImageMapping and it works fine.
Please note that poppler bindings that rely on C++/Qt lack this functionality.

Rugger answered 28/5, 2019 at 19:4 Comment(0)
L
0

I can't tell you about poppler, but in pyPDF you would (roughly)

  • Open the file in binary mode and pass the fileobj to pyPdf.PdfFileReader()
  • Get an object for each page using getPage(); use getNumPages() to get the total number of pages first.
  • The page object is dict-like and has a /Resources-key, the resulting object has a /XObject-key. The .values() of page['/Resources']['/XObject'] may have objects where ['SubType'] == '/Image'. Those are the objects you want.
  • Depending on the encoding of the image, ['/Filter'] can be '/FlateDecode', '/DCTDecode' or other. You may need to preprocess the object's data-stream according to the given filter before passing them on.
  • Use the _data attribute to get the raw bytes. Write those to disk or pass them on to an image library.

I wrote a hacky ad-filter for PDFs once that, among other things, re-encodes way-too-big-images to lower resolutions. The code should give you some more guidance than the bullet points above.

Notice that PDF is a hell of a file format, so your code will see many weird things. Pepper your code with assertions and always have a final else: raise ValueError on every if :-)

Also notice that the guidance posted above is not strictly correct. What you want to do is to get the PDF's catalog and work your way to the /Resources object that way; I can't recall object-tree from memory though. Have the reference ready.

Lilith answered 2/6, 2017 at 19:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.