mirror of
https://github.com/trafficlunar/blockmatic.git
synced 2026-06-27 22:24:12 +00:00
chore: get average colors of blocks
This commit is contained in:
parent
5790b41a29
commit
f329c2d71e
5 changed files with 318 additions and 0 deletions
2
generator/README.md
Normal file
2
generator/README.md
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
# blockmatic/generator
|
||||
Collection of Python scripts to generate data for blockmatic.
|
||||
36
generator/average-color.py
Normal file
36
generator/average-color.py
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import os
|
||||
import json
|
||||
from pathlib import Path
|
||||
from PIL import Image
|
||||
import numpy as np
|
||||
|
||||
directory = Path("blocks/")
|
||||
output = Path("average_colors.json")
|
||||
|
||||
# Main function
|
||||
def calculate(image_path):
|
||||
image = Image.open(image_path)
|
||||
image = image.convert("RGB")
|
||||
pixels = np.array(image)
|
||||
color = pixels.mean(axis=(0, 1))
|
||||
return tuple(int(c) for c in color)
|
||||
|
||||
# Create directory if it doesn't exist
|
||||
directory.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# Data for the colors to be written to a file
|
||||
color_data = {}
|
||||
|
||||
# Go through each file in the directory
|
||||
for filename in os.listdir(directory):
|
||||
file = os.path.join(directory, filename)
|
||||
color = calculate(file)
|
||||
|
||||
# Add to color_data while also removing the file extension (.png)
|
||||
color_data[filename[:-4]] = list(color)
|
||||
|
||||
# Write output to file
|
||||
with open(output, "w") as json_file:
|
||||
json.dump(color_data, json_file)
|
||||
|
||||
print("Done!")
|
||||
2
generator/requirements.txt
Normal file
2
generator/requirements.txt
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
numpy==2.1.3
|
||||
pillow==11.0.0
|
||||
Loading…
Add table
Add a link
Reference in a new issue