Advertisement
Guest User

часть 1

a guest
Aug 21st, 2022
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.71 KB | None | 0 0
  1. from PIL import Image, ImageDraw, ImageFilter
  2.  
  3.  
  4. def percents_to_complete(a: int, z: int, pc: int, r=5):
  5.     if round(a / z * 100) > pc:
  6.         pc = round(a / z * 100)
  7.         if pc % r == 0:
  8.             print(' '*4, pc, '%', sep='')
  9.     return pc
  10.  
  11.  
  12. def a():
  13.     pxls = []
  14.     png_map = Image.open("map.png")
  15.     pix = png_map.load()
  16.     c = 0
  17.     colors_list = [(0, 0, 0, 255),        # границы
  18.                    (187, 203, 255, 255),  # вода
  19.                    (98, 98, 98, 255),     # водные границы
  20.                    # (255, 255, 255, 255),  # города/ресы
  21.                    (174, 216, 243),
  22.                    (174, 216, 243, 255),
  23.                    (0, 0, 0),
  24.                    (214, 214, 214, 255),  # пустыня
  25.                    ]
  26.     tile_fill_rel = []
  27.     f = open('tile_fill_rel.txt', 'w')
  28.     c = 0
  29.     pc = 0
  30.     for width in range(png_map.size[0]):
  31.         for height in range(png_map.size[1]):
  32.             if pix[width, height] not in colors_list:  # для тестов - and pix[width, height] != (19, 131, 20, 255)
  33.                 tile_fill_rel.append([[width, height], pix[width, height]])
  34.                 ImageDraw.floodfill(png_map, (width, height), ((c // 400) * 20 + 50, ((c // 20) % 20) * 7 + 50, (c % 20) * 7 + 50, 255))
  35.                 colors_list.append(pix[width, height])
  36.                 c += 1
  37.         pc = percents_to_complete(width, png_map.size[0], pc)
  38.     print()
  39.     png_map.save('res1.png')
  40.     for e in tile_fill_rel:
  41.         f.write(str(e[0][0]) + ' ')
  42.         f.write(str(e[0][1]) + ' ')
  43.         f.write('{:02x}{:02x}{:02x}'.format(*e[1]))
  44.         f.write('\n')
  45.  
  46.     colors_to_del = [(0, 0, 0, 255), (0, 0, 0)]
  47.     for i in range(3):
  48.         print('a', i, ':', sep="")
  49.         pc = 0
  50.         for width in range(png_map.size[0] - 1):
  51.             for height in range(png_map.size[1] - 1):
  52.  
  53.                 ar = [pix[width+0, height+1], pix[width+1, height+0]]
  54.                 for e in ar:
  55.                     if pix[width, height] in colors_to_del and e in colors_list[5:]:
  56.                         # ImageDraw.Draw(png_map).point((width, height), e)
  57.                         ImageDraw.Draw(png_map).point((width, height), e)
  58.  
  59.             pc = percents_to_complete(width, png_map.size[0], pc)
  60.         print()
  61.         pc = 0
  62.         for width in range(png_map.size[0] - 1, 1, -1):
  63.             for height in range(png_map.size[1] - 1, 1, -1):
  64.  
  65.                 ar = [pix[width+0, height-1], pix[width-1, height+0]]
  66.                 for e in ar:
  67.                     if pix[width, height] in colors_to_del and e in colors_list[5:]:
  68.                         # ImageDraw.Draw(png_map).point((width, height), e)
  69.                         ImageDraw.Draw(png_map).point((width, height), e)
  70.  
  71.             pc = percents_to_complete(width, png_map.size[0], pc)
  72.         print()
  73.     png_map.save('res2.png')
  74.  
  75.     pc = 0
  76.     f = open('tile_fill_rel.txt', 'r')
  77.     # tile_fill_rel = []
  78.     # for line in f:
  79.     #     s = line.split(' ')
  80.     #     tile_fill_rel.append([[int(s[0]), int(s[1])], (int(s[2]), int(s[3]), int(s[4]), int(s[5]))])
  81.     for i in range(len(tile_fill_rel)):
  82.         ImageDraw.floodfill(png_map, (tile_fill_rel[i][0][0], tile_fill_rel[i][0][1]), tile_fill_rel[i][1])
  83.         pc = percents_to_complete(i, len(tile_fill_rel), pc, r=20)
  84.     png_map.save('res3.png')
  85.     print()
  86.  
  87.     '''
  88.    print(':root {')
  89.    for item in colors_list[4:]:
  90.        print('  --v: #{:02x}{:02x}{:02x};'.format(*item))
  91.    print('}')
  92.    print()
  93.    for item in colors_list:
  94.        print(item)
  95.    '''
  96.  
  97.     '''
  98.            if pix[width, height] == (255, 255, 255, 255):
  99.                g = c // 256 % 256
  100.                b = c % 256
  101.                ImageDraw.floodfill(map, (width, height), (0, g * 16, b, 255))
  102.                pxls.append([width, height])
  103.                c += 1
  104.  
  105.    pxls = []
  106.    for i in range(256):
  107.        pxls.append([])
  108.        for j in range(256):
  109.            pxls[i].append([])
  110.    for width in range(map.size[0]):
  111.        for height in range(map.size[1]):
  112.            r = pix[width, height][0]
  113.            g = pix[width, height][1]
  114.            b = pix[width, height][2]
  115.            if r == 0:
  116.                pxls[g][b].append([width, height])
  117.                ImageDraw.Draw(map).point((width, height), (255, 255, 255, 255))
  118.  
  119.    return pxls
  120.    '''
  121.  
  122.  
  123. def main(pxls, source_img):
  124.     pass
  125.     '''
  126.    blank_map = Image.open("blank_map.png")
  127.    pix = blank_map.load()
  128.    ba_frame = source_img
  129.    ba_pix = ba_frame.load()
  130.    for i in range(len(pxls)):
  131.        for j in range(len(pxls[i])):
  132.            c = 0
  133.            s = 0
  134.            dot = pxls[i][j]
  135.            if not dot:
  136.                continue
  137.            for pixel in dot:
  138.                c += 1
  139.                if type(ba_pix[pixel[0] + 30, pixel[1]]) == int:
  140.                    s += ba_pix[pixel[0] + 30, pixel[1]]
  141.                else:
  142.                    s += ba_pix[pixel[0] + 30, pixel[1]][0]
  143.            if s // c < 128:
  144.                ImageDraw.floodfill(blank_map, (pixel[0], pixel[1]), (0, 0, 0, 255))
  145.  
  146.    k = ba_frame.convert("RGB")
  147.    k = k.filter(ImageFilter.GaussianBlur(5))
  148.  
  149.    k = k.load()
  150.    for width in range(blank_map.size[0]):
  151.        for height in range(blank_map.size[1]):
  152.            if pix[width, height] == (88, 123, 158, 255):
  153.                r = int(k[width + 30, height][0] * 0.35 + 88 * 0.65)
  154.                g = int(k[width + 30, height][1] * 0.35 + 123 * 0.65)
  155.                b = int(k[width + 30, height][2] * 0.35 + 158 * 0.65)
  156.                ImageDraw.Draw(blank_map).point((width, height), (r, g, b, 255))
  157.  
  158.    return blank_map
  159.    '''
  160.  
  161.  
  162. if __name__ == "__main__":
  163.     print('a:')
  164.     a()
  165.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement