DChat/resource pack/generators/mc_items/generate.py

82 lines
2.3 KiB
Python
Raw Permalink Normal View History

2021-08-06 21:24:40 +02:00
from PIL import Image
import os
import json
# The size of each image in pixels.
IMAGE_SIZE = 16
# The directory containing the item image files.
ITEM_DIR = "item/"
# The directory that willcontain the texture sheet files.
SHEET_DIR = "sheets/"
# The size that the texture sheet will be. Should be 16 times the IMAGE_SIZE.
SHEET_SIZE = IMAGE_SIZE * 16
# Get a list of files in the item directory.
item_files = os.listdir(ITEM_DIR)
# Sort the list of items.
item_files = sorted(item_files)
# Create a new image of size SHEET_SIZE x SHEET_SIZE.
current_sheet = Image.new("RGBA", (SHEET_SIZE, SHEET_SIZE), (0, 0, 0, 0))
# Set the current position on the sheet to 0.
current_pos = 0
# Create an array to store finished sheets.
sheets = []
# Create an array to hold icon info.
icon_info = []
# Create the SHEET_DIR directory.
os.makedirs(SHEET_DIR)
# Loop over every item and add it to the texture sheet we are currently working on.
for item_file in item_files:
# Get the name of the item.
name = item_file.replace(".png", "")
# Load the image from disk.
image = Image.open(f"{ITEM_DIR}{item_file}")
# Calculate our position on the sheet.
x = current_pos % 16
y = current_pos // 16
# Paste the current item on the sheet.
current_sheet.paste(im=image, box=(x*16, y * 16))
icon_info.append({
"name": name,
"sheet": len(sheets),
"pos": current_pos,
"description": ["A default minecraft item."]
})
# Move one texture over on the sheet.
current_pos += 1
# Check if the current sheet is full.
if current_pos == 256:
# If it is add it to the array of finished sheets.
sheets.append(current_sheet)
# And create a new one.
current_sheet = Image.new("RGBA", (SHEET_SIZE, SHEET_SIZE), (0, 0, 0, 0))
# Move us back to the start of the current sheet.
current_pos = 0
# Check if the current sheet is partially filled.
if not current_pos == 0:
# If it is add it to the finished sheets.
sheets.append(current_sheet)
# Loop over all finished sheets.
for i in range(len(sheets)):
sheet = sheets[i]
# Save the sheet to disk.
sheet.save(f"{SHEET_DIR}sheet_{i}.png")
# Store icon_info into a json file.
with open("icon_info.json", "w") as icon_info_file:
json.dump(icon_info, icon_info_file, indent=4)