config/fonts/madecarvingsoft.woff2
config/fonts/madecarvingsoft.woff2
This is a binary file and will not be displayed.
+16
config/generation.json
+16
config/generation.json
···
1
+
{
2
+
"name": "Ewan Croft",
3
+
"sky_colours": {
4
+
"0": [10, 10, 40],
5
+
"4": [20, 20, 60],
6
+
"6": [100, 80, 150],
7
+
"9": [135, 206, 235],
8
+
"12": [70, 130, 180],
9
+
"15": [135, 206, 235],
10
+
"18": [255, 165, 0],
11
+
"20": [139, 69, 19],
12
+
"22": [20, 20, 60],
13
+
"23": [10, 10, 40]
14
+
},
15
+
"timezone": "Europe/London"
16
+
}
+107
src/generator.py
+107
src/generator.py
···
1
+
import json
2
+
import numpy as np
3
+
from PIL import Image, ImageDraw, ImageFont
4
+
import os
5
+
import sys
6
+
7
+
# Load sky colours, name, and timezone from generation.json
8
+
with open("./config/generation.json", "r") as file:
9
+
config = json.load(file)
10
+
sky_colours = config["sky_colours"]
11
+
name = config["name"]
12
+
timezone = config["timezone"]
13
+
14
+
# Function to interpolate RGB values between given key times
15
+
def interpolate_colour(hour):
16
+
hours = sorted(map(int, sky_colours.keys()))
17
+
for i in range(len(hours) - 1):
18
+
if hours[i] <= hour <= hours[i + 1]:
19
+
t = (hour - hours[i]) / (hours[i + 1] - hours[i])
20
+
c1, c2 = np.array(sky_colours[str(hours[i])]), np.array(
21
+
sky_colours[str(hours[i + 1])]
22
+
)
23
+
return tuple((1 - t) * c1 + t * c2)
24
+
return sky_colours[str(hour)] # Exact match case
25
+
26
+
# Generate 24 images if they do not already exist
27
+
width, height = 1500, 500
28
+
font_size = 50
29
+
font_path = "./config/fonts/madecarvingsoft.woff2" # Adjust if a different font is needed
30
+
31
+
# Folder to store images
32
+
output_folder = "./src/blobs"
33
+
print(f"Attempting to create folder: {output_folder}")
34
+
35
+
# Check and create the folder if it doesn't exist
36
+
try:
37
+
if not os.path.exists(output_folder):
38
+
os.makedirs(output_folder)
39
+
print(f"Folder created: {output_folder}")
40
+
else:
41
+
print(f"Folder already exists: {output_folder}")
42
+
except Exception as e:
43
+
print(f"Error creating folder: {e}")
44
+
45
+
# Check if images need to be generated
46
+
images_to_generate = []
47
+
for hour in range(24):
48
+
image_path = f"{output_folder}/{str(hour).zfill(2)}.png"
49
+
print(f"Image path for hour {hour}: {image_path}")
50
+
if not os.path.exists(image_path):
51
+
images_to_generate.append(hour)
52
+
53
+
# Regenerate images if needed
54
+
if images_to_generate:
55
+
print("Regenerating missing images...")
56
+
57
+
# Generate images
58
+
for hour in images_to_generate:
59
+
colour = interpolate_colour(hour)
60
+
fade_ratio = 0.3 # Make the gradient fade lower
61
+
gradient_height = int(height * fade_ratio)
62
+
gradient = np.vstack([
63
+
np.full((height - gradient_height, width, 3), colour, dtype=np.uint8),
64
+
np.linspace(colour, (255, 255, 255), gradient_height).astype(np.uint8).reshape(gradient_height, 1, 3).repeat(width, axis=1)
65
+
])
66
+
67
+
# Debugging: Check gradient shape and sample pixel value
68
+
print(f"Hour {hour}: Gradient shape - {gradient.shape}")
69
+
print(f"Hour {hour}: Sample pixel value at (0, 0) - {gradient[0, 0]}")
70
+
71
+
# Directly create the image from the gradient array
72
+
img = Image.fromarray(gradient)
73
+
74
+
# Debugging: Check image mode and size
75
+
print(f"Hour {hour}: Image mode - {img.mode}")
76
+
print(f"Hour {hour}: Image size - {img.size}")
77
+
78
+
# Add text
79
+
draw = ImageDraw.Draw(img)
80
+
try:
81
+
font = ImageFont.truetype(font_path, font_size)
82
+
except IOError:
83
+
font = ImageFont.load_default()
84
+
85
+
# Calculate text width and height using textbbox
86
+
bbox = draw.textbbox((0, 0), name, font=font)
87
+
text_width = bbox[2] - bbox[0]
88
+
text_height = bbox[3] - bbox[1]
89
+
90
+
position = (20, 20) # Top-left corner
91
+
text_colour = (255 - int(colour[0]), 255 - int(colour[1]), 255 - int(colour[2])) # Contrasting colour
92
+
draw.text(position, name, fill=text_colour, font=font)
93
+
94
+
# Save image to disk
95
+
image_path = f"{output_folder}/{str(hour).zfill(2)}.png"
96
+
try:
97
+
img.save(image_path)
98
+
print(f"Hour {hour}: Image saved successfully.")
99
+
except Exception as e:
100
+
print(f"Error saving image for hour {hour}: {e}")
101
+
102
+
print("Missing images regenerated.")
103
+
104
+
else:
105
+
print("All images already exist. No regeneration needed.")
106
+
107
+
print("Generator script completed.")