Palettes
blocks:
STONE: "#808080"
DIRT: "#8B4513"
GRASS_BLOCK: "#567D46"Want to generate a palette for EVERY block from a texture pack?
2
Adding the code
import os
from PIL import Image
import numpy as np
def compute_average_color(image_path):
"""Compute the average color of an image."""
with Image.open(image_path) as img:
# Ensure the image is in RGB mode.
img = img.convert('RGB')
# Convert the image to a NumPy array.
np_img = np.array(img)
# Calculate the mean of each color channel.
avg = np.mean(np_img.reshape(-1, 3), axis=0)
return tuple(avg.astype(int))
def rgb_to_hex(rgb):
"""Convert an (R, G, B) tuple to a hex string."""
return '#{:02X}{:02X}{:02X}'.format(*rgb)
def main():
# Set the directory containing the block texture images to "blocks".
input_dir = "blocks" # Images should be placed in this folder.
output_file = "blocks_colors.txt"
blocks = {}
# Iterate over all image files in the input directory.
for filename in os.listdir(input_dir):
if filename.lower().endswith(('.png', '.jpg', '.jpeg')):
# Use the filename (without extension) as the block name, in uppercase.
block_name = os.path.splitext(filename)[0].upper()
image_path = os.path.join(input_dir, filename)
# Compute the average color.
avg_color = compute_average_color(image_path)
# Convert the average color to a hex string.
hex_color = rgb_to_hex(avg_color)
blocks[block_name] = hex_color
# Write the results in the desired YAML-like format.
with open(output_file, "w") as file:
file.write("blocks:\n")
for block, color in blocks.items():
file.write(f" {block}: \"{color}\"\n")
print(f"Block colors written to {output_file}")
if __name__ == "__main__":
main()
Last updated