114 lines
3.5 KiB
Python
114 lines
3.5 KiB
Python
# Genera códigos QR para el sistema de mantenimiento del campamento.
|
|
# Mismo formato visual que la versión anterior: color #132A7C, logo NEM.png,
|
|
# pie de foto "Módulo | Habitación".
|
|
import csv
|
|
import qrcode
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
import os
|
|
import re
|
|
|
|
CSV_FILE = "qr_data.csv"
|
|
LOGO_FILE = "NEM.png"
|
|
OUTPUT_DIR = "output"
|
|
QR_COLOR = "#132A7C"
|
|
BG_COLOR = "#FFFFFF"
|
|
QR_SIZE = 400
|
|
LOGO_SIZE = 80
|
|
FOOTER_HEIGHT = 60
|
|
PADDING = 20
|
|
|
|
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
|
|
|
if not os.path.exists(CSV_FILE):
|
|
print(f"Error: no se encontró '{CSV_FILE}'.")
|
|
print("Use Admin → Códigos QR → Generar todos, o exporte CSV desde el panel.")
|
|
raise SystemExit(1)
|
|
|
|
logo = None
|
|
if os.path.exists(LOGO_FILE):
|
|
logo = Image.open(LOGO_FILE).convert("RGBA")
|
|
logo.thumbnail((LOGO_SIZE, LOGO_SIZE), Image.LANCZOS)
|
|
logo_bg = Image.new("RGBA", (logo.width + 12, logo.height + 12), "white")
|
|
logo_bg.paste(logo, (6, 6), logo)
|
|
logo = logo_bg
|
|
else:
|
|
print("Advertencia: NEM.png no encontrado. QR sin logo.")
|
|
|
|
try:
|
|
font = ImageFont.truetype("/System/Library/Fonts/Helvetica.ttc", 22)
|
|
except Exception:
|
|
try:
|
|
font = ImageFont.truetype("/Library/Fonts/Arial.ttf", 22)
|
|
except Exception:
|
|
font = ImageFont.load_default()
|
|
|
|
|
|
def safe_filename(text):
|
|
return re.sub(r'[\\/:*?"<>|]', "-", text)
|
|
|
|
|
|
def generate_qr(url, filename, caption_modulo, caption_ubicacion):
|
|
qr = qrcode.QRCode(
|
|
version=None,
|
|
error_correction=qrcode.constants.ERROR_CORRECT_H,
|
|
box_size=10,
|
|
border=4,
|
|
)
|
|
qr.add_data(url)
|
|
qr.make(fit=True)
|
|
|
|
qr_img = qr.make_image(fill_color=QR_COLOR, back_color=BG_COLOR).convert("RGB")
|
|
qr_img = qr_img.resize((QR_SIZE, QR_SIZE), Image.LANCZOS)
|
|
|
|
if logo:
|
|
qr_img_rgba = qr_img.convert("RGBA")
|
|
logo_x = (QR_SIZE - logo.width) // 2
|
|
logo_y = (QR_SIZE - logo.height) // 2
|
|
qr_img_rgba.paste(logo, (logo_x, logo_y), logo)
|
|
qr_img = qr_img_rgba
|
|
|
|
total_width = QR_SIZE + PADDING * 2
|
|
total_height = QR_SIZE + FOOTER_HEIGHT + PADDING * 2
|
|
|
|
final = Image.new("RGB", (total_width, total_height), BG_COLOR)
|
|
final.paste(qr_img.convert("RGB"), (PADDING, PADDING))
|
|
|
|
draw = ImageDraw.Draw(final)
|
|
caption = f"{caption_modulo} | {caption_ubicacion}"
|
|
bbox = draw.textbbox((0, 0), caption, font=font)
|
|
text_w = bbox[2] - bbox[0]
|
|
text_h = bbox[3] - bbox[1]
|
|
text_x = (total_width - text_w) // 2
|
|
text_y = QR_SIZE + PADDING + (FOOTER_HEIGHT - text_h) // 2
|
|
draw.text((text_x, text_y), caption, fill=QR_COLOR, font=font)
|
|
|
|
filepath = os.path.join(OUTPUT_DIR, filename)
|
|
final.save(filepath, "PNG")
|
|
return filepath
|
|
|
|
|
|
count = 0
|
|
used_filenames = set()
|
|
with open(CSV_FILE, newline="", encoding="utf-8") as f:
|
|
reader = csv.DictReader(f)
|
|
for row in reader:
|
|
codigo = row["codigo"].strip()
|
|
modulo = row["modulo"].strip()
|
|
ubicacion = row["ubicacion"].strip()
|
|
url = row["url"].strip()
|
|
|
|
if not url or not codigo:
|
|
continue
|
|
|
|
base_name = f"{safe_filename(modulo)}_{safe_filename(ubicacion)}"
|
|
filename = f"{base_name}.png"
|
|
if filename in used_filenames:
|
|
filename = f"{base_name}_{safe_filename(codigo)}.png"
|
|
used_filenames.add(filename)
|
|
|
|
generate_qr(url, filename, modulo, ubicacion)
|
|
count += 1
|
|
if count % 100 == 0:
|
|
print(f"Progreso: {count} QR generados...")
|
|
|
|
print(f"\nListo! {count} códigos QR guardados en '{OUTPUT_DIR}/'")
|