Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 697
kramer65 Avatar asked Oct 22 '14 13:10

kramer65


1 Answers

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.

like image 95
reza Avatar answered Oct 13 '22 06:10

reza