extract image from pdf python

import PyPDF2

from PIL import Image

if __name__ == '__main__':
    input1 = PyPDF2.PdfFileReader(open("input.pdf", "rb"))
    page0 = input1.getPage(0)
    xObject = page0['/Resources']['/XObject'].getObject()

    for obj in xObject:
        if xObject[obj]['/Subtype'] == '/Image':
            size = (xObject[obj]['/Width'], xObject[obj]['/Height'])
            data = xObject[obj].getData()
            if xObject[obj]['/ColorSpace'] == '/DeviceRGB':
                mode = "RGB"
            else:
                mode = "P"

            if xObject[obj]['/Filter'] == '/FlateDecode':
                img = Image.frombytes(mode, size, data)
                img.save(obj[1:] + ".png")
            elif xObject[obj]['/Filter'] == '/DCTDecode':
                img = open(obj[1:] + ".jpg", "wb")
                img.write(data)
                img.close()
            elif xObject[obj]['/Filter'] == '/JPXDecode':
                img = open(obj[1:] + ".jp2", "wb")
                img.write(data)
                img.close()

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
extract all image from pdf file python how Extract image from pdf python how to extract images format in pdf in python extract images from specific page pdf python extract image and its text from pdf python how to extract images from pdf python how to extract images from pdf using python 2.7 how to extract images from pdf using python pdf extract image python extract images from pdf python pdfminer extract images from pdf using python extract specific image from pdf python extract selected image from pdf python python module to extract images from pdf extract images from pdf using python how to extract scanned images from pdf using python extract images from pdf in python Extract the image from pdf python extract images from pdf file via python python extract pdf pages as images extract image from pdf ython extracting images from pdf python python get image from pdf extract image from a pdf using python extract image from pdf extract png images with transparent background from pdf python extract png images from pdf python python extract image with transparency from pdf python extract image in png from pdf pyMuPDF python extract rgba image from pdf python extract image in png from pdf extract images using python python extract text from image pdf extract all images from pdf python read image from pdf python images to pdf pythom pypdf2 extract all images from pdf using python python extract images from multiple pdf pages get the image from pdf python extract images from pdf python extract logo from image pdf python read pdf image python how to extract images from pdf in python python extract images from pdf extract images from pdf python extract data from pdf extract image from pdf using python python extract image from pdf python read pdf image generate all images from pdf python extract image from pdf page python extract image from pdf pypdf2 extract image from pdf python pypdf2 xobject
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source