Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. """ Converts pixel-art images into cross-stitch patterns.
  2. This tool assumes that 1px = 1 stitch.
  3. TODO:
  4. * Accept image name from command line. (DONE)
  5. * Change characters to symbols for ease of reading.
  6. * Expand number of symbols.
  7. * Create image from symbolized pixels instead of just printing to screen. (DONE)
  8. * Add grid lines and edge labels to image. (DONE)
  9. * Add legend to image, based on the `symbols` dictionary. (DONE)
  10. * Correspond hex colors to floss colors, where possible.
  11. * (Maybe) add stitch count for each color.
  12. * (Maybe) add GUI.
  13. """
  14. __author__ = "Noëlle Anthony"
  15. __version__ = "0.2.0"
  16. import sys
  17. from PIL import Image, ImageDraw
  18. from collections import defaultdict
  19. def main(img_name):
  20. img = Image.open(img_name)
  21. oimg_name_bits = img_name.split(".")
  22. oimg_name = "".join(oimg_name_bits[:-1]) + "_pattern." + oimg_name_bits[-1]
  23. w,h = img.size
  24. symbols = defaultdict(str)
  25. symbols["transparent"] = " "
  26. characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  27. # l = 0
  28. lines = []
  29. for i in range(h):
  30. line = []
  31. # k = 0
  32. for j in range(w):
  33. c = "".join(["{}{}".format(hex(x//16).split('x')[-1], hex(x%16).split('x')[-1]) for x in list(img.getpixel((j,i)))])
  34. d = " "
  35. if c[-2:] == "ff":
  36. cs = c[:-2]
  37. if cs not in symbols.keys():
  38. symbols[cs] = characters[0]
  39. characters = characters[1:]
  40. d = symbols[cs]
  41. line.append(d)
  42. # print(d, end="")
  43. # k += 1
  44. # if k == 9:
  45. # print("|", end="")
  46. # k = 0
  47. lines.append(line)
  48. # print()
  49. # l += 1
  50. # if l == 9:
  51. # for ww in range(int(w*1.1)+1):
  52. # if (ww+1)%10 == 0:
  53. # print("+", end="")
  54. # else:
  55. # print("-", end="")
  56. # l = 0
  57. # print()
  58. # print("\nLEGEND")
  59. legend = []
  60. for k,v in symbols.items():
  61. if v != " ":
  62. legend.append("{}: #{}".format(v,k))
  63. # print("\n".join(legend))
  64. owid, ohgt = (w*10)+10, (h*10)+30
  65. oimg = Image.new("RGB", (owid, ohgt), "white")
  66. draw = ImageDraw.Draw(oimg)
  67. for ww in range(1, w+1):
  68. posx = ww * 10
  69. linecolor = 0 if posx % 100 == 0 else (128,128,128)
  70. linewidth = 2 if posx % 100 == 0 else 1
  71. draw.line((posx, 10, posx, ohgt-20), fill=linecolor, width=linewidth)
  72. for hh in range(1, h+1):
  73. posy = hh * 10
  74. linecolor = 0 if posy % 100 == 0 else (128,128,128)
  75. linewidth = 2 if posx % 100 == 0 else 1
  76. draw.line((10, posy, owid, posy), fill=linecolor, width=linewidth)
  77. char_positions = [x*10+4 for x in range(1,h+1)]
  78. # print(char_positions)
  79. #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)}
  80. adjust = 0
  81. for line in lines:
  82. for char in range(len(line)):
  83. draw.text((char_positions[char], char_positions[0]-4+adjust), line[char], fill=0)
  84. adjust += 10
  85. draw.text((20, ohgt-10), " ".join(legend), fill=0)
  86. oimg.save(oimg_name)
  87. print("Saved {}".format(oimg_name))
  88. if __name__ == "__main__":
  89. #print(len(sys.argv))
  90. #print(sys.argv[1])
  91. if len(sys.argv) >= 2:
  92. img_name = sys.argv[1]
  93. else:
  94. img_name = "test.png"
  95. main(img_name)