-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.py
More file actions
58 lines (48 loc) · 1.67 KB
/
Copy pathgenerator.py
File metadata and controls
58 lines (48 loc) · 1.67 KB
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
import random
from PIL import Image, ImageDraw, ImageFont
# Open errors.txt and add every line without whitespace
errors = []
with open("errors.txt", "r", encoding="utf-8") as f:
for line in f:
stripped_line = line.strip()
if stripped_line:
errors.append(stripped_line)
# Select a random error and user
error = random.choice(errors)
users = ["user", "root", "yougo-rgb", "octocat", "tux"]
user = random.choice(users)
content = f"{user}@linux:~$ {error}"
# Write the error in ERROR.md
with open("ERROR.md", "w", encoding="utf-8") as f:
f.write(f"```txt\n{content}\n```")
# Image generation settings
padding = 20
background = (0, 0, 0)
border_radius = 12
font = ImageFont.truetype("./DejaVuSansMono.ttf", 25)
font_colors = [ (255, 255, 255), (255, 0, 0), (0, 255, 0)]
lines = content.split("\n")
# Creat a temp image to measure text sizes
temp_img = Image.new("RGB", (1, 1))
draw = ImageDraw.Draw(temp_img)
# Compute minimal image size
width = max(draw.textbbox((0, 0), line, font=font)[2] for line in lines) + padding * 2
height = max(draw.textbbox((0, 0), line, font=font)[3] for line in lines) + padding * 2
# Create transparent canvas and rounded background
img = Image.new("RGBA", (width, height), (0, 0, 0, 0))
draw = ImageDraw.Draw(img)
draw.rounded_rectangle(
[(0, 0), (width, height)],
radius=border_radius,
fill=background
)
# Render each line of text and select a random color
x = padding
foreground = random.choice(font_colors)
for line in lines:
bbox = draw.textbbox((0, 0), line, font=font)
line_height = bbox[3]
draw.text((padding, x), line, fill=foreground, font=font)
x += line_height
# Save final image
img.save("ERROR.png")