|
|
|
|
|
livro_limpo = ''' |
|
import cv2 |
|
import numpy as np |
|
from reportlab.pdfgen import canvas |
|
from reportlab.lib.units import inch |
|
import tempfile |
|
from PIL import Image, ImageDraw, ImageFont |
|
|
|
PAGE_WIDTH = 8.67 * inch |
|
PAGE_HEIGHT = 11.5 * inch |
|
MARGIN = 0.5 * inch |
|
|
|
def verificar_contraste(imagem_array): |
|
gray = cv2.cvtColor(imagem_array, cv2.COLOR_BGR2GRAY) |
|
contrast = gray.std() |
|
if contrast < 30: |
|
return "⚠️ Baixo contraste detectado! Use uma imagem com contorno escuro e fundo branco puro." |
|
return "✅ Contraste adequado." |
|
|
|
def detectar_pontos(imagem_array, mascara_array=None, idade=6): |
|
gray = cv2.cvtColor(imagem_array, cv2.COLOR_BGR2GRAY) |
|
_, thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY_INV) |
|
|
|
if mascara_array is not None and isinstance(mascara_array, np.ndarray): |
|
try: |
|
mask = cv2.cvtColor(mascara_array, cv2.COLOR_BGR2GRAY) |
|
_, mask_thresh = cv2.threshold(mask, 10, 255, cv2.THRESH_BINARY) |
|
thresh = cv2.bitwise_and(thresh, mask_thresh) |
|
except Exception as e: |
|
print("Erro ao processar máscara:", e) |
|
|
|
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): |
|
largura = 600 |
|
altura = 800 |
|
margem = 50 |
|
|
|
preview = np.ones((altura, largura, 3), dtype=np.uint8) * 255 |
|
|
|
if not pontos: |
|
return None |
|
|
|
x_coords, y_coords = zip(*pontos) |
|
min_x, max_x = min(x_coords), max(x_coords) |
|
min_y, max_y = min(y_coords), max(y_coords) |
|
|
|
escala_x = (largura - 2 * margem) / (max_x - min_x + 1e-5) |
|
escala_y = (altura - 2 * margem) / (max_y - min_y + 1e-5) |
|
escala = min(escala_x, escala_y) |
|
|
|
pontos_normalizados = [( |
|
int((x - min_x) * escala + margem), |
|
int((y - min_y) * escala + margem) |
|
) for x, y in pontos] |
|
|
|
for i, (x, y) in enumerate(pontos_normalizados): |
|
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_array, mascara_array, idade): |
|
pontos = detectar_pontos(imagem_array, mascara_array, 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), "Dot-to-Dot Book", fill="black", font=font) |
|
draw.text((580, 160), "For Kids Ages 4 to 12", fill="black", font=font) |
|
draw.ellipse((1100, 700, 1300, 900), outline="gray", width=3) |
|
draw.text((1120, 780), "Your Art Here", fill="gray", font=font) |
|
capa_path = tempfile.NamedTemporaryFile(delete=False, suffix=".png").name |
|
img.save(capa_path) |
|
return capa_path |
|
''' |
|
|
|
|
|
livro_path_final = "/mnt/data/livro.py" |
|
with open(livro_path_final, "w", encoding="utf-8") as f: |
|
f.write(livro_limpo) |
|
|
|
livro_path_final |
|
|