-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
65 lines (59 loc) · 2.03 KB
/
main.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# This is a sample Python script.
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
from PIL import Image
def GetNeighbors(point,r,cords):
neighbors = []
for x in range(-r,r):
for y in range(-r,r):
if(x*x + y*y <= r*r) and (point[0]+x,point[1]+y) in cords:
neighbors.append((point[0]+x,point[1]+y))
return neighbors
def GetNeighborsAverage(point,r,cords):
neighbors = []
pAmount = 0
pXSum = 0
pYSum = 0
for x in range(-r,r):
for y in range(-r,r):
if(x*x + y*y <= r*r) and (point[0]+x,point[1]+y) in cords:
neighbors.append((point[0]+x,point[1]+y))
pAmount += 1
pXSum += point[0] + x
pYSum += point[1] + y
return (round(pXSum/pAmount),round(pYSum/pAmount)),neighbors
def Generate_GCODE(x,y,z,speed):
return f"G01 X{x} Y{y} F{speed}"
def ConvertPicToCords(image,thershold=50):
image = image.convert('L')
cords = []
for x in range(image.width):
for y in range(image.height):
p = image.getpixel((x, y))
if p <= thershold:
cords.append((x,y))
return cords
def CordsToGroups(cords):
checked = []
average_Points = []
for cord in cords:
if cord not in checked:
aCord,neighbors = GetNeighborsAverage(cord, 4,cords)
print(aCord)
average_Points.append(aCord)
checked.append(cord)
checked.extend(neighbors)
return average_Points,checked
def plot_points_on_img(points,img,color=(255,0,0)):
for cord in points:
img.putpixel(cord,color)
return img
img = Image.open("A.png")
cords = ConvertPicToCords(img)
averages,checked = CordsToGroups(cords)
print(checked)
Average_img = plot_points_on_img(averages,img)
Average_img.show()
checked_img = plot_points_on_img(checked,img)
checked_img.show()
# See PyCharm help at https://www.jetbrains.com/help/pycharm/