Last active
February 15, 2023 16:34
-
-
Save gcrsaldanha/c2c2a7bcb745ee4882eb08bbfe13ac36 to your computer and use it in GitHub Desktop.
Criando imagens com Python (Pillow)
This file contains 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
# Para mais conteúdos de Python, acesse: https://instagram.com/gabrielsaldanha.dev | |
# É necessário ter Pillow instalado: | |
# $ python3 -m pip install Pillow | |
from PIL import Image, ImageDraw, ImageFont | |
image = Image.new("RGB", (1080, 1080), color=(41, 46, 48)) | |
img_draw = ImageDraw.Draw(image) | |
# Arquivo de fonte Helvetica.ttc deve estar no mesmo diretório | |
# que o programa está sendo executado | |
font = ImageFont.truetype("Helvetica.ttc", size=72) | |
def main(): | |
title = "Essa imagem\n\nfoi feita\n\nem Python" | |
img_draw.text((300, 405), title, font=font, fill=(234, 234, 235)) | |
seq = log_seq(end=270, points=30) | |
for i in seq: | |
xy0 = (i, i) | |
xy1 = 1080 - i - 1, 1080 - i - 1 | |
img_draw.rectangle([xy0, xy1], width=2) | |
image.save("img.png") | |
def log_seq(end, points): | |
""" | |
Retorna uma sequência de pontos normalizada em relação a `end`. | |
O tamanho da sequência é determinada por `points`. | |
Utilizamos a função logarítmica para obter a distância entre os pontos. | |
""" | |
from math import log | |
seq = [] | |
for i in range(points): | |
seq.append(log(i + 1)) | |
norm_factor = end / seq[i] # Normaliza os valores | |
return [norm_factor * point for point in seq] | |
main() |
hyddvbytff
commented
Feb 15, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment