Spaces:
Sleeping
Sleeping
tomaseo2022
commited on
Commit
·
1405ca8
1
Parent(s):
87d897f
Update app.py
Browse files
app.py
CHANGED
@@ -1,43 +1,33 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
|
|
3 |
from zipfile import ZipFile
|
4 |
-
from
|
5 |
-
from pdf2image import convert_from_bytes
|
6 |
|
7 |
def pdf_to_cbz(pdf_file):
|
8 |
-
if pdf_file is None:
|
9 |
-
raise ValueError("No se recibió ningún archivo PDF.")
|
10 |
-
|
11 |
-
# pdf_file es una tupla de (nombre, tipo, bytes)
|
12 |
pdf_filename, file_type, pdf_bytes = pdf_file
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
# Leer el archivo CBZ y devolver los bytes
|
29 |
-
with open(cbz_temp_file, 'rb') as cbz_file:
|
30 |
-
cbz_bytes = cbz_file.read()
|
31 |
-
|
32 |
-
# Limpiar el archivo temporal CBZ
|
33 |
-
os.remove(cbz_temp_file)
|
34 |
-
|
35 |
-
return cbz_bytes
|
36 |
|
37 |
iface = gr.Interface(
|
38 |
fn=pdf_to_cbz,
|
39 |
inputs=gr.File(type="binary", label="Cargar archivo PDF"),
|
40 |
-
outputs=gr.File(label="Descargar archivo CBZ")
|
|
|
|
|
41 |
)
|
42 |
|
43 |
-
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import os
|
3 |
+
import fitz
|
4 |
from zipfile import ZipFile
|
5 |
+
from io import BytesIO
|
|
|
6 |
|
7 |
def pdf_to_cbz(pdf_file):
|
|
|
|
|
|
|
|
|
8 |
pdf_filename, file_type, pdf_bytes = pdf_file
|
9 |
+
doc = fitz.open(stream=pdf_bytes, filetype="pdf")
|
10 |
+
cbz_temp_file = BytesIO()
|
11 |
+
|
12 |
+
with ZipFile(cbz_temp_file, 'w') as zipf:
|
13 |
+
for page_num in range(len(doc)):
|
14 |
+
page = doc.load_page(page_num)
|
15 |
+
pix = page.get_pixmap()
|
16 |
+
img_data = pix.tobytes("png")
|
17 |
+
img_filename = f"page_{page_num + 1}.png"
|
18 |
+
zipf.writestr(img_filename, img_data)
|
19 |
+
doc.close()
|
20 |
+
|
21 |
+
cbz_temp_file.seek(0)
|
22 |
+
return cbz_temp_file, "comic.cbz"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
iface = gr.Interface(
|
25 |
fn=pdf_to_cbz,
|
26 |
inputs=gr.File(type="binary", label="Cargar archivo PDF"),
|
27 |
+
outputs=gr.File(label="Descargar archivo CBZ"),
|
28 |
+
title="Conversor de PDF a CBZ",
|
29 |
+
description="Esta herramienta convierte un archivo PDF en un archivo CBZ."
|
30 |
)
|
31 |
|
32 |
+
if __name__ == "__main__":
|
33 |
+
iface.launch()
|