-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbadge.py
178 lines (157 loc) · 5.58 KB
/
badge.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# -*- coding: utf-8 -*-
# Author: HexPandaa
# Inspired by: https://github.com/RemiGascou/small-projects/blob/master/Python/RootMe_badge/get_rootme_badge.py
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
from os.path import dirname, isdir, isfile, abspath
from os import makedirs
from typing import Union
import themes
class Badge:
THEMES = {
"light": themes.LightTheme,
"dark": themes.DarkTheme
}
FONT = "assets/BebasNeue-Regular.ttf"
def __init__(self,
pseudo: str,
profile_picture: str,
score: int,
title: str,
ranking: int,
total_users: int,
theme: str = "light",
width: int = 500,
height: int = 200
) -> None:
self.pseudo = pseudo
self.pp = profile_picture
self.score = score
self.title = title
self.ranking = ranking
self.total_users = total_users
self.theme = self.THEMES.get(theme, "light")
self.width = width
self.height = height
self.badge: Union[Image.Image, None] = None
@classmethod
def get_themes(cls):
return sorted(tuple(cls.THEMES.keys()))
def __draw_profile_picture(self) -> None:
pp: Image.Image = Image.open(self.pp)
size = int(self.height * (2/3)), int(self.height * (2/3)) # We want the pp to be 2/3 the height of the badge
pp = pp.resize(
size=size,
resample=Image.BICUBIC
# resample=Image.NEAREST
)
# We want the pp's top left corner to be at an offset from the top left corner of the badge
offset = int(self.height * 0.1), int(self.height * 0.1)
if pp.mode == "RGBA":
self.badge.paste(pp, offset, mask=pp)
else:
self.badge.paste(pp, offset)
def __draw_username(self) -> None:
size = int(self.height * 0.15)
font = ImageFont.truetype(self.FONT, size=size)
d = ImageDraw.Draw(self.badge)
offset = (
int(self.height * (2/3)) + int(self.height * 0.2), # The size of the pp, plus spacing between the objects
int(self.height * 0.1) # Spacing from the top of the badge
)
d.text(
xy=offset,
text=self.pseudo,
fill=self.theme.username_color,
font=font
)
def __draw_points(self) -> None:
size = int(self.height * 0.10)
font = ImageFont.truetype(self.FONT, size=size)
d = ImageDraw.Draw(self.badge)
offset = (
int(self.height * (2/3)) + int(self.height * 0.2), # The size of the pp, plus spacing between the objects
int(self.height * 0.35) # Spacing from the top of the badge + size of the username + padding
)
d.text(
xy=offset,
text=f"{self.score} pts",
fill=self.theme.score_color,
font=font
)
def __draw_logo(self) -> None:
size = int(self.height * (1/3)), int(self.height * (1/3))
logo: Image.Image = Image.open(self.theme.logo)
logo = logo.resize(
size=size,
resample=Image.BICUBIC
)
offset = (
int(self.width - size[0] - self.height * 0.1),
int(self.height * 0.1)
)
self.badge.paste(
im=logo,
box=offset,
mask=logo
)
def __draw_title(self) -> None:
size = int(self.height * 0.15)
font = ImageFont.truetype(self.FONT, size=size)
d = ImageDraw.Draw(self.badge)
offset = (
int(self.height * 0.1), # Spacing from the left side of the badge
int(self.height * 0.8) # Spacing from the bottom of the badge
)
d.text(
xy=offset,
text=self.title,
fill=self.theme.title_color,
font=font
)
def __draw_ranking(self) -> None:
size = int(self.height * 0.10)
font = ImageFont.truetype(self.FONT, size=size)
d = ImageDraw.Draw(self.badge)
offset = (
int(self.height * (2 / 3)) + int(self.height * 0.2), # The size of the pp, plus spacing between the objects
int(self.height * 0.50) # Spacing from the top of the badge + size of the username + padding
)
ranking = int(self.ranking)
total = int(self.total_users)
text = f"{ranking}/{total}"
if ranking != 0 and total != 0:
text += f" (Top {ranking / total * 100:.2f}%)"
d.text(
xy=offset,
text=text,
fill=self.theme.ranking_color,
font=font
)
def create(self) -> Image.Image:
self.badge = Image.new(
mode="RGB",
size=(self.width, self.height),
color=self.theme.background_color
)
if isfile(self.pp):
self.__draw_profile_picture()
self.__draw_username()
self.__draw_points()
self.__draw_logo()
self.__draw_title()
self.__draw_ranking()
return self.badge
def save(self, filepath: str) -> None:
filepath = abspath(filepath)
if not isdir(dirname(filepath)):
raise IOError(f"The folder does not exist: '{dirname(filepath)}'")
if self.badge is None:
self.create()
self.badge.save(filepath)
print(f"Badge saved in {filepath}.")
def show(self, viewer: str = None) -> None:
if self.badge is None:
self.create()
self.badge.show(command=viewer)