forked from McJazzy/hexagonpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage.py
More file actions
71 lines (53 loc) · 1.76 KB
/
image.py
File metadata and controls
71 lines (53 loc) · 1.76 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
from PIL import Image,ImageDraw
import numpy as np
import math
def hexagon_corners(center,size):
x = center[0]
y = center[1]
w = math.sqrt(3) * size
h = 2 * size
return [
(x - w / 2, y - h / 4),
(x, y - h /2),
(x + w/2, y - h / 4),
(x + w/2, y + h/4),
(x, y + h/2),
(x - w / 2, y + h/4)
]
def rectangle_corners(center, w, h):
x = center[0]
y = center[1]
return [
(x - w/2, y - h/2),
(x + w/2 , y - h/2),
(x +w/2, y + h/2),
(x -w /2, y + h/2)
]
def hexagonify(path, hexagon_size):
im = Image.open(path)
I = np.asarray(im)
draw = ImageDraw.Draw(im)
w = math.sqrt(3) * hexagon_size
h = 2 * hexagon_size
#numer of hexagons horizontally and vertically
num_hor = int(im.size[0] / w) + 2
num_ver = int(im.size[1] / h * 4 / 3) + 2
for i in range(0,num_hor*num_ver):
column = i % num_hor
row = i // num_hor
even = row % 2 # the even rows of hexagons has w/2 offset on the x-axis compared to odd rows.
p = hexagon_corners((column*w + even * w/2,row*h * 3/4), hexagon_size)
# compute the average color of the hexagon, use a rectangle approximation.
raw = rectangle_corners((column*w + even * w/2, row*h * 3/4), w, h)
r = []
for points in raw:
np0 = int(np.clip(points[0], 0, im.size[0]))
np1 = int(np.clip(points[1], 0, im.size[1]))
r.append((np0,np1))
color = np.average(I[r[0][1]:r[3][1],r[0][0]:r[1][0]], axis=(0,1))
color = tuple(color.astype(int))
draw.polygon(p, fill=color)
return im
if __name__ == "__main__":
im = hexagonify('Lenna.png', 2)
im.show()