-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbanner.py
68 lines (63 loc) · 2.22 KB
/
banner.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
import subprocess
from datetime import datetime
import time
from youtube import get_views
def text_wrap(text, font, max_width):
lines = []
# If the width of the text is smaller than image width
# we don't need to split it, just add it to the lines array
# and return
if font.getsize(text)[0] <= max_width:
lines.append(text)
else:
# split the line by spaces to get words
words = text.split(' ')
i = 0
# append every word to a line while its width is shorter than image width
while i < len(words):
line = ''
while i < len(words) and font.getsize(line + words[i])[0] <= max_width:
line = line + words[i] + " "
i += 1
if not line:
line = words[i]
i += 1
# when the line gets longer than the max width do not append the word,
# add the line to the lines array
lines.append(line)
return lines
def draw_text(dt_string):
x = 610
y = 130
views = get_views()
#print (views)
img = Image.open('bg.jpg')
image_size = img.size
font = ImageFont.truetype('Roboto-Bold.ttf', size=117)
text = "HOJE É "+ dt_string +". ESSE VIDEO TEM APROX " +str(views)+ " VIZUALIZAÇÕES. ESSA IMAGEM ALTERA A CADA 5 MINUTOS"
lines = text_wrap(text, font, image_size[0] - x)
line_height = font.getsize('hg')[1]
draw = ImageDraw.Draw(img)
for line in lines:
# draw the line on the image
draw.text((x, y), line, fill=(255,255,255,128), font=font)
# update the y position so that we can use it for next line
y = y + line_height
# save the image
thumb = "thumbnail.png"
img.save('thumbnail/'+ str(thumb), optimize=True)
cmd = "python3 thumb.py --video-id Al5W-wZ2FOs --file "+ "thumbnail/"+thumb
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
out, err = p.communicate()
print (out)
count = 0
while True:
now = datetime.now()
dt_string = now.strftime("%d-%m-%Y")
draw_text(dt_string)
count = count + 1
print (count)
time.sleep(300)