from PIL import Image, ImageDraw
S = 4 W, H = 96, 64
OUTLINE = (52, 38, 28, 255)
WHITE = (255, 255, 255, 255)
def canvas():
img = Image.new("RGBA", (W * S, H * S), (0, 0, 0, 0))
return img, ImageDraw.Draw(img)
def save(img, name):
img = img.resize((W, H), Image.LANCZOS)
img.save(f"assets/sprites/flag_{name}.png")
print(f"flag_{name}")
def px(*vals):
return tuple(v * S for v in vals)
def border(d):
d.rectangle([0, 0, W * S - 1, H * S - 1], outline=OUTLINE, width=S)
def bands(name, colours, vertical, weights=None):
img, d = canvas()
weights = weights or [1] * len(colours)
span = W if vertical else H
total = sum(weights)
at = 0
last = len(colours) - 1
for index, (colour, weight) in enumerate(zip(colours, weights)):
end = span if index == last else at + span * weight / total
if vertical:
d.rectangle([px(at, 0), px(end, H)], fill=colour)
else:
d.rectangle([px(0, at), px(W, end)], fill=colour)
at = end
border(d)
save(img, name)
UJ_BLUE = (1, 33, 105, 255)
UJ_RED = (200, 16, 46, 255)
img, d = canvas()
d.rectangle([px(0, 0), px(W, H)], fill=UJ_BLUE)
for width, colour in ((13, WHITE), (5, UJ_RED)):
d.line([px(0, 0), px(W, H)], fill=colour, width=width * S)
d.line([px(0, H), px(W, 0)], fill=colour, width=width * S)
for arm, colour in ((11, WHITE), (7, UJ_RED)):
d.rectangle([px(0, (H - arm) / 2), px(W, (H + arm) / 2)], fill=colour)
d.rectangle([px((W - arm) / 2, 0), px((W + arm) / 2, H)], fill=colour)
border(d)
save(img, "en")
bands("fr", [(0, 35, 149, 255), WHITE, (237, 41, 57, 255)], vertical=True)
bands("de", [(0, 0, 0, 255), (221, 0, 0, 255), (255, 206, 0, 255)], vertical=False)
bands("it", [(0, 140, 69, 255), (244, 245, 240, 255), (205, 33, 42, 255)], vertical=True)
bands("nl", [(174, 28, 40, 255), WHITE, (33, 70, 139, 255)], vertical=False)
bands("ru", [WHITE, (0, 57, 166, 255), (213, 43, 30, 255)], vertical=False)
img, d = canvas()
d.rectangle([px(0, 0), px(W, H)], fill=WHITE)
disc = H * 0.6
d.ellipse(
[px((W - disc) / 2, (H - disc) / 2), px((W + disc) / 2, (H + disc) / 2)],
fill=(188, 0, 45, 255),
)
border(d)
save(img, "ja")
bands(
"es",
[(170, 21, 27, 255), (241, 191, 0, 255), (170, 21, 27, 255)],
vertical=False,
weights=[1, 2, 1],
)