|
|
|
import cv2 |
|
import numpy as np |
|
from reportlab.pdfgen import canvas |
|
from reportlab.lib.units import inch |
|
import tempfile |
|
import os |
|
from PIL import Image, ImageDraw, ImageFont |
|
|
|
PAGE_WIDTH = 8.67 * inch |
|
PAGE_HEIGHT = 11.5 * inch |
|
MARGIN = 0.5 * inch |
|
|
|
def verificar_contraste(imagem_path): |
|
img = cv2.imread(imagem_path) |
|
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) |
|
contrast = gray.std() |
|
if contrast < 30: |
|
return "⚠️ Baixo contraste detectado! Use uma imagem mais nítida ou com fundo branco puro." |
|
return "✅ Contraste adequado." |
|
|
|
def detectar_pontos(imagem_path, mascara_path=None, idade=6): |
|
img = cv2.imread(imagem_path) |
|
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) |
|
_, thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY_INV) |
|
if mascara_path and os.path.exists(mascara_path): |
|
mask = cv2.imread(mascara_path, 0) |
|
thresh = cv2.bitwise_and(thresh, mask) |
|
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) |
|
if not contours: |
|
return [] |
|
|
|
main_contour = max(contours, key=cv2.contourArea) |
|
max_points = min(30 + (idade - 4) * 10, 150) |
|
approx = [tuple(pt[0]) for pt in main_contour[::max(1, len(main_contour)//max_points)]] |
|
return sorted(approx, key=lambda p: (p[1], p[0])) |
|
|
|
def gerar_preview_com_pontos(pontos): |
|
preview = np.ones((int(PAGE_HEIGHT), int(PAGE_WIDTH), 3), dtype=np.uint8) * 255 |
|
for i, (x, y) in enumerate(pontos): |
|
cv2.circle(preview, (x, y), 4, (0, 0, 0), -1) |
|
cv2.putText(preview, str(i+1), (x + 5, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 0), 1) |
|
preview_path = tempfile.NamedTemporaryFile(delete=False, suffix=".png").name |
|
cv2.imwrite(preview_path, preview) |
|
return preview_path |
|
|
|
def gerar_pdf(pontos): |
|
temp_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") |
|
c = canvas.Canvas(temp_pdf.name, pagesize=(PAGE_WIDTH, PAGE_HEIGHT)) |
|
for i, (x, y) in enumerate(pontos): |
|
c.circle(x, PAGE_HEIGHT - y, 2, fill=1) |
|
c.setFont("Helvetica", 8) |
|
c.drawString(x + 3, PAGE_HEIGHT - y + 3, str(i + 1)) |
|
c.save() |
|
return temp_pdf.name |
|
|
|
def processar_e_mostrar(imagem_path, mascara_path, idade): |
|
pontos = detectar_pontos(imagem_path, mascara_path, idade) |
|
preview = gerar_preview_com_pontos(pontos) |
|
pdf = gerar_pdf(pontos) |
|
return {"preview": preview, "pdf": pdf} |
|
|
|
def gerar_preview_kdp(): |
|
img = Image.new("RGB", (1500, 1000), color=(250, 240, 210)) |
|
draw = ImageDraw.Draw(img) |
|
font = ImageFont.load_default() |
|
draw.rectangle([50, 50, 1450, 950], outline="black", width=4) |
|
draw.text((600, 100), "Livro de Conectar Pontos", fill="black", font=font) |
|
draw.text((580, 160), "Para crianças de 4 a 12 anos", fill="black", font=font) |
|
draw.ellipse((1100, 700, 1300, 900), outline="gray", width=3) |
|
draw.text((1120, 780), "Sua Arte Aqui", fill="gray", font=font) |
|
|
|
capa_path = tempfile.NamedTemporaryFile(delete=False, suffix=".png").name |
|
img.save(capa_path) |
|
return capa_path |
|
|