-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_formatter.py
More file actions
53 lines (46 loc) · 2.24 KB
/
image_formatter.py
File metadata and controls
53 lines (46 loc) · 2.24 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from PIL import Image, ImageDraw, ImageFont
from typing import List
from textwrap import wrap
class ImageFormatter:
def __init__(self, tweets: List[str], image_path: str):
self.tweets = tweets # lista de tweets que serão escritos na imagem
self.image_path = image_path # caminho da imagem
# cria uma nova imagem com tamanho 800x800 e cor de fundo
self.img = Image.new('RGB', (800, 600), color=(73, 109, 137))
# cria um objeto para desenhar na imagem
self.d = ImageDraw.Draw(self.img)
# define a fonte e tamanho do texto
self.font = ImageFont.truetype('arial.ttf', 24)
def create_image(self):
text_y = 50 # posição inicial do texto na imagem
for tweet in self.tweets:
text = tweet
# divide o texto em linhas com no máximo 40 caracteres
wrapped_text = wrap(text, width=40)
for line in wrapped_text:
self.d.text((50, text_y), line, font=self.font,
fill=(255, 255, 255)) # escreve o texto na imagem
text_y += 30 # incrementa a posição do texto na imagem
# desenha uma linha abaixo do texto
self.d.line((50, text_y, 750, text_y), fill=(255, 255, 255))
text_y += 10
self.img.save(self.image_path) # salva a imagem
def format_image(self, css_path: str):
with open(css_path, 'r') as css:
styles = css.read()
# Get the font-family from the CSS file
font_url = styles.split("src: url(/")[1].split(")")[0].strip()
# Get the font-size from the CSS file
font_size = int(styles.split("font-size:")
[1].split(";")[0].strip().replace("px", ""))
# Get the font-color from the CSS file
font_color = styles.split("color:")[1].split(";")[0].strip()
# print(font_url)
# print(font_size)
# print(font_color)
# Update the font object with the new font-family and font-size
self.font = ImageFont.truetype(font_url, font_size)
self.d.textfont = self.font
self.d.fill = font_color
# save imag
self.img.save(self.image_path)