-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
65 lines (52 loc) · 2.22 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import sys
import fitz # PyMuPDF
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QFileDialog, QLabel
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt
import os
class PDFImageExtractor(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle("PDF Image Extractor")
self.setGeometry(100, 100, 400, 200)
layout = QVBoxLayout()
self.label = QLabel("Select a PDF file to extract images", self)
self.label.setAlignment(Qt.AlignCenter)
layout.addWidget(self.label)
self.button = QPushButton("Choose PDF", self)
self.button.clicked.connect(self.choose_pdf)
layout.addWidget(self.button)
self.setLayout(layout)
def choose_pdf(self):
options = QFileDialog.Options()
file_path, _ = QFileDialog.getOpenFileName(self, "Open PDF File", "", "PDF Files (*.pdf)", options=options)
if file_path:
output_folder = QFileDialog.getExistingDirectory(self, "Select Output Folder")
if output_folder:
self.extract_images(file_path, output_folder)
def extract_images(self, pdf_path, output_folder):
pdf_document = fitz.open(pdf_path)
img_count = 0
output_folder = f"{output_folder}/export"
if not os.path.exists(output_folder):
os.makedirs(output_folder)
for page_num in range(pdf_document.page_count):
page = pdf_document[page_num]
images = page.get_images(full=True)
for img_index, img in enumerate(images):
xref = img[0]
base_image = pdf_document.extract_image(xref)
img_bytes = base_image["image"]
img_ext = base_image["ext"]
img_count += 1
img_path = os.path.join(output_folder, f"image_page{page_num + 1}_{img_index + 1}.{img_ext}")
with open(img_path, "wb") as img_file:
img_file.write(img_bytes)
self.label.setText(f"Extracted {img_count} images to: {output_folder}")
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = PDFImageExtractor()
ex.show()
sys.exit(app.exec_())