File size: 4,025 Bytes
0a94767 0e2c31f 0a94767 a4b6d66 65fb503 a4b6d66 65fb503 a4b6d66 65fb503 a4b6d66 65fb503 9d02d62 65fb503 a4b6d66 65fb503 a4b6d66 0e2c31f a4b6d66 0e2c31f a4b6d66 65fb503 a4b6d66 65fb503 a4b6d66 65fb503 a4b6d66 0e2c31f 0a94767 0e2c31f 0a94767 |
1 2 3 4 5 6 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# Gerar versão final limpa do livro.py sem instruções de escrita de arquivo
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
'''
# Salvar como novo arquivo pronto para upload no Hugging Face
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
|