-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
imagecreator.py
60 lines (46 loc) · 2.2 KB
/
imagecreator.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
from PIL import Image, ImageDraw, ImageSequence,ImageFont
import random
import os
import textwrap
gifs = os.listdir("gokugifs")
strings = {
"en": {
"rule": "Rule ",
},
"tr": {
"rule": "Kural ",
}
}
def writeText(gif, ruleid :int , text:str,filename:str = "rule",lang = "en"):
frames = []
gif.seek(0) # go to the first frame of the gif
frame = ImageDraw.Draw(gif) # create a draw object to draw on the gif
font = ImageFont.truetype("impact.ttf", int(gif.width / 10) ) # create a font object
lines = textwrap.wrap(text, width=int(gif.width / 20)) #we wrap the text to fit the width of the gif
_, _, ruleWidth, _ = frame.textbbox((0, 0), f"{strings[lang]['rule']} {str(ruleid)}",font=font) # calculate the width and height of the text
for frame in ImageSequence.Iterator(gif):
frame = frame.convert("RGBA")
newImage = []
for item in frame.getdata():
if item[3] == 0:
newImage.append((54, 57, 63, 100)) # discord background color
else:
newImage.append(item)
frame.putdata(newImage)
draw = ImageDraw.Draw(frame,"RGBA")
width = frame.width
height = frame.height
for x,y in [(0,1),(0,-1),(1,0),(-1,0),(0,0)]:
draw.text(((width - ruleWidth) / 2 + x ,10 + y), f"{strings[lang]['rule']} {str(ruleid)}" , fill="white" if x == 0 and y == 0 else "black" ,font=font)
for i, line in enumerate(lines[::-1]):
_, _, descriptionWidth, descriptionHeight = draw.textbbox((0, 0), line,font=font) # calculate the width and height of the text
for x,y in [(0,1),(0,-1),(1,0),(-1,0),(0,0)]:
draw.text(((width - descriptionWidth) / 2 + x ,height - 5 - ((i + 1) * descriptionHeight + y)), line, fill="white" if x == 0 and y == 0 else "black",font=font)
frames.append(frame)
frames[0].save(filename + ".gif", save_all=True, append_images=frames[1:],duration=gif.info["duration"],optimize = True)
return filename + ".gif"
if __name__ == "__main__":
gif = random.choice(gifs)
print(gif)
writeText(Image.open("gokugifs/" + gif),1,"adsadlas",lang="en")
os.system("rule.gif")