-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_batch.py
More file actions
126 lines (100 loc) · 4.84 KB
/
process_batch.py
File metadata and controls
126 lines (100 loc) · 4.84 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python3
"""
Script para procesar múltiples imágenes organizadas por producto
"""
import sys
import re
sys.path.insert(0, '/home/pragma/development/perras-underwear-img-with-logos')
from process_images import ImageProcessor
# Lista de URLs
urls = [
"https://acdn-us.mitiendanube.com/stores/001/969/453/products/body-tokio2-ma-1200-0ec53d51d0e06a585b17581451545899-1024-1024.webp",
"https://acdn-us.mitiendanube.com/stores/001/969/453/products/body-tokio-ma-e93599cf5c017be21517581451550367-1024-1024.webp",
"https://acdn-us.mitiendanube.com/stores/001/969/453/products/charo-blanco-3-a4837ab00cce17f1d017653057076387-1024-1024.webp",
"https://acdn-us.mitiendanube.com/stores/001/969/453/products/charo-blanco-medias-2-a843cbf61581d1712917653057075825-1024-1024.webp",
"https://acdn-us.mitiendanube.com/stores/001/969/453/products/charo-negro-medias-1-909cc1a4357678fde817653063530576-1024-1024.webp",
"https://acdn-us.mitiendanube.com/stores/001/969/453/products/diseno-sin-titulo-41-e6ada36eac1805624617065444344714-1024-1024.webp",
"https://acdn-us.mitiendanube.com/stores/001/969/453/products/tokio-3-ma-1200-ce4b86c579f4c5946b17044859181489-1024-1024.webp",
"https://acdn-us.mitiendanube.com/stores/001/969/453/products/tokio-8-ma-1200-3cebb90c832c78275b17044859184013-1024-1024.webp",
]
def extract_product_name(url):
"""
Extrae el nombre del producto de la URL.
Ejemplo: 'body-tokio2-ma-1200-0ec53d51d0e06a585b17581451545899-1024-1024.webp'
-> 'body-tokio2'
"""
# Obtener el nombre del archivo
filename = url.split('/')[-1]
# Remover la extensión
filename_no_ext = filename.rsplit('.', 1)[0]
# Patrón: nombre-del-producto-[hash]-[dimensiones]
# Buscamos todo antes del primer hash (secuencia larga de letras/números)
match = re.match(r'^(.+?)-[a-f0-9]{32,}-\d+-\d+$', filename_no_ext)
if match:
product_name = match.group(1)
else:
# Si no coincide el patrón, usar todo antes del último guion con números
parts = filename_no_ext.split('-')
# Encontrar donde empiezan los hashes/números largos
product_parts = []
for part in parts:
# Si es un hash largo o dimensiones, parar
if len(part) > 10 and part.isalnum():
break
if part.isdigit() and len(part) == 4: # dimensiones como 1024
break
product_parts.append(part)
product_name = '-'.join(product_parts) if product_parts else 'producto'
return product_name
def main():
# Configuración
logo_path = "/home/pragma/.gemini/antigravity/brain/ce5903a1-f849-4b4e-a013-135db996cdf6/uploaded_image_1766786140416.png"
print("=" * 70)
print("PROCESAMIENTO POR LOTES DE IMÁGENES")
print("=" * 70)
print(f"\nTotal de imágenes a procesar: {len(urls)}\n")
# Agrupar URLs por producto
products = {}
for url in urls:
product_name = extract_product_name(url)
if product_name not in products:
products[product_name] = []
products[product_name].append(url)
print(f"Productos detectados: {len(products)}")
for product_name, product_urls in products.items():
print(f" - {product_name}: {len(product_urls)} imagen(es)")
print("\n" + "=" * 70)
# Procesar cada producto
total_processed = 0
total_errors = 0
for product_name, product_urls in products.items():
print(f"\n📦 Procesando producto: {product_name}")
print("-" * 70)
# Crear procesador con directorio específico para este producto
processor = ImageProcessor(logo_path, output_dir=f"productos/{product_name}")
for i, url in enumerate(product_urls, 1):
try:
# Extraer nombre de archivo original
original_filename = url.split('/')[-1]
output_filename = original_filename.replace('.webp', '.png')
print(f"\n [{i}/{len(product_urls)}] Procesando: {original_filename}")
output_path = processor.process_image(url, output_filename)
print(f" ✓ Guardado en: {output_path}")
total_processed += 1
except Exception as e:
print(f" ✗ Error: {e}")
total_errors += 1
# Resumen final
print("\n" + "=" * 70)
print("RESUMEN FINAL")
print("=" * 70)
print(f"✓ Imágenes procesadas exitosamente: {total_processed}")
if total_errors > 0:
print(f"✗ Errores: {total_errors}")
print(f"📁 Productos organizados: {len(products)}")
print("\nDirectorios creados:")
for product_name in products.keys():
print(f" - productos/{product_name}/")
print("=" * 70)
if __name__ == "__main__":
main()