您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

stitchify.py 1.0KB

12345678910111213141516171819202122232425262728293031323334353637
  1. """ Converts pixel-art images into cross-stitch patterns.
  2. This tool assumes that 1px = 1 stitch.
  3. TODO:
  4. * Change characters to symbols for ease of reading.
  5. * Expand number of symbols.
  6. * Create image from symbolized pixels instead of just printing to screen.
  7. * Add grid lines and edge labels to image.
  8. * Add legend to image, based on the `symbols` dictionary.
  9. * Correspond hex colors to floss colors, where possible.
  10. * (Maybe) add stitch count for each color.
  11. * (Maybe) add GUI.
  12. """
  13. __author__ = "Noëlle Anthony"
  14. __version__ = "0.1.0"
  15. from PIL import Image
  16. from collections import defaultdict
  17. img = Image.open('test.png')
  18. w,h = img.size
  19. symbols = defaultdict(str)
  20. characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  21. for i in range(h):
  22. for j in range(w):
  23. c = "".join(["{}{}".format(hex(x//16).split('x')[-1], hex(x%16).split('x')[-1]) for x in list(img.getpixel((j,i)))])
  24. d = " "
  25. if c not in symbols.keys():
  26. symbols[c] = characters[0]
  27. characters = characters[1:]
  28. d = symbols[c]
  29. print(d, end="")
  30. print()