Skip to content

Instantly share code, notes, and snippets.

@Alliegaytor
Created August 8, 2023 05:11
Show Gist options
  • Save Alliegaytor/c1fc1804b310eb58cb0cdde01902ed3b to your computer and use it in GitHub Desktop.
Save Alliegaytor/c1fc1804b310eb58cb0cdde01902ed3b to your computer and use it in GitHub Desktop.
Generate canvases based on pixels placed by certain users or instances or timestamps
from PIL import Image, ImageDraw
from datetime import datetime
# Get yours here https://cdn.sc07.company/canvas/2023/pixels.log.txt
log_filename = 'pixels.log.txt'
# Show pixles after this unix timestamp
start_time = 0
# Show specific users (example top 10 pixel placers)
# users = "guantousam@lemmy.world \
# ShadowGlider@lemmy.world \
# DragonM97HD@lemmy.world \
# Lokloy@lemmy.world \
# DJSoundwav3@lemmy.world \
# ubermeisters@lemmy.world \
# peter_pangolin@toast.ooo \
# Bottom@lemm.ee \
# NanoPi@lemmy.world \
# akq@lemm.ee"
# None specified will show ALL
# CASE SENSITIVE!!!
users = None
# Instances. None specified will show ALL instances
# Example, only show blahaj and lemmy.zip
# instances = "lemmy.blahaj.zone lemmy.zip"
instances = None
# Color palette dictionary
color_palette = {
0: "000000",
1: "222222",
2: "555555",
3: "888888",
4: "CDCDCD",
5: "FFFFFF",
6: "FFD5BC",
7: "FFB783",
8: "B66D3D",
9: "77431F",
10: "FC7510",
11: "FCA80E",
12: "FDE817",
13: "FFF491",
14: "BEFF40",
15: "70DD13",
16: "31A117",
17: "0B5F35",
18: "277E6C",
19: "32B69F",
20: "88FFF3",
21: "24B5FE",
22: "125CC7",
23: "262960",
24: "8B2FA8",
25: "D24CE9",
26: "FF59EF",
27: "FFA9D9",
28: "FF6474",
29: "F02523",
30: "B11206",
31: "740C00"
}
# Function to convert hex color to RGB tuple
def hex_to_rgb(hex_color: str) -> tuple[int, int, int]:
return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
# Mapping pallet index to RGB color values
color_mapping = {index: hex_to_rgb(hex_color) for index, hex_color in color_palette.items()}
# Read log
def read_log_entries(filename: str) -> list[tuple]:
log_entries = []
with open(filename, 'r') as log_file:
for line in log_file:
parts = line.strip().split('\t')
timestamp = parts[0]
user = parts[1]
x_coord = int(parts[2])
y_coord = int(parts[3])
color = int(parts[4])
action = parts[5]
log_entries.append((timestamp, user, x_coord, y_coord, color, action))
return log_entries
def filter_pixels(actions: str, start_time: int, users = None, instances = None):
log_entries = read_log_entries(log_filename)
pixel_actions = {}
for entry in log_entries:
timestamp, user, x_coord, y_coord, color, action = entry
if start_time == 0 or datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S,%f') > datetime.fromtimestamp(start_time):
if not users or user in users:
if not instances or user.split("@")[-1] in instances:
if action == actions:
pixel_actions[(x_coord, y_coord)] = (user, color_mapping[color], timestamp)
return pixel_actions
def draw_pixels(pixels, output: str) -> None:
image_size = (1000, 1000)
image = Image.new("RGB", image_size, "white")
draw = ImageDraw.Draw(image)
for pixel, (user, color, _) in pixels.items():
x, y = pixel
draw.point((x, y), fill=color)
image.save(output)
draw_pixels(filter_pixels("user place", start_time, users, instances), "output_image.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment