Spaces:
Sleeping
Sleeping
Commit
路
99419c6
1
Parent(s):
8d10365
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,11 @@
|
|
1 |
import gradio as gr
|
2 |
-
import fitz
|
3 |
import zipfile
|
4 |
import tempfile
|
5 |
import shutil
|
6 |
import os
|
|
|
|
|
7 |
|
8 |
def cbz_to_pdf(cbz_file):
|
9 |
try:
|
@@ -11,7 +13,7 @@ def cbz_to_pdf(cbz_file):
|
|
11 |
if cbz_file is None:
|
12 |
raise ValueError("No se recibi贸 ning煤n archivo CBZ.")
|
13 |
|
14 |
-
# Crear un
|
15 |
temp_dir = tempfile.mkdtemp()
|
16 |
cbz_temp_path = os.path.join(temp_dir, "temp.cbz")
|
17 |
with open(cbz_temp_path, "wb") as f:
|
@@ -21,30 +23,32 @@ def cbz_to_pdf(cbz_file):
|
|
21 |
pdf_filename = os.path.join(temp_dir, "output.pdf")
|
22 |
pdf = fitz.open()
|
23 |
|
24 |
-
# Abrir el archivo CBZ
|
25 |
with zipfile.ZipFile(cbz_temp_path, 'r') as zipf:
|
26 |
-
# Ordenar los nombres de los archivos
|
27 |
sorted_files = sorted(zipf.namelist())
|
28 |
|
29 |
-
#
|
30 |
for file_name in sorted_files:
|
31 |
image_bytes = zipf.read(file_name)
|
32 |
-
|
|
|
|
|
|
|
33 |
pdf_page = pdf.new_page(width=img[0].width, height=img[0].height)
|
34 |
-
pdf_page.insert_image(pdf_page.rect,
|
35 |
|
36 |
-
# Guardar el PDF
|
37 |
pdf.save(pdf_filename)
|
38 |
pdf.close()
|
39 |
|
40 |
-
#
|
41 |
-
shutil.rmtree(temp_dir)
|
42 |
-
|
43 |
return pdf_filename
|
44 |
|
45 |
except Exception as e:
|
46 |
return f"Error al procesar el archivo: {str(e)}"
|
47 |
|
|
|
48 |
iface = gr.Interface(
|
49 |
fn=cbz_to_pdf,
|
50 |
inputs=gr.File(type="binary", label="Cargar archivo CBZ"),
|
|
|
1 |
import gradio as gr
|
2 |
+
import fitz # PyMuPDF
|
3 |
import zipfile
|
4 |
import tempfile
|
5 |
import shutil
|
6 |
import os
|
7 |
+
from PIL import Image
|
8 |
+
import io
|
9 |
|
10 |
def cbz_to_pdf(cbz_file):
|
11 |
try:
|
|
|
13 |
if cbz_file is None:
|
14 |
raise ValueError("No se recibi贸 ning煤n archivo CBZ.")
|
15 |
|
16 |
+
# Crear un directorio temporal para el archivo CBZ
|
17 |
temp_dir = tempfile.mkdtemp()
|
18 |
cbz_temp_path = os.path.join(temp_dir, "temp.cbz")
|
19 |
with open(cbz_temp_path, "wb") as f:
|
|
|
23 |
pdf_filename = os.path.join(temp_dir, "output.pdf")
|
24 |
pdf = fitz.open()
|
25 |
|
26 |
+
# Abrir el archivo CBZ
|
27 |
with zipfile.ZipFile(cbz_temp_path, 'r') as zipf:
|
28 |
+
# Ordenar los nombres de los archivos
|
29 |
sorted_files = sorted(zipf.namelist())
|
30 |
|
31 |
+
# Procesar cada imagen y a帽adirla al PDF
|
32 |
for file_name in sorted_files:
|
33 |
image_bytes = zipf.read(file_name)
|
34 |
+
image = Image.open(io.BytesIO(image_bytes))
|
35 |
+
pdfbytes = io.BytesIO()
|
36 |
+
image.save(pdfbytes, format="PNG")
|
37 |
+
img = fitz.open("png", pdfbytes.getvalue())
|
38 |
pdf_page = pdf.new_page(width=img[0].width, height=img[0].height)
|
39 |
+
pdf_page.insert_image(pdf_page.rect, pixmap=img[0].get_pixmap())
|
40 |
|
41 |
+
# Guardar el archivo PDF
|
42 |
pdf.save(pdf_filename)
|
43 |
pdf.close()
|
44 |
|
45 |
+
# Devolver el archivo PDF
|
|
|
|
|
46 |
return pdf_filename
|
47 |
|
48 |
except Exception as e:
|
49 |
return f"Error al procesar el archivo: {str(e)}"
|
50 |
|
51 |
+
# Crear la interfaz de Gradio
|
52 |
iface = gr.Interface(
|
53 |
fn=cbz_to_pdf,
|
54 |
inputs=gr.File(type="binary", label="Cargar archivo CBZ"),
|