-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnShield.py
More file actions
62 lines (50 loc) · 1.75 KB
/
Copy pathUnShield.py
File metadata and controls
62 lines (50 loc) · 1.75 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
import os
import sys
import copy
from PIL import Image
#Minimum number of pixels per letter:
MIN_PIXEL_PER_LETTER_COUNT = 200
#Store locations for each color type:
class PixelLoc():
def __init__(self, color_tup):
self.color_tup = color_tup
self.loc = []
self.count = 1
#Open the input file and the out file:
inp = Image.open(sys.argv[1], "r")
out = Image.new("RGBA", inp.size, (255, 255, 255))
print "Input image side: %s" % (str(inp.size), )
#String for resetting the image back to black:
reset_str = out.tostring()
color_dict = {}
for i in xrange(inp.size[0]):
for j in xrange(inp.size[1]):
cur_pixel = inp.getpixel((i, j))
#Save all the color locations for each color:
if color_dict.has_key(cur_pixel):
color_dict[cur_pixel].count += 1
else:
color_dict[cur_pixel] = PixelLoc(cur_pixel)
color_dict[cur_pixel].loc.append((i, j))
#if (0, 0, 0) == cur_pixel:
# out.putpixel((i, j), (255, 255, 255))
#else:
# out.putpixel((i, j), cur_pixel)
#Find only colors over the min count:
big_dict = {}
for i in color_dict.keys():
if color_dict[i].count > MIN_PIXEL_PER_LETTER_COUNT:
big_dict[i] = color_dict[i]
#Create a picture for each letter (by the count):
counter = 0
for color in big_dict.keys():
print "Key >>>", color
# print "color: %s, locations: %s" % (str(big_dict[color].color_tup), big_dict[color].loc)
#Make it black for easier recognition:
for i in big_dict[color].loc:
out.putpixel(i, (0, 0, 0))
#Save the picture and reset the image object:
out.save("parsed%s.png" % (counter))
counter += 1
out.fromstring(reset_str)
print "ended!"