Files
Logo-Imagenes/main.py
T
2026-07-20 14:42:39 +02:00

87 lines
2.9 KiB
Python

import os
import datetime
from PIL import Image, ExifTags
logo_ver = '/home/jesusjmma/Documents/Logos/CEEBI/CEEBI/Logo CEEBI vertical blanco.png'
logo_hor = './logo.png'
logo_file = logo_hor
logoIm = Image.open(logo_file)
initlogoWidth, initlogoHeight = logoIm.size
logoWidth, logoHeight = initlogoWidth, initlogoHeight
Image.MAX_IMAGE_PIXELS = None
os.makedirs('conlogo', exist_ok = True)
def get_unique_filename(directorio, filename):
"""
Genera un nombre de archivo único si el archivo ya existe en el directorio especificado.
"""
base, extension = os.path.splitext(filename)
counter = 1
temp_filename = datetime.datetime.fromtimestamp(os.path.getmtime(folder+"/"+filename)).strftime('%Y%m%d_%H%M%S') + extension
end_dir = 'conlogo/' + folder.replace('withoutlogo/', '', 1).replace('withoutlogo', '', 1)
if not os.path.exists(end_dir):
os.makedirs(end_dir)
base_filename = os.path.splitext(temp_filename)[0]
new_filename = f"{base_filename}{extension}"
while os.path.isfile(os.path.join(end_dir, new_filename)):
new_filename = f"{base_filename}_{counter}{extension}"
counter += 1
return os.path.join(end_dir, new_filename)
i = 1
for folder,subfolder,files in os.walk('withoutlogo'):
for filename in files:
print(i)
i += 1
logoWidth, logoHeight = initlogoWidth, initlogoHeight
if not (filename.endswith('.png') or filename.endswith('.jpg') or filename.endswith('.jpeg') or filename.endswith('.JPEG') or filename.endswith('.JPG')) or filename == logo_file:
continue
try:
im = Image.open(folder+"/"+filename)
except Exception as e:
print(e)
continue
try:
for orientation in ExifTags.TAGS.keys():
if ExifTags.TAGS[orientation] == 'Orientation':
break
exif = im._getexif()
if exif[orientation] == 3:
im = im.rotate(180, expand=True)
elif exif[orientation] == 6:
im = im.rotate(270, expand=True)
elif exif[orientation] == 8:
im = im.rotate(90, expand=True)
except Exception as e:
print(e)
width, height = im.size
sq_fit_size = int(0.35 * min(width, height))
margen = int(0.035 * min(width, height))
if logoWidth > sq_fit_size or logoHeight > sq_fit_size:
if logoWidth > logoHeight:
logoHeight = int((sq_fit_size / logoWidth) * logoHeight)
logoWidth = sq_fit_size
else:
logoWidth = int((sq_fit_size / logoHeight) * logoWidth)
logoHeight = sq_fit_size
logoIm = logoIm.resize((logoWidth, logoHeight))
im.paste(logoIm, (margen, height - logoHeight - margen), logoIm)
new_filename = get_unique_filename(folder, filename)
im.save(new_filename)