Skip to content

Instantly share code, notes, and snippets.

@bunker-inspector
Created October 9, 2019 17:48
Show Gist options
  • Save bunker-inspector/518fb6358e379db27de902e310bac9fd to your computer and use it in GitHub Desktop.
Save bunker-inspector/518fb6358e379db27de902e310bac9fd to your computer and use it in GitHub Desktop.
Creates bitmaps of simple simulated blots of ink
import sys
import imageio
from datetime import datetime
def gen_map(blots, rows, cols, normal):
grid = [[255] * cols for _ in range(rows)]
def blot_and_bleed(blot, grid, rows, cols, normal):
row,col,dark = blot
if row < 0 or col < 0 or row >= rows or col >= cols:
return grid
dark_normalized = int(255 - (dark * normal))
if dark_normalized < grid[row][col]:
grid[row][col] = dark_normalized
else:
return grid
grid = blot_and_bleed((row+1, col, dark-1), grid, cols, rows, normal)
grid = blot_and_bleed((row-1, col, dark-1), grid, cols, rows, normal)
grid = blot_and_bleed((row, col+1, dark-1), grid, cols, rows, normal)
grid = blot_and_bleed((row, col-1, dark-1), grid, cols, rows, normal)
return grid
for blot in blots:
grid = blot_and_bleed(blot, grid, rows, cols, normal)
return grid
if __name__ == '__main__':
num_maps = int(sys.stdin.readline().strip())
time_suffix = datetime.now().strftime('%d-%m-%Y-%H:%M:%S')
for i in range(num_maps):
rows, cols = list(map(int, sys.stdin.readline().strip().split(' ')))
num_blots = int(sys.stdin.readline().strip())
blots = []
max_dark = float('-inf')
for _ in range(num_blots):
row, col, dark = list(map(int, sys.stdin.readline().strip().split(' ')))
if dark > max_dark:
max_dark = dark
blots.append((row,col,dark))
normal_const = 1 / (max_dark / 255)
grid = gen_map(blots, rows, cols, normal_const)
output_name = 'blotmap_%s_%d.bmp' % (time_suffix, i+1)
imageio.imwrite(output_name, grid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment