我想用Python从一个pdf中提取一些图像。我可以使用poppler-utils库中的pdfimages在Linux命令行上轻松地提取图像,就像这样:
pdfimages my_file.pdf /tmp/image
接下来,我找到了一个Python绑定库此处,并使用常规的sudo apt-get install python-poppler
进行了安装。现在,在python解释器中,我可以执行以下操作:
>>> 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']
>>>
从这里开始,我有点迷失了。在这个列表中,我找不到任何类似于pdfimages名称或相关的内容。在python-poppler的源文件中,我甚至找不到.py文件。
是否有人知道如何在Python中使用poppler pdfimages工具?欢迎提供任何技巧!
subprocess.Popen()
调用 Linux 的pdfimages
命令将图像提取到临时文件夹中,然后循环遍历这些图像并在 Python 中读取它们。也许这对你来说也是一个解决方案。祝你好运! - kramer65poppler-qt5
。 - reza