Последний раз редактировалось kotenok gav 28.05.2020, 19:19, всего редактировалось 4 раз(а).
Код: (вырезано из-за лимита символов)
Теперь [[(118, 35), (117, 35)], [(118, 34), (118, 35)], [(118, 36), (117, 36)], [(118, 35), (118, 36)], [(119, 34), (118, 34)], [(119, 35), (118, 35)], [(119, 36), (118, 36)], [(119, 37), (118, 37)], [(120, 29), (120, 30)], [(120, 31), (120, 32)], [(120, 34), (119, 34)], [(120, 35), (119, 35)], [(120, 36), (119, 36)], [(120, 37), (119, 37)], [(120, 37), (120, 38)], [(121, 29), (120, 29)], [(121, 28), (121, 29)], [(121, 29), (121, 30)], [(121, 31), (121, 32)], [(121, 35), (120, 35)], [(121, 36), (120, 36)], [(121, 35), (121, 36)], [(121, 39), (120, 39)], [(122, 28), (121, 28)], [(122, 27), (122, 28)], [(122, 29), (121, 29)], [(122, 35), (121, 35)], [(122, 36), (121, 36)], [(122, 37), (121, 37)], [(122, 40), (121, 40)], [(123, 27), (122, 27)], [(123, 26), (123, 27)], [(123, 28), (122, 28)], [(123, 37), (122, 37)], [(123, 38), (122, 38)], [(123, 39), (122, 39)], [(123, 40), (122, 40)], [(124, 26), (123, 26)], [(124, 27), (123, 27)], [(124, 37), (123, 37)], [(124, 38), (123, 38)], [(124, 39), (123, 39)], [(124, 40), (123, 40)], [(125, 26), (124, 26)], [(125, 25), (125, 26)], [(125, 27), (124, 27)], [(125, 39), (124, 39)], [(125, 40), (124, 40)], [(126, 25), (125, 25)], [(126, 26), (125, 26)], [(126, 39), (125, 39)], [(126, 40), (125, 40)], [(127, 25), (126, 25)], [(127, 24), (127, 25)], [(127, 26), (126, 26)], [(127, 39), (126, 39)], [(127, 40), (126, 40)], [(128, 24), (127, 24)], [(128, 25), (127, 25)], [(128, 39), (127, 39)], [(128, 41), (127, 41)], [(129, 24), (128, 24)], [(129, 25), (128, 25)], [(129, 40), (128, 40)], [(129, 41), (128, 41)], [(130, 24), (129, 24)], [(130, 25), (129, 25)], [(130, 40), (129, 40)], [(130, 41), (129, 41)], [(131, 24), (130, 24)], [(131, 25), (130, 25)], [(131, 40), (130, 40)], [(131, 41), (130, 41)], [(132, 24), (131, 24)], [(132, 25), (131, 25)], [(132, 39), (131, 39)], [(132, 41), (131, 41)], [(133, 24), (132, 24)], [(133, 25), (132, 25)], [(133, 39), (132, 39)], [(133, 40), (132, 40)], [(134, 24), (133, 24)], [(134, 25), (133, 25)], [(134, 38), (133, 38)], [(134, 40), (133, 40)], [(135, 24), (134, 24)], [(135, 26), (134, 26)], [(135, 34), (134, 34)], [(135, 35), (134, 35)], [(135, 38), (134, 38)], [(135, 39), (134, 39)], [(136, 25), (135, 25)], [(136, 26), (135, 26)], [(136, 34), (135, 34)], [(136, 35), (135, 35)], [(136, 38), (135, 38)], [(136, 39), (135, 39)], [(137, 25), (136, 25)], [(137, 27), (136, 27)], [(137, 34), (136, 34)], [(137, 35), (136, 35)], [(137, 38), (136, 38)], [(137, 39), (136, 39)], [(138, 26), (137, 26)], [(138, 28), (137, 28)], [(138, 28), (138, 29)], [(138, 34), (137, 34)], [(138, 36), (137, 36)], [(138, 38), (137, 38)], [(138, 39), (137, 39)], [(139, 27), (138, 27)], [(139, 27), (139, 28)], [(139, 30), (138, 30)], [(139, 35), (138, 35)], [(139, 36), (138, 36)], [(139, 38), (138, 38)], [(139, 39), (138, 39)], [(140, 29), (139, 29)], [(140, 31), (139, 31)], [(140, 35), (139, 35)], [(140, 36), (139, 36)], [(140, 37), (139, 37)], [(140, 39), (139, 39)], [(141, 30), (140, 30)], [(141, 31), (140, 31)], [(141, 32), (140, 32)], [(141, 33), (140, 33)], [(141, 33), (141, 34)], [(141, 35), (140, 35)], [(141, 38), (140, 38)], [(142, 30), (141, 30)], [(142, 34), (142, 35)], [(142, 37), (141, 37)], [(143, 30), (142, 30)], [(143, 30), (143, 31)], [(143, 32), (143, 33)], [(143, 34), (142, 34)]] и [[(121, 33), (121, 34), (121, 35)]] (хотя эта граница все равно, очевидно, незамкнутая...). -- 29 май 2020, 00:59 --А, я нашел баг. Самое обидное, что я его починил, но не заметил еще в трех местах его же... -- 29 май 2020, 01:04 --Код:
from PIL import Image, ImageDraw
def add(vector1, vector2):
return (vector1[0] + vector2[0], vector1[1] + vector2[1])
def substract(vector1, vector2):
return (vector1[0] - vector2[0], vector1[1] - vector2[1])
def sortByX(pixel):
return pixel[0]
def sortByY(pixel):
return pixel[1]
def neighbours(pixel):
return list(map(lambda x: add(pixel, x), [(0,1), (1, 0), (0, -1), (-1, 0)]))
def cyclicMap(function, iterable):
iterableList = []
for i in iterable:
iterableList.append(i)
mapList = []
for i in range(len(iterableList) - 1):
mapList.append(function(iterableList[i], iterableList[i + 1]))
mapList.append(function(iterableList[-1], iterableList[0]))
return mapList
def neighbourLines(pixel):
lines = cyclicMap(lambda x, y: [add(pixel, x), add(pixel, y)],
[(0, 0), (1, 0), (1, -1), (0, -1)])
for i in range(3, -1, -1):
if levelByPixel[add(pixel, {0: (0, -1), 1: (1, 0), 2: (0, 1),
3: (-1, 0)}[i])] == levelByPixel[pixel]:
del lines[i]
return lines
def neighbourPixels(line):
gradient = substract(line[1], line[0])
return list(map(lambda x: add(line[0], x), {(1, 0): [(0, -1), (0, 0)],
(-1, 0): [(-1, -1), (-1, 0)],
(0, 1): [(-1, 0), (0, 0)],
(0, -1):
[(-1, -1), (0, -1)]}[gradient]))
image = Image.open("test.png")
draw = ImageDraw.Draw(image)
width = image.size[0]
height = image.size[1]
pixels = image.load()
polygonPixels = []
for y in range(height):
for x in range(width):
pixel = pixels[x, y]
if pixel[0:3] == (0, 0, 255):
polygonPixels.append((x, y))
notVisited = polygonPixels
borders = []
SVG = ''
levelByPixel = {(0, 0): 0, (width - 1, 0): 0, (0, height - 1): 0, (width - 1, height - 1): 0}
polygonPixels.sort(key = sortByX)
levelByPixel[polygonPixels[0]] = 1
levelByPixel[polygonPixels[1]] = 1
polygonPixels.sort(key = sortByY)
levelByPixel[polygonPixels[0]] = 1
levelByPixel[polygonPixels[1]] = 1
levelNotSetted = [pixel for pixel in [(i, j) for i in range(width) for j in
range(height)] if not pixel in
levelByPixel.keys()]
levelSetted = list(levelByPixel.keys())
while levelNotSetted != []:
error = True
for pixel in levelSetted:
for pix in neighbours(pixel):
if ((pix in levelNotSetted) and
(pixels[pixel[0], pixel[1]][0:3]
== pixels[pix[0], pix[1]][0:3])):
error = False
levelNotSetted.remove(pix)
levelSetted.append(pix)
levelByPixel[pix] = levelByPixel[pixel]
if error:
for level in range(max(levelByPixel.values()) + 1):
for pixel in levelSetted:
if levelByPixel[pixel] == level:
for pix in neighbours(pixel):
if pix in levelNotSetted:
levelNotSetted.remove(pix)
levelSetted.append(pix)
levelByPixel[pix] = level + 1
for y in range(height):
s = ''
for x in range(width):
if (x, y) in levelSetted:
s += str(levelByPixel[(x, y)])
else:
s += '.'
print(s)
openPaths = []
closedPaths = []
checkedLines = []
for x in range(1, width - 1):
for y in range(1, height - 1):
for line in neighbourLines((x, y)):
if line in checkedLines:
continue
line2 = line
line2.reverse()
if line2 in checkedLines:
continue
begin = line[0]
end = line[1]
neighboursPixelsToLine = neighbourPixels([begin, end])
if (levelByPixel[neighboursPixelsToLine[0]] ==
levelByPixel[neighboursPixelsToLine[1]]):
continue
paths1 = [] #paths, such that path[1] of them is begin
paths2 = [] #paths, such that path[0] of them is end
for path in openPaths:
if path[1] == begin:
paths1.append(path)
elif path[0] == end:
paths2.append(path)
if (len(paths1) == 0) and (len(paths2) == 0):
openPaths.append(line)
elif len(paths1) > 0:
paths = []
for path in paths1:
neighboursPixelsToPath = neighbourPixels([path[0], path[1]])
if ((levelByPixel[neighboursPixelsToLine[0]] ==
levelByPixel[neighboursPixelsToPath[0]]) and
(levelByPixel[neighboursPixelsToLine[1]] ==
levelByPixel[neighboursPixelsToPath[1]])):
paths.append(path)
if (len(paths) == 0):
openPaths.append(line)
else:
path = paths[0]
openPaths.remove(path)
if path[0] == end:
closedPaths.append(path)
else:
if len(paths2) > 0:
paths = []
for path2 in paths2:
neighboursPixelsToPath =\
neighbourPixels([path2[0], path2[1]])
if ((levelByPixel[neighboursPixelsToLine[0]] ==
levelByPixel[neighboursPixelsToPath[0]]) and
(levelByPixel[neighboursPixelsToLine[1]] ==
levelByPixel[neighboursPixelsToPath[1]])):
paths.append(path2)
if len(paths) == 0:
path.append(end)
else:
path = path + paths[0]
openPaths.append(path)
elif len(paths2) > 0:
paths = []
for path in paths2:
neighboursPixelsToPath = neighbourPixels([path[0], path[1]])
if ((levelByPixel[neighboursPixelsToLine[0]] ==
levelByPixel[neighboursPixelsToPath[0]]) and
(levelByPixel[neighboursPixelsToLine[1]] ==
levelByPixel[neighboursPixelsToPath[1]])):
paths.append(path)
if (len(paths) == 0):
openPaths.append(line)
else:
path = paths[0]
openPaths.remove(path)
if path[1] == begin:
closedPaths.append(path)
else:
if len(paths1) > 0:
paths = []
for path1 in paths1:
neighboursPixelsToPath =\
neighbourPixels([path1[0], path1[1]])
if ((levelByPixel[neighboursPixelsToLine[0]] ==
levelByPixel[neighboursPixelsToPath[0]]) and
(levelByPixel[neighboursPixelsToLine[1]] ==
levelByPixel[neighboursPixelsToPath[1]])):
paths.append(path1)
if len(paths) == 0:
path = [begin] + path
else:
path = paths[0] + path
openPaths.append(path)
checkedLines.append(line)
print(openPaths, closedPaths)
[[(118, 35), (117, 35)], [(118, 36), (117, 36)], [(118, 35), (118, 36)], [(120, 31), (120, 32)], [(119, 34), (118, 34)], [(119, 36), (118, 36)], [(119, 37), (118, 37)], [(120, 37), (120, 38)], [(120, 29), (120, 30)], [(121, 31), (121, 32)], [(120, 35), (119, 35)], [(121, 33), (121, 34)], [(121, 36), (120, 36)], [(121, 39), (120, 39)], [(121, 28), (121, 29)], [(121, 29), (121, 30)], [(121, 35), (121, 36)], [(122, 36), (121, 36)], [(122, 27), (122, 28)], [(123, 28), (122, 28)], [(122, 37), (121, 37)], [(122, 40), (121, 40)], [(123, 26), (123, 27)], [(124, 37), (123, 37)], [(123, 38), (122, 38)], [(123, 39), (122, 39)], [(125, 26), (124, 26)], [(124, 27), (123, 27)], [(124, 40), (123, 40)], [(125, 39), (124, 39)], [(126, 25), (125, 25)], [(126, 26), (125, 26)], [(126, 40), (125, 40)], [(127, 24), (127, 25)], [(127, 39), (126, 39)], [(128, 25), (127, 25)], [(128, 41), (127, 41)], [(129, 24), (128, 24)], [(129, 40), (128, 40)], [(130, 25), (129, 25)], [(131, 40), (130, 40)], [(130, 41), (129, 41)], [(131, 24), (130, 24)], [(132, 41), (131, 41)], [(132, 25), (131, 25)], [(132, 39), (131, 39)], [(133, 24), (132, 24)], [(134, 25), (133, 25)], [(133, 40), (132, 40)], [(135, 24), (134, 24)], [(134, 38), (133, 38)], [(135, 26), (134, 26)], [(135, 34), (134, 34)], [(135, 35), (134, 35)], [(135, 39), (134, 39)], [(136, 25), (135, 25)], [(137, 27), (136, 27)], [(137, 35), (136, 35)], [(136, 38), (135, 38)], [(138, 26), (137, 26)], [(138, 28), (137, 28)], [(138, 28), (138, 29)], [(137, 34), (136, 34)], [(137, 39), (136, 39)], [(139, 27), (138, 27)], [(139, 27), (139, 28)], [(139, 30), (138, 30)], [(138, 36), (137, 36)], [(138, 38), (137, 38)], [(140, 29), (139, 29)], [(139, 35), (138, 35)], [(140, 36), (139, 36)], [(140, 37), (139, 37)], [(139, 39), (138, 39)], [(140, 31), (139, 31)], [(141, 32), (140, 32)], [(141, 33), (140, 33)], [(141, 33), (141, 34)], [(141, 35), (140, 35)], [(141, 38), (140, 38)], [(141, 30), (140, 30)], [(142, 37), (141, 37)], [(143, 30), (142, 30)], [(143, 30), (143, 31)], [(143, 32), (143, 33)], [(142, 34), (142, 35)]] и [], все равно не так... -- 29 май 2020, 01:49 --Хм, вот вижу, что, скажем, есть линия [(123, 39), (122, 39)], затем линия [(124, 39), (123, 39)] почему-то не добавляется, затем линия [(125, 39), (124, 39)] добавляется отдельно.
|