Created
February 9, 2022 12:47
-
-
Save tiagocordeiro/c4f49a574758d5207ed0bbb99dcd7545 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import click | |
import os | |
import json | |
from PIL import Image, ImageDraw | |
file = open("categorias.json") | |
data = json.load(file) | |
total_files = len(data) | |
files_count = 0 | |
arquivos_gerados = 0 | |
arquivos_existentes = 0 | |
with click.progressbar(length=total_files, show_eta=False) as bar: | |
for categoria in data: | |
imagem = Image.open("./categorias-bg.png") | |
draw = ImageDraw.Draw(imagem) | |
nome = categoria["name"].lower() | |
if os.path.isfile(f"./saida-with-img-wo-txt/{nome}.png"): | |
click.echo(click.style(f"\033[47G\033[0J Arquivo: {click.format_filename(nome + '.txt')} já existe", fg="red"), nl=False) | |
arquivos_existentes += 1 | |
else: | |
if os.path.isfile(f"./images/{nome}.png"): | |
imagem_produto_arquivo = Image.open(f"./images/{nome}.png") | |
baseh = 600 | |
hpercent = (baseh/float(imagem_produto_arquivo.size[1])) | |
wsize = int((float(imagem_produto_arquivo.size[0])*float(hpercent))) | |
imagem_produto_arquivo = imagem_produto_arquivo.resize((wsize,baseh), Image.ANTIALIAS) | |
imagem_pos = (imagem.size[1] - imagem_produto_arquivo.size[1]) / 2 | |
imagem_pos_h = (imagem.size[0] - imagem_produto_arquivo.size[0]) / 2 | |
imagem.paste(imagem_produto_arquivo, (int(imagem_pos_h), int(imagem_pos)), imagem_produto_arquivo) | |
imagem.save(f"./saida-with-img-wo-txt/{nome}.png") | |
click.echo(click.style(f"\033[47G\033[0J Arquivo: {click.format_filename(nome + '.txt')} gerado", fg="green"), nl=False) | |
arquivos_gerados += 1 | |
files_count += 1 | |
bar.update(1) | |
click.echo(click.style(f"\033[47G\033[0J"), nl=False) | |
click.echo(click.style(f"{arquivos_gerados} Foram gerados ", fg="green")) | |
click.echo(click.style(f"{arquivos_existentes} Já existem", fg="red")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment