main.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. from PIL import Image
  2. import json
  3. def readtxt():
  4. file = open("emoji.txt")
  5. txt = file.read()
  6. file.close()
  7. txt = txt.split("\n")
  8. res = []
  9. for t in txt:
  10. t = t.replace("\r", "")
  11. if t != "":
  12. res.append(t)
  13. print("read txt count: ", len(res))
  14. return res
  15. def readcss():
  16. txt = readtxt()
  17. file = open("emoji.css")
  18. css = file.read()
  19. file.close()
  20. css = css.split("\n\n")
  21. result = []
  22. tlist = ""
  23. for st in css:
  24. props = st.replace(".em.", "").split(", .")[0].split("{")
  25. name = props[0].replace(" ", "").split(",")[0]
  26. if name in txt:
  27. tlist = tlist + name + "\n"
  28. value = props[1].split("\n")[1].split(":")[1].split(" ")
  29. values = {
  30. "x" : float(value[1].split("%")[0]),
  31. "y" : float(value[2].split("%")[0])
  32. }
  33. result.append({
  34. "name": name,
  35. "value": values
  36. })
  37. # file = open("result.json", "w+")
  38. # file.write(json.dumps(result, indent=2))
  39. # file.write(tlist)
  40. # file.close()
  41. return result
  42. def fixto64(number):
  43. a = round(number / 64)
  44. return a * 64
  45. def slice(config):
  46. img = Image.open("emoji.png")
  47. width = img.width
  48. height = img.height
  49. x = config["value"]["x"]
  50. y = config["value"]["y"]
  51. x = round(width * x / 100, 2)
  52. y = round(height * y / 100, 2)
  53. x = x - 32 - 10
  54. y = y - 32 + 2
  55. x = fixto64(x)
  56. y = fixto64(y)
  57. res = {
  58. "name": config["name"],
  59. "rect": [x, y]
  60. }
  61. return res
  62. # box = (img.width * x, img.height * y, img.width * x + 64, img.height * y + 64)
  63. # box = (x, y, x + 64, y + 64)
  64. # img2 = img.crop(box)
  65. # print(config["name"])
  66. # img2.save("./emoji/" + config["name"] + ".png")
  67. if __name__ == "__main__":
  68. result = readcss()
  69. # print("read css count: ", len(result))
  70. res = {}
  71. for item in result:
  72. cnf = slice(item)
  73. res[cnf["name"]] = cnf["rect"]
  74. file = open("emoji.json", "w+")
  75. file.write(json.dumps(res, indent=2))
  76. file.close()
  77. # img = Image.open("emoji.png")
  78. # width = img.width
  79. # height = img.height
  80. # x = round(width * 0.65, 2)
  81. # y = round(height * 0.475, 2)
  82. # print(x, y)
  83. # img.crop((x, y, x + 64, y + 64)).save("smile.png")