Недавно я дорабатывал утилиту для обработки PDF файлов, которая в том числе должна была модифицировать изображения. То была программа была на .Net , но для ее проверки надо было выгрузить из файла изображения и убедиться, что все изменено как надо.
На скорую руку написал на Python скрипт для экспорта изображений из PDF файла.
В скрипте никакого интерактива - в переменной 't' полный путь к файлу, 'outdir' - директория для экспорта изображений. Используемые библиотеки - os и pymupdf.
Сам код ниже:
t = "C:\\Users\\Aleksei\\AppData\\Local\\IE\\8PNQGRH1\\открытка[1].pdf"
doc = pymupdf.open(t) # open a document
out = open("output.txt", "wb") # create a text output
for page in doc: # iterate the document pages
text = page.get_text().encode("utf8") # get plain text (is in UTF-8)
out.write(text) # write text of page
out.write(bytes((12,))) # write page delimiter (form feed 0x0C)
out.close()
outdir = "c:\\temp\\pdfimages\"
if not os.path.exists(outdir):
os.makedirs(outdir)
for page_index in range(len(doc)): # iterate over pdf pages
page = doc[page_index] # get the page
image_list = page.get_images()
# print the number of images found on the page
if image_list:
print(f"Found {len(image_list)} images on page {page_index}")
else:
print("No images found on page", page_index)
for image_index, img in enumerate(image_list, start=1): # enumerate the image list
xref = img[0] # get the XREF of the image
pix = pymupdf.Pixmap(doc, xref) # create a Pixmap
if pix.n - pix.alpha > 3: # CMYK: convert to RGB first
pix = pymupdf.Pixmap(pymupdf.csRGB, pix)
pix.save( (outdir + "page_%s-image_%s.png") % (page_index, image_index)) # save the image as png
pix = None
Запускал из PyCharm нажатием Shift + F10.
