-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdice_art_pattern_from_image.py
38 lines (29 loc) · 1.04 KB
/
dice_art_pattern_from_image.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
from PIL import Image, ImageOps, ImageDraw
dicew = 100
im = Image.open("/tmp/image.png")
im = ImageOps.grayscale(im)
im = ImageOps.equalize(im)
diceh = im.height / im.width * dicew
dicesize = int(im.width / dicew)
nim = Image.new("L", (im.width, im.height), 'white')
nimd = ImageDraw.Draw(nim)
for y in range(0, im.height-dicesize, dicesize):
for x in range(0, im.width-dicesize, dicesize):
thisSectorColor = 0
for dicex in range(0, dicesize):
for dicey in range(0, dicesize):
thisSectorColor += im.getpixel((x+dicex, y+dicey))
thisSectorColor = thisSectorColor / (dicesize **2 )
nimd.rectangle(((x, y),(x+dicesize, y+dicesize)), thisSectorColor)
diceNumber = (255-thisSectorColor) * 5 / 255 + 1
#print (x, y, thisSectorColor, diceNumber)
switcher = {
1: "⚀",
2: "⚁",
3: "⚂",
4: "⚃",
5: "⚄",
6: "⚅",
}
print switcher.get(diceNumber),
print