Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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()
Comment

PREVIOUS NEXT
Code Example
Python :: selenium text returns empty string python 
Python :: pandas remove rows with null in column 
Python :: fastest way to output text file in python + Cout 
Python :: append one column pandas dataframe 
Python :: python list to string with spaces 
Python :: select a value randomly in a set python 
Python :: how to exit the program in pygame 
Python :: pytz timezone list 
Python :: adaptive thresholding 
Python :: mean class accuracy sklearn 
Python :: invoice parsing ocr python 
Python :: how to delete everything on a file python 
Python :: how to check if a message includes a word discord.py 
Python :: how to remove all characters from a string in python 
Python :: view point cloud open3d 
Python :: python get financial data 
Python :: pathlib recursive search 
Python :: matplotlib add legend axis x 
Python :: coronavirus program in python 
Python :: check if coroutine python 
Python :: python aritmethic print 
Python :: python legend outside 
Python :: ball bounce in pygame 
Python :: python poner en mayusculas 
Python :: from time import sleep, time 
Python :: pygame hide cursor 
Python :: 1052 uri solution 
Python :: python rickroll code 
Python :: fastest sort python 
Python :: check if user has manage messages discord.py 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =