Upload 3 files
Browse files- app.py +39 -0
- livro.py +75 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
livro.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
from reportlab.pdfgen import canvas
|
5 |
+
from reportlab.lib.units import inch
|
6 |
+
import tempfile
|
7 |
+
import os
|
8 |
+
from PIL import Image, ImageDraw, ImageFont
|
9 |
+
|
10 |
+
PAGE_WIDTH = 8.67 * inch
|
11 |
+
PAGE_HEIGHT = 11.5 * inch
|
12 |
+
MARGIN = 0.5 * inch
|
13 |
+
|
14 |
+
def verificar_contraste(imagem_path):
|
15 |
+
img = cv2.imread(imagem_path)
|
16 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
17 |
+
contrast = gray.std()
|
18 |
+
if contrast < 30:
|
19 |
+
return "⚠️ Baixo contraste detectado! Use uma imagem mais nítida ou com fundo branco puro."
|
20 |
+
return "✅ Contraste adequado."
|
21 |
+
|
22 |
+
def detectar_pontos(imagem_path, mascara_path=None, idade=6):
|
23 |
+
img = cv2.imread(imagem_path)
|
24 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
25 |
+
_, thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY_INV)
|
26 |
+
if mascara_path and os.path.exists(mascara_path):
|
27 |
+
mask = cv2.imread(mascara_path, 0)
|
28 |
+
thresh = cv2.bitwise_and(thresh, mask)
|
29 |
+
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
|
30 |
+
if not contours:
|
31 |
+
return []
|
32 |
+
|
33 |
+
main_contour = max(contours, key=cv2.contourArea)
|
34 |
+
max_points = min(30 + (idade - 4) * 10, 150) # Ex: idade 4 -> 30 pts, 12 -> 110 pts
|
35 |
+
approx = [tuple(pt[0]) for pt in main_contour[::max(1, len(main_contour)//max_points)]]
|
36 |
+
return sorted(approx, key=lambda p: (p[1], p[0]))
|
37 |
+
|
38 |
+
def gerar_preview_com_pontos(pontos):
|
39 |
+
preview = np.ones((int(PAGE_HEIGHT), int(PAGE_WIDTH), 3), dtype=np.uint8) * 255
|
40 |
+
for i, (x, y) in enumerate(pontos):
|
41 |
+
cv2.circle(preview, (x, y), 4, (0, 0, 0), -1)
|
42 |
+
cv2.putText(preview, str(i+1), (x + 5, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 0), 1)
|
43 |
+
preview_path = tempfile.NamedTemporaryFile(delete=False, suffix=".png").name
|
44 |
+
cv2.imwrite(preview_path, preview)
|
45 |
+
return preview_path
|
46 |
+
|
47 |
+
def gerar_pdf(pontos):
|
48 |
+
temp_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
|
49 |
+
c = canvas.Canvas(temp_pdf.name, pagesize=(PAGE_WIDTH, PAGE_HEIGHT))
|
50 |
+
for i, (x, y) in enumerate(pontos):
|
51 |
+
c.circle(x, PAGE_HEIGHT - y, 2, fill=1)
|
52 |
+
c.setFont("Helvetica", 8)
|
53 |
+
c.drawString(x + 3, PAGE_HEIGHT - y + 3, str(i + 1))
|
54 |
+
c.save()
|
55 |
+
return temp_pdf.name
|
56 |
+
|
57 |
+
def processar_e_mostrar(imagem_path, mascara_path, idade):
|
58 |
+
pontos = detectar_pontos(imagem_path, mascara_path, idade)
|
59 |
+
preview = gerar_preview_com_pontos(pontos)
|
60 |
+
pdf = gerar_pdf(pontos)
|
61 |
+
return {"preview": preview, "pdf": pdf}
|
62 |
+
|
63 |
+
def gerar_preview_kdp():
|
64 |
+
img = Image.new("RGB", (1500, 1000), color=(250, 240, 210))
|
65 |
+
draw = ImageDraw.Draw(img)
|
66 |
+
font = ImageFont.load_default()
|
67 |
+
draw.rectangle([50, 50, 1450, 950], outline="black", width=4)
|
68 |
+
draw.text((600, 100), "Livro de Conectar Pontos", fill="black", font=font)
|
69 |
+
draw.text((580, 160), "Para crianças de 4 a 12 anos", fill="black", font=font)
|
70 |
+
draw.ellipse((1100, 700, 1300, 900), outline="gray", width=3)
|
71 |
+
draw.text((1120, 780), "Sua Arte Aqui", fill="gray", font=font)
|
72 |
+
|
73 |
+
capa_path = tempfile.NamedTemporaryFile(delete=False, suffix=".png").name
|
74 |
+
img.save(capa_path)
|
75 |
+
return capa_path
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
gradio
|
3 |
+
opencv-python
|
4 |
+
numpy
|
5 |
+
reportlab
|
6 |
+
pillow
|