Skip to content

Instantly share code, notes, and snippets.

@aflaag
Last active March 21, 2024 09:22
Show Gist options
  • Save aflaag/c60382752b743c85dfa99b83a213a835 to your computer and use it in GitHub Desktop.
Save aflaag/c60382752b743c85dfa99b83a213a835 to your computer and use it in GitHub Desktop.
import images
def ex(file_dati, file_png, spaziatura):
dimensions = data(file_dati)
biggest = tuple(map(lambda line: (sum(map(lambda building: building[0], line)) + spaziatura * (len(line) + 1), max(map(lambda building: building[1], line))), dimensions))
w = max(map(lambda t: t[0], biggest))
h = spaziatura * (len(dimensions) + 1) + sum(map(lambda t: t[1], biggest))
black_stripe = [(0, 0, 0)] * w
picture = list(map(lambda _: black_stripe.copy(), range(h)))
curr_y = spaziatura
center_x = w >> 1
for i in range(len(dimensions)):
line = dimensions[i]
len_line_1 = len(line) - 1
curr_biggest = biggest[i]
highest_building = curr_biggest[1]
if len_line_1:
interval = (w - curr_biggest[0]) // len_line_1 + spaziatura
curr_x = spaziatura
else:
interval = 0
curr_x = center_x - (line[0][0] >> 1)
for j in range(len_line_1 + 1):
building = line[j]
width = building[0]
height = building[1]
y = ((highest_building - height) >> 1) + curr_y
offset = curr_x + width
stripe = [dimensions[i][j][2:5]] * width
for Y in range(height):
picture[y + Y][curr_x:offset] = stripe
curr_x += width + interval
curr_y += spaziatura + highest_building
images.save(picture, file_png)
return w, h
def data(file_dati):
return [[tuple(line[(j << 2) + j:(j << 2) + j + 5]) for j in range(len(line) // 5)] for line in list(map(lambda line: list(map(int, line.translate({44: 32, 9: 32, 10: 32}).split())), open(file_dati, encoding="utf-8").readlines()))]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment