Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- import re
- from webcolors import rgb_to_hex, hex_to_rgb
- from PIL import Image, ImageDraw
- def main():
- source_map = Image.open("map.png")
- pix = source_map.load()
- names_list = []
- colors_list = []
- cords_list = []
- colors_ignore_list = [(0, 0, 0, 255), (0, 0, 0), (174, 216, 243, 255), (174, 216, 243)]
- print('stage 1:')
- i = 0
- for width in range(source_map.size[0]):
- for height in range(source_map.size[1]):
- if pix[width, height] not in colors_list and \
- pix[width, height] not in colors_ignore_list:
- names_list.append('id' + '0' * (3 - len(str(i))) + str(i))
- colors_list.append(pix[width, height])
- cords_list.append((width, height))
- i += 1
- print(' ', width, '/', source_map.size[0])
- print('stage 1 passed\n')
- tiles = []
- for i in range(len(colors_list)):
- tile = {'id': names_list[i],
- 'color_id': rgb_to_hex(colors_list[i][:3]),
- 'x': cords_list[i][0],
- 'y': cords_list[i][1],
- 'routes': [],
- }
- tiles.append(tile)
- test_map = Image.open("ports.png")
- pix = test_map.load()
- ports = []
- for i in range(len(tiles)):
- if pix[tiles[i]['x'], tiles[i]['y']] == (0, 0, 255, 255):
- ports.append(tiles[i]['id'])
- for i in range(len(tiles)):
- if tiles[i]['id'] in ports:
- for port in ports:
- if port != tiles[i]['id']:
- tiles[i]['routes'].append(port)
- tiles_map = Image.open("tiles.png")
- pix = tiles_map.load()
- print('stage 2:')
- for i in range(len(tiles)):
- found_routes = []
- color = hex_to_rgb(tiles[i]['color_id'])
- color = (color.red, color.green, color.blue, 255)
- print(' ', i, '/', len(tiles))
- for width in range(1, source_map.size[0]-1):
- for height in range(1, source_map.size[1]-1):
- neighbours_list = [pix[width+1, height], pix[width, height+1],
- pix[width-1, height], pix[width, height-1],
- pix[width+1, height+1], pix[width-1, height-1],
- pix[width+1, height-1], pix[width-1, height+1]]
- for neighbour in neighbours_list:
- if pix[width, height] == color and (neighbour != color and neighbour not in colors_ignore_list):
- route_to = next(t for t in tiles if t['color_id'] == rgb_to_hex(neighbour[:3]))['id']
- if route_to not in found_routes:
- found_routes.append(route_to)
- tiles[i]['routes'].append(route_to)
- print('stage 2 passed\n')
- def sub():
- tiles = json.load(open('temp02.json'))
- test_map = Image.open("map2.png")
- for tile in tiles:
- r = tile['routes']
- if len(tile['routes']) >= 31:
- r = tile['routes'][31:]
- for route in r:
- t = next(t for t in tiles if t['id'] == route)
- ImageDraw.Draw(test_map).line([(tile['x'], tile['y']), (t['x'], t['y'])], (100, 100, 100, 255))
- test_map.save('res1.png')
- test_map = Image.open("res1.png")
- for tile in tiles:
- ImageDraw.Draw(test_map).rectangle([(tile['x'] - 1, tile['y'] - 1), (tile['x'] + 1, tile['y'] + 1)],
- (255, 0, 0))
- ImageDraw.Draw(test_map).text((tile['x'] + 3, tile['y'] - 5), tile['id'], fill="#ff8080")
- test_map.save('res999.png')
- with open('temp02.json', 'w') as f:
- json.dump(tiles, f, indent=2)
- if __name__ == "__main__":
- # main()
- tiles = json.load(open('temp02.json'))
- for i in range(len(tiles)):
- id0 = re.split(r'([a-z])', tiles[i]['id'])
- id1 = '0' * (2 - len(id0[0])) + id0[0]
- for j in range(1, len(id0)):
- id1 = id1 + id0[j]
- tiles[i] = {
- 'id': id1,
- 'x': tiles[i]['x'],
- 'y': tiles[i]['y'],
- 'routes': tiles[i]['routes'],
- }
- tiles = sorted(tiles, key=lambda d: d['id'])
- for i in range(len(tiles)):
- while True:
- if tiles[i]['id'][0] == '0':
- tiles[i]['id'] = tiles[i]['id'][1:]
- else:
- break
- with open('final_version.json', 'w') as f:
- json.dump(tiles, f, indent=2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement