/
archur.py
executable file
·111 lines (82 loc) · 4.02 KB
/
archur.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
#!/usr/bin/env python3
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
from PIL import ImageOps
from PIL import ImageColor
import random
import argparse
import os
DEFAULT_DIR="/usr/share/archur"
themes = {"solarized": {"text": (147,161,161),
"logo": (147,161,161),
"background": (0, 43, 54)},
"black": {"text": (255,255,255),
"logo": (255, 255, 255),
"background": (0,0,0)},
"standart": {"text": (77,77,77),
"logo": (23, 147, 209),
"background": (255,255,255)},
"inverted": {"text": (255,255,255),
"logo": (23, 147, 209),
"background": (51,51,51)}}
def get_random_text():
lines = open(DEFAULT_DIR+'/text.txt').read().splitlines()
return random.choice(lines)
def get_random_theme():
key = random.choice(list(themes.keys()))
return themes[key]
def text_draw(draw, text, pixel, ress, theme):
text = text.split("\\n")
W, H = ress
base_height = (H/100)*70
for i in text:
font = ImageFont.truetype("DejaVuSansMono.ttf", pixel)
w, h = font.getsize(i)
draw.text(((W-w)/2, base_height), i, theme["text"], font=font)
base_height += (H/100)*8
def generate_img(output="", theme={}, text="", resolution=(1920,1080), text_scale=1.0, logo_scale=1.0):
# img = Image.open(backdrop)
img = Image.new("RGB", resolution, theme["background"])
W, H = img.size
logo = Image.open(DEFAULT_DIR+"/assets/logo.png")
colorized_img = ImageOps.colorize(logo.convert("L"), theme["logo"], theme["background"])
size = int((W/100)*17*logo_scale)
logo_newsize = colorized_img.resize((size, size), Image.ANTIALIAS)
img.paste(logo_newsize, (int((W-size)/2), int((H-size)/2)))
draw = ImageDraw.Draw(img)
base_font_pixle = int((56/1920)*resolution[0]*text_scale)
font = ImageFont.truetype("DejaVuSansMono.ttf", base_font_pixle)
w, h = font.getsize(text)
text_draw(draw, text, base_font_pixle, img.size, theme)
img.save(output, quality=100)
def main():
parser = argparse.ArgumentParser(description='Generate random Arch wallpaper')
parser.add_argument('-o','--output', help='Output file name', required=True)
parser.add_argument('-t','--theme', default=get_random_theme(),
help='The theme to use, else random. \'black\', \'solarized\', \'standart\' or \'inverted\'', required=False)
parser.add_argument('--text', default=get_random_text(), help='Text on the picture, or random', required=False)
parser.add_argument('-r', '--resolution', default=(1920,1080), help='Sets the resolution of the image. Example: 1920x1080', required=False)
parser.add_argument('-ts', '--text-scale', default=(1.0), help='Sets scale for the text. Example: 1.75', required=False)
parser.add_argument('-ls', '--logo-scale', default=(1.0), help='Sets scale for the logo. Example: 3.0', required=False)
parser.add_argument('-fg', '--foreground-color', type=str, help='Color for the text and the logo.', required=False)
parser.add_argument('-bg', '--background-color', type=str, help='Color for the background.', required=False)
args = vars(parser.parse_args())
output = args["output"]
if isinstance(args["theme"], str):
args["theme"] = themes[args["theme"]]
if isinstance(args["resolution"], str):
x,y = args["resolution"].split("x")
args["resolution"] = (int(x),int(y))
if args.get("foreground_color"):
try:
args["theme"]["text"] = ImageColor.getrgb(args["foreground_color"])
except: pass
if args.get("background_color"):
try:
args["theme"]["background"] = ImageColor.getrgb(args["background_color"])
except: pass
generate_img(output=output, theme=args["theme"], text=args["text"], resolution=args["resolution"],
text_scale=float(args["text_scale"]), logo_scale=float(args["logo_scale"]))
if __name__ == '__main__':
main()