-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWallpaperCreator.py
157 lines (104 loc) · 4.03 KB
/
WallpaperCreator.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
from PIL import Image, ImageFont, ImageDraw
from screeninfo import get_monitors
import os
class Creator:
def __init__(self,
Text = "Test",
FontSize=100,
AuthorFontSize = 80,
FontType="Jellyka_Estrya_Handwriting.ttf",
FontColor=(255,255,255),
BackgroundColor = "black",
Path = "/home/" + os.environ["USER"] + "/Pictures/VisualQuoteWallpaper.png"):
self.__Text = ""
self.__Author = ""
self.Text = Text
self.__FontSize = FontSize
self.__AuthorFontSize = AuthorFontSize
self.__FontType = FontType
self.__FontColor = FontColor
self.__BackgroundColor = BackgroundColor
self.__WallpaperSize = self.AutoSize()
self.__img = None
self.__path = Path
self.CreateWallpaper()
@property
def FontSize(self):
return self.__FontSize
@FontSize.setter
def FontSize(self, value):
self.__FontSize = value
@property
def AuthorFontSize(self):
return self.__AuthorFontSize
@AuthorFontSize.setter
def AuthorFontSize(self, value):
self.__AuthorFontSize = value
@property
def FontType(self):
return self.__FontType
@FontType.setter
def FontType(self, value):
self.__FontType = value
@property
def WallpaperSize(self):
return self.__WallpaperSize
@WallpaperSize.setter
def WallpaperSize(self, value):
if isinstance(value, tuple):
if len(value) == 2:
self.__WallpaperSize = value
return self.__WallpaperSize
raise ValueError("WallpaperSize must be tuple with length 2.")
@property
def Text(self):
return self.__Text
@Text.setter
def Text(self, value):
self.__Text, self.__Author = self.__FormatText(value)
def AutoSize(self):
resolution = get_monitors()[0]
self.__WallpaperSize = (resolution.width, resolution.height)
return resolution.width, resolution.height
def __FormatText(self, text):
text, author = text.split("--")
text = text.split(" ")
formated_text = ""
for i, token in enumerate(text, start=1):
formated_text += token
if i % 15:
formated_text += " "
continue
formated_text += "\n"
return formated_text, author
def __TextSize(self, draw, text, fnt):
return draw.textsize(text, font=fnt)
def __Center(self, size):
w, h = size
width, height = self.__WallpaperSize
x = width/2 - w/2
y = height/2 - h/2
return int(x), int(y)
def __PositionAuthor(self, text_size, author_size, offset=0.1):
text_x, text_y = self.__Center(text_size)
text_width, text_height = text_size
author_width, author_height = author_size
author_x = (text_x + text_width) - int((text_x + text_width)*offset) - author_width
author_y = (text_y + text_height) + 10
return int(author_x), int(author_y)
def CreateWallpaper(self):
resolution = get_monitors()[0]
width, height = self.__WallpaperSize
img = Image.new("RGB", self.__WallpaperSize, color=self.__BackgroundColor)
d = ImageDraw.Draw(img)
text_fnt = ImageFont.truetype(self.__FontType, self.__FontSize)
text_size = self.__TextSize(d, self.__Text, text_fnt)
d.text(self.__Center(text_size), self.__Text, font=text_fnt, fill=self.__FontColor)
author_fnt = ImageFont.truetype(self.__FontType, self.__AuthorFontSize)
author_size = self.__TextSize(d, self.__Author, author_fnt)
d.text(self.__PositionAuthor(text_size, author_size), self.__Author, font=author_fnt, fill=self.__FontColor)
self.__img = img
def SaveWallpaper(self):
self.__img.save(self.__path)
def ShowWallpaper(self):
self.__img.show()