Created
March 9, 2023 20:57
-
-
Save loon3/30df5d751d216bef3eb7078df9493af6 to your computer and use it in GitHub Desktop.
Create your own Brick Brock Clock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image, ImageDraw | |
import math | |
import numpy as np | |
# Load input image | |
input_image = Image.open("brick.png") | |
# Create blank output image | |
output_image = Image.new("RGB", (1200, 1200), "white") | |
# Draw circle on output image | |
draw = ImageDraw.Draw(output_image) | |
draw.ellipse((200, 200, 1000, 1000), fill=None, outline="white", width=10) | |
# Resize input image to fit inside circle | |
resize_factor = min(180 / input_image.width, 180 / input_image.height) | |
resized_image = input_image.resize((int(input_image.width * resize_factor), int(input_image.height * resize_factor))) | |
# Create a circular mask for the resized image | |
mask = Image.new("L", resized_image.size, 0) | |
draw = ImageDraw.Draw(mask) | |
draw.rectangle((0, 0, resized_image.width, resized_image.height), fill=255) | |
# Define hand lengths and colors | |
hour_hand_length = 320 | |
minute_hand_length = 280 | |
hand_color = (0, 0, 0) | |
# Define animation parameters | |
num_frames = 24 # Number of frames in animation | |
angle_per_frame = 360 / num_frames # Angle by which hands rotate per frame | |
# Loop over each frame and draw hands at the appropriate angle | |
frames = [] | |
for i in range(num_frames): | |
# Calculate angle of hands for this frame | |
hour_angle = (i * angle_per_frame) % 360 | |
minute_angle = 0 | |
# Calculate coordinates of hands | |
hour_x = int(600 + hour_hand_length * math.cos(math.radians(hour_angle - 90))) | |
hour_y = int(600 + hour_hand_length * math.sin(math.radians(hour_angle - 90))) | |
minute_x = int(600 + minute_hand_length * math.cos(math.radians(minute_angle - 90))) | |
minute_y = int(600 + minute_hand_length * math.sin(math.radians(minute_angle - 90))) | |
# Create a copy of the output image for this frame | |
frame = output_image.copy() | |
# Draw hands on the frame | |
draw = ImageDraw.Draw(frame) | |
draw.line((600, 600, hour_x, hour_y), fill="red", width=4) | |
draw.line((600, 600, minute_x, minute_y), fill=hand_color, width=8) | |
# Paste resized input image at 12 locations around the circle | |
for j in range(12): | |
angle = j * (360/12) - 90 | |
x = int(600 + 500 * math.cos(math.radians(angle))) | |
y = int(600 + 500 * math.sin(math.radians(angle))) | |
frame.paste(resized_image, (x-resized_image.width//2, y-resized_image.height//2), mask=mask) | |
# Append frame to list of frames | |
frames.append(frame) | |
# Save frames as an animated GIF | |
frames[0].save("clock.gif", save_all=True, append_images=frames[1:], duration=100, loop=0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment