-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_buffer.py
More file actions
132 lines (71 loc) · 2.6 KB
/
basic_buffer.py
File metadata and controls
132 lines (71 loc) · 2.6 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
128
129
130
131
132
#!/usr/bin/env python
'''
Basic Buffer
'''
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import cKDTree
from PIL import Image, ImageFilter
import csv
# example case 1350
width = 1350
height = int(width/2)
# load raster image and note which points are lit
image = Image.open(f'usa-raster-flat-{str(width)}.png')
pixels = np.array(image)
#load raster image and note which points define the border
border = image.filter(ImageFilter.FIND_EDGES)
border_pixels = np.array(border)
borderPts = np.where(border_pixels != 0)
#convert to interger arrays rather then boolean
pixels = pixels*1
border_pixels = border_pixels*1
#sum to defined index values for border, inside and outside.
pixel_indicies = (pixels+border_pixels)
#We now have an 2D numpy array where:
pixel_indicies = pixel_indicies - 1
#Inner pixels: 0, Outer pixels: -1, border_pixels: 1
#pixel_indicies = pixel_indicies*(255/2)
#Converting borderPts into a more readable array
current_border_pts = []
for i in range(len(borderPts[0])):
current_border_pts.append([borderPts[0][i],borderPts[1][i]])
#now loop and updated current border_points:
#while -1 in pixel_indicies:
for trial in range(0,10):
print(trial)
for pts in current_border_pts:
x = pts[0]
y = pts[1]
if x-1 > 0 and y-1 > 0 and x+1 < width and y+1 < height:
if pixel_indicies[x-1][y] == -1:
pixel_indicies[x-1][y] += 3
#E
elif pixel_indicies[x+1][y] == -1:
pixel_indicies[x+1][y] += 3
#S
elif pixel_indicies[x][y-1] == -1:
pixel_indicies[x][y-1] += 3
#N
elif pixel_indicies[x][y+1] == -1:
pixel_indicies[x][y+1] += 3
#SW
elif pixel_indicies[x-1][y-1] == -1:
pixel_indicies[x-1][y-1] += 3
#SE
elif pixel_indicies[x+1][y-1] == -1:
pixel_indicies[x+1][y-1] += 3
#NE
elif pixel_indicies[x+1][y+1] == -1:
pixel_indicies[x+1][y+1] += 3
#NW
elif pixel_indicies[x-1][y+1] == -1:
pixel_indicies[x-1][y+1] += 3
new_borderPts = np.where(pixel_indicies == 2)
updated_border_pts = []
for i in range(len(new_borderPts[0])):
updated_border_pts.append([new_borderPts[0][i],new_borderPts[1][i]])
current_border_pts = updated_border_pts
pixel_indicies_alt = (pixel_indicies + 1)*(255/3)
image = Image.fromarray(pixel_indicies_alt)
image.show()