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.

stitchify.py 3.9KB

5 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. try:
  23. img = Image.open("/var/www/noelle.codes/node/stitchimg/" + img_name)
  24. oimg_name_bits = img_name.split("/")[-1].split(".")
  25. oimg_name = "".join(oimg_name_bits[:-1]) + "_pattern." + oimg_name_bits[-1]
  26. oimg_fullpath = "/var/www/noelle.codes/node/stitchimg/" + oimg_name
  27. w,h = img.size
  28. symbols = defaultdict(str)
  29. symbols["transparent"] = " "
  30. characters = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
  31. symbol_counts = defaultdict(int)
  32. # l = 0
  33. lines = []
  34. for i in range(h):
  35. line = []
  36. # k = 0
  37. for j in range(w):
  38. c = "".join(["{}{}".format(hex(x//16).split('x')[-1], hex(x%16).split('x')[-1]) for x in list(img.getpixel((j,i)))])
  39. d = " "
  40. if c[-2:] == "ff":
  41. cs = c[:-2]
  42. if cs not in symbols.keys():
  43. symbols[cs] = characters[0]
  44. characters = characters[1:]
  45. symbol_counts[cs] += 1
  46. d = symbols[cs]
  47. line.append(d)
  48. # print(d, end="")
  49. # k += 1
  50. # if k == 9:
  51. # print("|", end="")
  52. # k = 0
  53. lines.append(line)
  54. # print()
  55. # l += 1
  56. # if l == 9:
  57. # for ww in range(int(w*1.1)+1):
  58. # if (ww+1)%10 == 0:
  59. # print("+", end="")
  60. # else:
  61. # print("-", end="")
  62. # l = 0
  63. # print()
  64. # print("\nLEGEND")
  65. legend = []
  66. keys = 0
  67. for k,v in symbols.items():
  68. if v != " ":
  69. keys += 1
  70. legend.append("{}: #{} ({}ct)".format(v, k, symbol_counts[k]))
  71. #print("{} keys".format(keys))
  72. # print("\n".join(legend))
  73. owid, ohgt = (w*10)+10, (h*10)+20+(15*(int(keys/3)+1))
  74. #print((owid, ohgt))
  75. oimg = Image.new("RGB", (owid, ohgt), "white")
  76. draw = ImageDraw.Draw(oimg)
  77. woff, hoff = int(((w)%10)/2)+1, int(((h)%10)/2)+1
  78. for ww in range(1, w+1):
  79. posx = ww * 10
  80. linecolor = 0 if (posx-(woff*10)) % 100 == 0 else (128,128,128)
  81. linewidth = 2 if (posx-(woff*10)) % 100 == 0 else 1
  82. draw.line((posx, 10, posx, ((h+1)*10)), fill=linecolor, width=linewidth)
  83. for hh in range(1, h+2):
  84. posy = hh * 10
  85. linecolor = 0 if (posy-(hoff*10)) % 100 == 0 else (128,128,128)
  86. linewidth = 2 if (posy-(hoff*10)) % 100 == 0 else 1
  87. draw.line((10, posy, owid, posy), fill=linecolor, width=linewidth)
  88. char_positions = [x*10+4 for x in range(1,max(h,w)+1)]
  89. # print(char_positions)
  90. #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)}
  91. adjust = 0
  92. for line in lines:
  93. for char in range(len(line)):
  94. # print(char_positions[char])
  95. # print(line[char])
  96. try:
  97. draw.text((char_positions[char], char_positions[0]-4+adjust), line[char], fill=0)
  98. except:
  99. pass
  100. adjust += 10
  101. legend_out = ""
  102. item_ct = 0
  103. for item in legend:
  104. item_ct += 1
  105. legend_out += item
  106. if item_ct % 3 == 0:
  107. legend_out += "\n"
  108. else:
  109. legend_out += " "
  110. draw.text((20, (h*10)+20), legend_out, fill=0)
  111. oimg.save(oimg_fullpath)
  112. print("{}".format(oimg_name))
  113. sys.stdout.flush()
  114. # if display:
  115. # oimg.show()
  116. except Exception as err:
  117. with open("stitchify_err.log", "a") as log:
  118. log.write("Error: {}\n".format(str(err)))
  119. ostr = "stitchify_error.png"
  120. print(ostr)
  121. sys.stdout.flush()
  122. if __name__ == "__main__":
  123. create_stitch(sys.argv[1])