Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,51 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import fitz
|
3 |
+
import os
|
4 |
+
import zipfile
|
5 |
+
|
6 |
+
def pdf_to_images(pdf_file):
|
7 |
+
|
8 |
+
doc = fitz.open(pdf_file)
|
9 |
+
images = []
|
10 |
+
|
11 |
+
for page_id in range(doc.page_count):
|
12 |
+
page = doc[page_id]
|
13 |
+
|
14 |
+
pix = page.get_pixmap()
|
15 |
+
img_bytes = pix.tobytes("png")
|
16 |
+
images.append((img_bytes, f"{page_id+1}.png"))
|
17 |
+
|
18 |
+
|
19 |
+
doc.close()
|
20 |
+
|
21 |
+
|
22 |
+
temp_dir = "temp_images"
|
23 |
+
os.makedirs(temp_dir, exist_ok=True)
|
24 |
+
|
25 |
+
|
26 |
+
for img_bytes, img_name in images:
|
27 |
+
with open(os.path.join(temp_dir, img_name), "wb") as f:
|
28 |
+
f.write(img_bytes)
|
29 |
+
|
30 |
+
|
31 |
+
zip_path = "images.zip"
|
32 |
+
with zipfile.ZipFile(zip_path, "w") as zipf:
|
33 |
+
for img_name in os.listdir(temp_dir):
|
34 |
+
zipf.write(os.path.join(temp_dir, img_name), img_name)
|
35 |
+
|
36 |
+
|
37 |
+
for img_name in os.listdir(temp_dir):
|
38 |
+
os.remove(os.path.join(temp_dir, img_name))
|
39 |
+
os.rmdir(temp_dir)
|
40 |
+
|
41 |
+
return zip_path
|
42 |
+
|
43 |
+
def main():
|
44 |
+
iface = gr.Interface(
|
45 |
+
fn=pdf_to_images,
|
46 |
+
inputs=gr.File(label="Upload PDF File"),
|
47 |
+
outputs=gr.File(label="Download ZIP File"),
|
48 |
+
title="PDF to Images Converter",
|
49 |
+
description="Upload a PDF file and download a ZIP file containing all the pages as images."
|
50 |
+
)
|
51 |
+
iface.launch()
|