Advertisement
Guest User

часть 2

a guest
Aug 21st, 2022
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.47 KB | None | 0 0
  1. import json
  2. import re
  3.  
  4. from webcolors import rgb_to_hex, hex_to_rgb
  5. from PIL import Image, ImageDraw
  6.  
  7.  
  8. def main():
  9.     source_map = Image.open("map.png")
  10.     pix = source_map.load()
  11.     names_list = []
  12.     colors_list = []
  13.     cords_list = []
  14.     colors_ignore_list = [(0, 0, 0, 255), (0, 0, 0), (174, 216, 243, 255), (174, 216, 243)]
  15.  
  16.     print('stage 1:')
  17.     i = 0
  18.     for width in range(source_map.size[0]):
  19.         for height in range(source_map.size[1]):
  20.             if pix[width, height] not in colors_list and \
  21.                pix[width, height] not in colors_ignore_list:
  22.                 names_list.append('id' + '0' * (3 - len(str(i))) + str(i))
  23.                 colors_list.append(pix[width, height])
  24.                 cords_list.append((width, height))
  25.                 i += 1
  26.         print('   ', width, '/', source_map.size[0])
  27.     print('stage 1 passed\n')
  28.  
  29.     tiles = []
  30.     for i in range(len(colors_list)):
  31.         tile = {'id': names_list[i],
  32.                 'color_id': rgb_to_hex(colors_list[i][:3]),
  33.                 'x': cords_list[i][0],
  34.                 'y': cords_list[i][1],
  35.                 'routes': [],
  36.                 }
  37.         tiles.append(tile)
  38.  
  39.     test_map = Image.open("ports.png")
  40.     pix = test_map.load()
  41.     ports = []
  42.     for i in range(len(tiles)):
  43.         if pix[tiles[i]['x'], tiles[i]['y']] == (0, 0, 255, 255):
  44.             ports.append(tiles[i]['id'])
  45.  
  46.     for i in range(len(tiles)):
  47.         if tiles[i]['id'] in ports:
  48.             for port in ports:
  49.                 if port != tiles[i]['id']:
  50.                     tiles[i]['routes'].append(port)
  51.  
  52.     tiles_map = Image.open("tiles.png")
  53.     pix = tiles_map.load()
  54.  
  55.     print('stage 2:')
  56.     for i in range(len(tiles)):
  57.         found_routes = []
  58.         color = hex_to_rgb(tiles[i]['color_id'])
  59.         color = (color.red, color.green, color.blue, 255)
  60.         print('   ', i, '/', len(tiles))
  61.         for width in range(1, source_map.size[0]-1):
  62.             for height in range(1, source_map.size[1]-1):
  63.                 neighbours_list = [pix[width+1, height], pix[width, height+1],
  64.                                    pix[width-1, height], pix[width, height-1],
  65.                                    pix[width+1, height+1], pix[width-1, height-1],
  66.                                    pix[width+1, height-1], pix[width-1, height+1]]
  67.                 for neighbour in neighbours_list:
  68.                     if pix[width, height] == color and (neighbour != color and neighbour not in colors_ignore_list):
  69.                         route_to = next(t for t in tiles if t['color_id'] == rgb_to_hex(neighbour[:3]))['id']
  70.                         if route_to not in found_routes:
  71.                             found_routes.append(route_to)
  72.                             tiles[i]['routes'].append(route_to)
  73.     print('stage 2 passed\n')
  74.  
  75.  
  76. def sub():
  77.     tiles = json.load(open('temp02.json'))
  78.  
  79.     test_map = Image.open("map2.png")
  80.  
  81.     for tile in tiles:
  82.         r = tile['routes']
  83.         if len(tile['routes']) >= 31:
  84.             r = tile['routes'][31:]
  85.         for route in r:
  86.             t = next(t for t in tiles if t['id'] == route)
  87.             ImageDraw.Draw(test_map).line([(tile['x'], tile['y']), (t['x'], t['y'])], (100, 100, 100, 255))
  88.  
  89.     test_map.save('res1.png')
  90.     test_map = Image.open("res1.png")
  91.     for tile in tiles:
  92.         ImageDraw.Draw(test_map).rectangle([(tile['x'] - 1, tile['y'] - 1), (tile['x'] + 1, tile['y'] + 1)],
  93.                                            (255, 0, 0))
  94.         ImageDraw.Draw(test_map).text((tile['x'] + 3, tile['y'] - 5), tile['id'], fill="#ff8080")
  95.     test_map.save('res999.png')
  96.  
  97.     with open('temp02.json', 'w') as f:
  98.         json.dump(tiles, f, indent=2)
  99.  
  100.  
  101. if __name__ == "__main__":
  102.  
  103.     # main()
  104.  
  105.     tiles = json.load(open('temp02.json'))
  106.     for i in range(len(tiles)):
  107.         id0 = re.split(r'([a-z])', tiles[i]['id'])
  108.         id1 = '0' * (2 - len(id0[0])) + id0[0]
  109.         for j in range(1, len(id0)):
  110.             id1 = id1 + id0[j]
  111.         tiles[i] = {
  112.             'id': id1,
  113.             'x': tiles[i]['x'],
  114.             'y': tiles[i]['y'],
  115.             'routes': tiles[i]['routes'],
  116.         }
  117.     tiles = sorted(tiles, key=lambda d: d['id'])
  118.     for i in range(len(tiles)):
  119.         while True:
  120.             if tiles[i]['id'][0] == '0':
  121.                 tiles[i]['id'] = tiles[i]['id'][1:]
  122.             else:
  123.                 break
  124.  
  125.     with open('final_version.json', 'w') as f:
  126.         json.dump(tiles, f, indent=2)
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement