Skip to content

Instantly share code, notes, and snippets.

@PaulloClara
Created February 4, 2024 23:34
Show Gist options
  • Save PaulloClara/70712514d8cafe7e19e6b82315642ca6 to your computer and use it in GitHub Desktop.
Save PaulloClara/70712514d8cafe7e19e6b82315642ca6 to your computer and use it in GitHub Desktop.
Convert .avif to .png with Pillow
# Instalar: pip install pillow-avif-plugin Pillow
import os
from PIL import Image
import pillow_avif
def convert_avif_to_png(input_dir, output_dir):
# Verifica se o diretório de saída existe, se não, cria-o
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Lista todos os arquivos no diretório de entrada
image_files = os.listdir(input_dir)
# Loop através de cada arquivo no diretório de entrada
for file_name in image_files:
if file_name.lower().endswith('.avif'):
input_path = os.path.join(input_dir, file_name)
output_path = os.path.join(output_dir, os.path.splitext(file_name)[0] + '.png')
# Abre a imagem usando a biblioteca Pillow
img = Image.open(input_path)
# Converte e salva a imagem no formato PNG
img.save(output_path, 'PNG')
print(f'Conversão de {input_path} para {output_path} concluída.')
# Diretório de entrada das imagens
input_directory = './input'
# Diretório de saída das imagens convertidas
output_directory = './output'
convert_avif_to_png(input_directory, output_directory)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment