You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

5 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # coding: utf-8
  2. """ Converts pixel-art images into cross-stitch patterns.
  3. This tool assumes that 1px = 1 stitch.
  4. TODO:
  5. * Accept image name from command line. (DONE)
  6. * Change characters to symbols for ease of reading.
  7. * Expand number of symbols.
  8. * Create image from symbolized pixels instead of just printing to screen. (DONE)
  9. * Add grid lines and edge labels to image. (DONE)
  10. * Add legend to image, based on the `symbols` dictionary. (DONE)
  11. * Correspond hex colors to floss colors, where possible.
  12. * (Maybe) add stitch count for each color. (DONE)
  13. * (Maybe) add GUI. (DONE)
  14. * Make sure legend width doesn't exceed image width.
  15. """
  16. __author__ = "Noëlle Anthony"
  17. __version__ = "0.4.0"
  18. import sys
  19. from PIL import Image, ImageDraw
  20. from collections import defaultdict
  21. def create_stitch(img_name):
  22. img = Image.open("/var/www/noelle.codes/node/stitchimg/" + img_name)
  23. oimg_name_bits = img_name.split("/")[-1].split(".")
  24. oimg_name = "".join(oimg_name_bits[:-1]) + "_pattern." + oimg_name_bits[-1]
  25. oimg_fullpath = "/var/www/noelle.codes/node/stitchimg/" + oimg_name
  26. w,h = img.size
  27. symbols = defaultdict(str)
  28. symbols["transparent"] = " "
  29. characters = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
  30. symbol_counts = defaultdict(int)
  31. # l = 0
  32. lines = []
  33. for i in range(h):
  34. line = []
  35. # k = 0
  36. for j in range(w):
  37. c = "".join(["{}{}".format(hex(x//16).split('x')[-1], hex(x%16).split('x')[-1]) for x in list(img.getpixel((j,i)))])
  38. d = " "
  39. if c[-2:] == "ff":
  40. cs = c[:-2]
  41. if cs not in symbols.keys():
  42. symbols[cs] = characters[0]
  43. characters = characters[1:]
  44. symbol_counts[cs] += 1
  45. d = symbols[cs]
  46. line.append(d)
  47. # print(d, end="")
  48. # k += 1
  49. # if k == 9:
  50. # print("|", end="")
  51. # k = 0
  52. lines.append(line)
  53. # print()
  54. # l += 1
  55. # if l == 9:
  56. # for ww in range(int(w*1.1)+1):
  57. # if (ww+1)%10 == 0:
  58. # print("+", end="")
  59. # else:
  60. # print("-", end="")
  61. # l = 0
  62. # print()
  63. # print("\nLEGEND")
  64. legend = []
  65. keys = 0
  66. for k,v in symbols.items():
  67. if v != " ":
  68. keys += 1
  69. legend.append("{}: #{} ({}ct)".format(v, k, symbol_counts[k]))
  70. #print("{} keys".format(keys))
  71. # print("\n".join(legend))
  72. owid, ohgt = (w*10)+10, (h*10)+20+(15*(int(keys/3)+1))
  73. #print((owid, ohgt))
  74. oimg = Image.new("RGB", (owid, ohgt), "white")
  75. draw = ImageDraw.Draw(oimg)
  76. woff, hoff = int(((w)%10)/2)+1, int(((h)%10)/2)+1
  77. for ww in range(1, w+1):
  78. posx = ww * 10
  79. linecolor = 0 if (posx-(woff*10)) % 100 == 0 else (128,128,128)
  80. linewidth = 2 if (posx-(woff*10)) % 100 == 0 else 1
  81. draw.line((posx, 10, posx, ((h+1)*10)), fill=linecolor, width=linewidth)
  82. for hh in range(1, h+2):
  83. posy = hh * 10
  84. linecolor = 0 if (posy-(hoff*10)) % 100 == 0 else (128,128,128)
  85. linewidth = 2 if (posy-(hoff*10)) % 100 == 0 else 1
  86. draw.line((10, posy, owid, posy), fill=linecolor, width=linewidth)
  87. char_positions = [x*10+4 for x in range(1,max(h,w)+1)]
  88. # print(char_positions)
  89. #char_colors = {" ": (0,0,0), "A": (0,0,0), "B": (128,0,0), "C": (0,128,0), "D": (0,255,255), "E": (128,128,0), "F": (128,0,128), "G": (0,0,0)}
  90. adjust = 0
  91. for line in lines:
  92. for char in range(len(line)):
  93. # print(char_positions[char])
  94. # print(line[char])
  95. try:
  96. draw.text((char_positions[char], char_positions[0]-4+adjust), line[char], fill=0)
  97. except:
  98. pass
  99. adjust += 10
  100. legend_out = ""
  101. item_ct = 0
  102. for item in legend:
  103. item_ct += 1
  104. legend_out += item
  105. if item_ct % 3 == 0:
  106. legend_out += "\n"
  107. else:
  108. legend_out += " "
  109. draw.text((20, (h*10)+20), legend_out, fill=0)
  110. oimg.save(oimg_name)
  111. print("{}".format(oimg_fullpath))
  112. sys.stdout.flush()
  113. # if display:
  114. # oimg.show()
  115. if __name__ == "__main__":
  116. create_stitch(sys.argv[1])