magictk/packer/run.py

42 lines
1.5 KiB
Python

import PIL
import os
import shutil
import cairosvg
import json
import base64
import pickle
size_list = [12, 16, 32, 48, 64]
if (os.path.exists("packer"+os.sep+"temp")):
print("contiune convert png")
else:
print("covert...")
os.mkdir("packer"+os.sep+"temp")
for i in os.listdir("packer"+os.sep+"source"):
if (os.path.splitext(i)[1] == ".svg"):
print("convert "+i+" to png")
with open("packer"+os.sep+"source"+os.sep+i, "r") as file:
cairosvg.svg2png(file_obj=file, write_to="packer"+os.sep+"temp" +
os.sep+os.path.splitext(i)[0]+".png")
print("--- covert json ---")
if (os.path.exists("packer"+os.sep+"output")):
shutil.rmtree("packer"+os.sep+"output")
os.mkdir("packer"+os.sep+"output")
full_lst = {}
for i in os.listdir("packer"+os.sep+"temp"):
img_origin = PIL.Image.open("packer"+os.sep+"temp"+os.sep+i)
for size in size_list:
print("convert "+i+" to json with size "+str(size))
img = img_origin.resize((size, size))
img_lst = []
for x in range(size):
img_lst.append([])
for y in range(size):
img_lst[-1].append(img.getpixel((x, y))[-1])
full_lst["icon/"+os.path.splitext(i)[0]+"@"+str(size)
] = base64.b64encode(bytes(json.dumps(img_lst), encoding="utf-8"))
print("write...")
with open("packer"+os.sep+"output"+os.sep+"output.pack", "wb") as file:
file.write(pickle.dumps(full_lst))