Upload 3 files
Browse files- app.py +38 -72
- requirements.txt +1 -2
app.py
CHANGED
@@ -1,73 +1,39 @@
|
|
1 |
-
import cv2
|
2 |
-
import numpy as np
|
3 |
-
from reportlab.pdfgen import canvas
|
4 |
-
from reportlab.lib.units import inch
|
5 |
-
import tempfile
|
6 |
-
from PIL import Image, ImageDraw, ImageFont
|
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 |
-
def gerar_pdf(pontos):
|
47 |
-
temp_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
|
48 |
-
c = canvas.Canvas(temp_pdf.name, pagesize=(PAGE_WIDTH, PAGE_HEIGHT))
|
49 |
-
for i, (x, y) in enumerate(pontos):
|
50 |
-
c.circle(x, PAGE_HEIGHT - y, 2, fill=1)
|
51 |
-
c.setFont("Helvetica", 8)
|
52 |
-
c.drawString(x + 3, PAGE_HEIGHT - y + 3, str(i + 1))
|
53 |
-
c.save()
|
54 |
-
return temp_pdf.name
|
55 |
-
|
56 |
-
def processar_e_mostrar(imagem_array, mascara_array, idade):
|
57 |
-
pontos = detectar_pontos(imagem_array, mascara_array, idade)
|
58 |
-
preview = gerar_preview_com_pontos(pontos)
|
59 |
-
pdf = gerar_pdf(pontos)
|
60 |
-
return {"preview": preview, "pdf": pdf}
|
61 |
-
|
62 |
-
def gerar_preview_kdp():
|
63 |
-
img = Image.new("RGB", (1500, 1000), color=(250, 240, 210))
|
64 |
-
draw = ImageDraw.Draw(img)
|
65 |
-
font = ImageFont.load_default()
|
66 |
-
draw.rectangle([50, 50, 1450, 950], outline="black", width=4)
|
67 |
-
draw.text((600, 100), "Dot-to-Dot Book", fill="black", font=font)
|
68 |
-
draw.text((580, 160), "For Kids Ages 4 to 12", fill="black", font=font)
|
69 |
-
draw.ellipse((1100, 700, 1300, 900), outline="gray", width=3)
|
70 |
-
draw.text((1120, 780), "Your Art Here", fill="gray", font=font)
|
71 |
-
capa_path = tempfile.NamedTemporaryFile(delete=False, suffix=".png").name
|
72 |
-
img.save(capa_path)
|
73 |
-
return capa_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
|
2 |
+
import gradio as gr
|
3 |
+
from livro import processar_e_mostrar, verificar_contraste, gerar_preview_kdp
|
4 |
+
|
5 |
+
with gr.Blocks() as demo:
|
6 |
+
with gr.Row():
|
7 |
+
imagem_input = gr.Image(label="Imagem Original", type="filepath")
|
8 |
+
mascara = gr.ImageEditor(label="Marcar áreas para preservar (opcional)")
|
9 |
+
with gr.Row():
|
10 |
+
idade = gr.Slider(minimum=4, maximum=12, step=1, value=6, label="Idade da criança (ajusta número de pontos)")
|
11 |
+
contraste_alerta = gr.Textbox(label="Aviso de Contraste", interactive=False)
|
12 |
+
with gr.Row():
|
13 |
+
imagem_resultado = gr.Image(label="Prévia com Pontos Numerados")
|
14 |
+
with gr.Row():
|
15 |
+
gerar_btn = gr.Button("Gerar Pontos")
|
16 |
+
salvar_btn = gr.Button("Salvar como PDF")
|
17 |
+
capa_btn = gr.Button("Gerar Preview da Capa")
|
18 |
+
saida_pdf = gr.File(label="Download do PDF")
|
19 |
+
saida_capa = gr.Image(label="Preview da Capa")
|
20 |
+
|
21 |
+
resultado = {}
|
22 |
+
|
23 |
+
def gerar(imagem_path, mask, idade):
|
24 |
+
global resultado
|
25 |
+
aviso = verificar_contraste(imagem_path)
|
26 |
+
resultado = processar_e_mostrar(imagem_path, mask, idade)
|
27 |
+
return aviso, resultado["preview"]
|
28 |
+
|
29 |
+
def salvar_pdf():
|
30 |
+
return resultado.get("pdf")
|
31 |
+
|
32 |
+
def gerar_capa():
|
33 |
+
return gerar_preview_kdp()
|
34 |
+
|
35 |
+
gerar_btn.click(gerar, inputs=[imagem_input, mascara, idade], outputs=[contraste_alerta, imagem_resultado])
|
36 |
+
salvar_btn.click(salvar_pdf, outputs=saida_pdf)
|
37 |
+
capa_btn.click(gerar_capa, outputs=saida_capa)
|
38 |
+
|
39 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
|
2 |
-
gradio
|
3 |
opencv-python
|
4 |
numpy
|
5 |
reportlab
|
6 |
pillow
|
7 |
-
|
|
|
1 |
|
2 |
+
gradio
|
3 |
opencv-python
|
4 |
numpy
|
5 |
reportlab
|
6 |
pillow
|
|