img2bop/img2bop.py
2024-12-17 11:31:30 +02:00

50 lines
2.7 KiB
Python

import json
from PIL import Image
import sys
def normalise(r, g, b):
return [r / 255.0, g / 255.0, b / 255.0, 0]
if len(sys.argv) < 3:
print("You must specify an input and an output file in order to use this!\nExample: python3 img2bop.py input.jpeg output.bopijson")
exit()
im = Image.open(sys.argv[1]).convert('RGB')
bop = json.load(open("./sample.json"))
w, h = im.size
for x in range(w):
for y in range(h):
r, g, b = im.getpixel((x, y))
colour = normalise(r, g, b)
newBlock = {
"__gdtype": "Dictionary",
"pairs": [
{ "key": { "__gdtype": "StringName", "name": "uid" }, "value": 420 + x * h + y },
{ "key": { "__gdtype": "StringName", "name": "block_id" }, "value": 0 },
{ "key": { "__gdtype": "StringName", "name": "block_name" }, "value": "Block" },
{ "key": { "__gdtype": "StringName", "name": "block_color" }, "value": { "__gdtype": "Color", "values": colour } },
{ "key": { "__gdtype": "StringName", "name": "block_position" }, "value": { "__gdtype": "Vector3", "values": [x, -y, 0.0] } },
{ "key": { "__gdtype": "StringName", "name": "block_rotation" }, "value": { "__gdtype": "Vector3", "values": [0.0, 0.0, 0.0] } },
{ "key": { "__gdtype": "StringName", "name": "block_scale" }, "value": { "__gdtype": "Vector3", "values": [1.0, 1.0, 1.0] } },
{ "key": { "__gdtype": "StringName", "name": "block_pattern" }, "value": 0 },
{ "key": { "__gdtype": "StringName", "name": "block_pattern_color" }, "value": { "__gdtype": "Color", "values": [0.0, 0.0, 0.0, 1.0] } },
{ "key": "position_enabled", "value": False },
{ "key": "position_points", "value": { "__gdtype": "PackedVector3Array", "values": [] } },
{ "key": "position_travel_speed", "value": 5.0 },
{ "key": "rotation_enabled", "value": False },
{ "key": "rotation_pivot_offset", "value": { "__gdtype": "Vector3", "values": [0.0, 0.0, 0.0] } },
{ "key": "rotation_direction", "value": { "__gdtype": "Vector3", "values": [0.0, 0.0, 0.0] } },
{ "key": "rotation_speed", "value": 1.0 },
{ "key": { "__gdtype": "StringName", "name": "transparency" }, "value": 7 },
{ "key": { "__gdtype": "StringName", "name": "collision_enabled" }, "value": True },
{ "key": { "__gdtype": "StringName", "name": "transparency_enabled" }, "value": False }
]
}
bop["level_blocks"].append(newBlock)
with open(sys.argv[2], "w") as file:
json.dump(bop, file, indent=4)