Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PIL import Image, ImageDraw, ImageFilter
- def percents_to_complete(a: int, z: int, pc: int, r=5):
- if round(a / z * 100) > pc:
- pc = round(a / z * 100)
- if pc % r == 0:
- print(' '*4, pc, '%', sep='')
- return pc
- def a():
- pxls = []
- png_map = Image.open("map.png")
- pix = png_map.load()
- c = 0
- colors_list = [(0, 0, 0, 255), # границы
- (187, 203, 255, 255), # вода
- (98, 98, 98, 255), # водные границы
- # (255, 255, 255, 255), # города/ресы
- (174, 216, 243),
- (174, 216, 243, 255),
- (0, 0, 0),
- (214, 214, 214, 255), # пустыня
- ]
- tile_fill_rel = []
- f = open('tile_fill_rel.txt', 'w')
- c = 0
- pc = 0
- for width in range(png_map.size[0]):
- for height in range(png_map.size[1]):
- if pix[width, height] not in colors_list: # для тестов - and pix[width, height] != (19, 131, 20, 255)
- tile_fill_rel.append([[width, height], pix[width, height]])
- ImageDraw.floodfill(png_map, (width, height), ((c // 400) * 20 + 50, ((c // 20) % 20) * 7 + 50, (c % 20) * 7 + 50, 255))
- colors_list.append(pix[width, height])
- c += 1
- pc = percents_to_complete(width, png_map.size[0], pc)
- print()
- png_map.save('res1.png')
- for e in tile_fill_rel:
- f.write(str(e[0][0]) + ' ')
- f.write(str(e[0][1]) + ' ')
- f.write('{:02x}{:02x}{:02x}'.format(*e[1]))
- f.write('\n')
- colors_to_del = [(0, 0, 0, 255), (0, 0, 0)]
- for i in range(3):
- print('a', i, ':', sep="")
- pc = 0
- for width in range(png_map.size[0] - 1):
- for height in range(png_map.size[1] - 1):
- ar = [pix[width+0, height+1], pix[width+1, height+0]]
- for e in ar:
- if pix[width, height] in colors_to_del and e in colors_list[5:]:
- # ImageDraw.Draw(png_map).point((width, height), e)
- ImageDraw.Draw(png_map).point((width, height), e)
- pc = percents_to_complete(width, png_map.size[0], pc)
- print()
- pc = 0
- for width in range(png_map.size[0] - 1, 1, -1):
- for height in range(png_map.size[1] - 1, 1, -1):
- ar = [pix[width+0, height-1], pix[width-1, height+0]]
- for e in ar:
- if pix[width, height] in colors_to_del and e in colors_list[5:]:
- # ImageDraw.Draw(png_map).point((width, height), e)
- ImageDraw.Draw(png_map).point((width, height), e)
- pc = percents_to_complete(width, png_map.size[0], pc)
- print()
- png_map.save('res2.png')
- pc = 0
- f = open('tile_fill_rel.txt', 'r')
- # tile_fill_rel = []
- # for line in f:
- # s = line.split(' ')
- # tile_fill_rel.append([[int(s[0]), int(s[1])], (int(s[2]), int(s[3]), int(s[4]), int(s[5]))])
- for i in range(len(tile_fill_rel)):
- ImageDraw.floodfill(png_map, (tile_fill_rel[i][0][0], tile_fill_rel[i][0][1]), tile_fill_rel[i][1])
- pc = percents_to_complete(i, len(tile_fill_rel), pc, r=20)
- png_map.save('res3.png')
- print()
- '''
- print(':root {')
- for item in colors_list[4:]:
- print(' --v: #{:02x}{:02x}{:02x};'.format(*item))
- print('}')
- print()
- for item in colors_list:
- print(item)
- '''
- '''
- if pix[width, height] == (255, 255, 255, 255):
- g = c // 256 % 256
- b = c % 256
- ImageDraw.floodfill(map, (width, height), (0, g * 16, b, 255))
- pxls.append([width, height])
- c += 1
- pxls = []
- for i in range(256):
- pxls.append([])
- for j in range(256):
- pxls[i].append([])
- for width in range(map.size[0]):
- for height in range(map.size[1]):
- r = pix[width, height][0]
- g = pix[width, height][1]
- b = pix[width, height][2]
- if r == 0:
- pxls[g][b].append([width, height])
- ImageDraw.Draw(map).point((width, height), (255, 255, 255, 255))
- return pxls
- '''
- def main(pxls, source_img):
- pass
- '''
- blank_map = Image.open("blank_map.png")
- pix = blank_map.load()
- ba_frame = source_img
- ba_pix = ba_frame.load()
- for i in range(len(pxls)):
- for j in range(len(pxls[i])):
- c = 0
- s = 0
- dot = pxls[i][j]
- if not dot:
- continue
- for pixel in dot:
- c += 1
- if type(ba_pix[pixel[0] + 30, pixel[1]]) == int:
- s += ba_pix[pixel[0] + 30, pixel[1]]
- else:
- s += ba_pix[pixel[0] + 30, pixel[1]][0]
- if s // c < 128:
- ImageDraw.floodfill(blank_map, (pixel[0], pixel[1]), (0, 0, 0, 255))
- k = ba_frame.convert("RGB")
- k = k.filter(ImageFilter.GaussianBlur(5))
- k = k.load()
- for width in range(blank_map.size[0]):
- for height in range(blank_map.size[1]):
- if pix[width, height] == (88, 123, 158, 255):
- r = int(k[width + 30, height][0] * 0.35 + 88 * 0.65)
- g = int(k[width + 30, height][1] * 0.35 + 123 * 0.65)
- b = int(k[width + 30, height][2] * 0.35 + 158 * 0.65)
- ImageDraw.Draw(blank_map).point((width, height), (r, g, b, 255))
- return blank_map
- '''
- if __name__ == "__main__":
- print('a:')
- a()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement