refactor: replace generator with node.js scripts

This commit is contained in:
trafficlunar 2024-12-21 14:49:51 +00:00
parent 09cdd58963
commit 3f60fc2a29
6 changed files with 39 additions and 44 deletions

7
.gitignore vendored
View file

@ -23,7 +23,6 @@ dist-ssr
*.sln
*.sw?
# Python
.venv
generator/blocks
generator/*.json
# Generator
generator/blocks/
generator/data/

View file

@ -1,2 +1,3 @@
# blockmatic/generator
Collection of Python scripts to generate data for blockmatic.
# @blockmatic/generator
Collection of Node.JS scripts to generate data for blockmatic.

View file

@ -1,36 +0,0 @@
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("RGBA")
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!")

View file

@ -0,0 +1,25 @@
const fs = require("fs");
const path = require("path");
const { getAverageColor } = require("fast-average-color-node");
const INPUT = path.join(__dirname, "blocks/");
const OUTPUT = path.join(__dirname, "data/average_colors.json");
const data = {};
(async () => {
const files = fs.readdirSync(INPUT);
for (const file of files) {
const filePath = path.join(INPUT, file);
const color = await getAverageColor(filePath);
const fileName = file.slice(0, -4);
data[fileName] = [color.value[0], color.value[1], color.value[2], color.value[3]];
}
fs.writeFileSync(OUTPUT, JSON.stringify(data));
console.log("Done!");
})();

8
generator/package.json Normal file
View file

@ -0,0 +1,8 @@
{
"name": "@blockmatic/generator",
"type": "commonjs",
"version": "1.0.0",
"dependencies": {
"fast-average-color-node": "^3.1.0",
}
}

View file

@ -1,2 +0,0 @@
numpy==2.1.3
pillow==11.0.0