-
Notifications
You must be signed in to change notification settings - Fork 3
/
ex18_BlackWhiteChars.py
51 lines (40 loc) · 1.18 KB
/
ex18_BlackWhiteChars.py
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
"""
Experiment 18
Convert image to a set of B(black) and W(white) characters
Showcase: https://www.instagram.com/p/C305UGvIMlM://www.instagram.com/p/C305UGvIMlM/
"""
import sys
from PIL import Image, ImageDraw, ImageFont
def GetAverage(xy, imBW):
average=0
count=0
for x in range(xy[0],xy[2]):
for y in range(xy[1],xy[3]):
average += imBW.getpixel((x,y))
count += 1
average /= count
if average>bPoint:
return (c1, 'W')
return (c2, 'B')
c1 = (255,255,255)
c2 = (0,0,0)
c3 = (122,122,122)
fontPath = "Font path, such as font, such as font.ttf"
bPoint = .8
wBlocksN = 30
with Image.open(sys.argv[1]) as imIn:
w,h = imIn.size
hBlocksN = int( (h/w)*wBlocksN )
l = int(w/wBlocksN)
f = ImageFont.truetype(fontPath, size=l)
imBW = imIn.convert('L')
imOut = Image.new('RGB', imIn.size, color=c3)
draw = ImageDraw.Draw(imOut)
for i in range(wBlocksN+1):
x = i*l
for j in range(hBlocksN+1):
y = j*l
xy = (x,y,x+l,y+l)
color,character = GetAverage(xy, imBW)
draw.text((x+l/2,y+l/2), character, font=f, fill=color)
imOut.show()