from PIL import Image import json def readtxt(): file = open("emoji.txt") txt = file.read() file.close() txt = txt.split("\n") res = [] for t in txt: t = t.replace("\r", "") if t != "": res.append(t) print("read txt count: ", len(res)) return res def readcss(): txt = readtxt() file = open("emoji.css") css = file.read() file.close() css = css.split("\n\n") result = [] tlist = "" for st in css: props = st.replace(".em.", "").split(", .")[0].split("{") name = props[0].replace(" ", "").split(",")[0] if name in txt: tlist = tlist + name + "\n" value = props[1].split("\n")[1].split(":")[1].split(" ") values = { "x" : float(value[1].split("%")[0]), "y" : float(value[2].split("%")[0]) } result.append({ "name": name, "value": values }) # file = open("result.json", "w+") # file.write(json.dumps(result, indent=2)) # file.write(tlist) # file.close() return result def fixto64(number): a = round(number / 64) return a * 64 def slice(config): img = Image.open("emoji.png") width = img.width height = img.height x = config["value"]["x"] y = config["value"]["y"] x = round(width * x / 100, 2) y = round(height * y / 100, 2) x = x - 32 - 10 y = y - 32 + 2 x = fixto64(x) y = fixto64(y) res = { "name": config["name"], "rect": [x, y] } return res # box = (img.width * x, img.height * y, img.width * x + 64, img.height * y + 64) # box = (x, y, x + 64, y + 64) # img2 = img.crop(box) # print(config["name"]) # img2.save("./emoji/" + config["name"] + ".png") if __name__ == "__main__": result = readcss() # print("read css count: ", len(result)) res = {} for item in result: cnf = slice(item) res[cnf["name"]] = cnf["rect"] file = open("emoji.json", "w+") file.write(json.dumps(res, indent=2)) file.close() # img = Image.open("emoji.png") # width = img.width # height = img.height # x = round(width * 0.65, 2) # y = round(height * 0.475, 2) # print(x, y) # img.crop((x, y, x + 64, y + 64)).save("smile.png")