forked from joyeusenoelle/stitchify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpixelstitchifier.py
More file actions
127 lines (116 loc) · 3.48 KB
/
Copy pathpixelstitchifier.py
File metadata and controls
127 lines (116 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
""" Converts pixel-art images into cross-stitch patterns.
This tool assumes that 1px = 1 stitch.
TODO:
* Accept image name from command line. (DONE)
* Change characters to symbols for ease of reading.
* Expand number of symbols.
* Create image from symbolized pixels instead of just printing to screen. (DONE)
* Add grid lines and edge labels to image. (DONE)
* Add legend to image, based on the `symbols` dictionary. (DONE)
* Correspond hex colors to floss colors, where possible.
* (Maybe) add stitch count for each color. (DONE)
* (Maybe) add GUI.
"""
__author__ = "Noëlle Anthony"
__version__ = "0.3.2"
import sys
from PIL import Image, ImageDraw
from collections import defaultdict
def main(img_name):
img = Image.open(img_name)
oimg_name_bits = img_name.split(".")
oimg_name = "".join(oimg_name_bits[:-1]) + "_pattern." + oimg_name_bits[-1]
w,h = img.size
symbols = defaultdict(str)
symbols["transparent"] = " "
characters = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
symbol_counts = defaultdict(int)
# l = 0
lines = []
for i in range(h):
line = []
# k = 0
for j in range(w):
c = "".join(["{}{}".format(hex(x//16).split('x')[-1], hex(x%16).split('x')[-1]) for x in list(img.getpixel((j,i)))])
d = " "
if c[-2:] == "ff":
cs = c[:-2]
if cs not in symbols.keys():
symbols[cs] = characters[0]
characters = characters[1:]
symbol_counts[cs] += 1
d = symbols[cs]
line.append(d)
# print(d, end="")
# k += 1
# if k == 9:
# print("|", end="")
# k = 0
lines.append(line)
# print()
# l += 1
# if l == 9:
# for ww in range(int(w*1.1)+1):
# if (ww+1)%10 == 0:
# print("+", end="")
# else:
# print("-", end="")
# l = 0
# print()
# print("\nLEGEND")
legend = []
keys = 0
for k,v in symbols.items():
if v != " ":
keys += 1
legend.append("{}: #{} ({}ct)".format(v, k, symbol_counts[k]))
print("{} keys".format(keys))
# print("\n".join(legend))
owid, ohgt = (w*10)+10, (h*10)+20+(15*(int(keys/3)+1))
print((owid, ohgt))
oimg = Image.new("RGB", (owid, ohgt), "white")
draw = ImageDraw.Draw(oimg)
woff, hoff = int(((w)%10)/2)+1, int(((h)%10)/2)+1
for ww in range(1, w+1):
posx = ww * 10
linecolor = 0 if (posx-(woff*10)) % 100 == 0 else (128,128,128)
linewidth = 2 if (posx-(woff*10)) % 100 == 0 else 1
draw.line((posx, 10, posx, ((h+1)*10)), fill=linecolor, width=linewidth)
for hh in range(1, h+2):
posy = hh * 10
linecolor = 0 if (posy-(hoff*10)) % 100 == 0 else (128,128,128)
linewidth = 2 if (posy-(hoff*10)) % 100 == 0 else 1
draw.line((10, posy, owid, posy), fill=linecolor, width=linewidth)
char_positions = [x*10+4 for x in range(1,max(h,w)+1)]
# print(char_positions)
#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)}
adjust = 0
for line in lines:
for char in range(len(line)):
# print(char_positions[char])
# print(line[char])
try:
draw.text((char_positions[char], char_positions[0]-4+adjust), line[char], fill=0)
except:
pass
adjust += 10
legend_out = ""
item_ct = 0
for item in legend:
item_ct += 1
legend_out += item
if item_ct % 3 == 0:
legend_out += "\n"
else:
legend_out += " "
draw.text((20, (h*10)+20), legend_out, fill=0)
oimg.save(oimg_name)
print("Saved {}".format(oimg_name))
if __name__ == "__main__":
#print(len(sys.argv))
#print(sys.argv[1])
if len(sys.argv) >= 2:
img_name = sys.argv[1]
else:
img_name = "test.png"
main(img_name)