Após executar a tarefa de conversão dos formatos de imagens para PNG (que postei aqui “Convertendo arquivos ‘.jpeg’, ‘.jpg’, ‘.webp’ e ‘.jfif’ para ‘.png’ com Python”), foi necessário cortar as imagens convertidas para o formato de foto 3×4…
🐍 As imagens convertidas para PNG ficaram assim:
🐍 Para cortá-las no formato 3×4, utilizei o seguinte script em Python:
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 |
import os from PIL import Image input_directory = 'D:/_TEMP/fotos/' output_directory = 'D:/_TEMP/fotos/3x4' cropping_ratio = 3 / 4 # Proporção 3x4 # Criar diretório de saída se não existir os.makedirs(output_directory, exist_ok=True) # Percorre todos os arquivos no diretório de entrada for filename in os.listdir(input_directory): if filename.lower().endswith(('.jpeg', '.jpg', '.webp', '.png', '.jfif')): # Caminhos completos dos arquivos de entrada e saída input_path = os.path.join(input_directory, filename) output_path = os.path.join(output_directory, f'{os.path.splitext(filename)[0]}_cortada.png') # Carrega a imagem with Image.open(input_path) as image: width, height = image.size # Calcula as dimensões do corte if width > height * cropping_ratio: target_width = height * cropping_ratio x1 = int((width - target_width) / 2) x2 = int(x1 + target_width) y1 = 0 y2 = height else: target_height = width / cropping_ratio y1 = int((height - target_height) / 2) y2 = int(y1 + target_height) x1 = 0 x2 = width # Realiza o corte aproximado cropped_image = image.crop((x1, y1, x2, y2)) cropped_image.save(output_path) |
—
🐍 E o resultado foi este:
*Atualizado em 26 de julho de 2023