非常に役に立た無いプログラムを作ってみました。

何処かで架空の国旗を作った
みたいな話が出ていたので
自分も架空の国旗作りに挑戦してみました。

解説動画はこちら




コードはこちら
from PIL import Image, ImageDraw, ImageFont
import matplotlib.pyplot as plt
import numpy as np
import random
%matplotlib inline

size  = (320, 180)
c,o = (0,64,128,192,255) , (32,96,160,224)
colors = [(r,g,b) for r in c for g in c for b in c]
ol_colors = [(r,g,b) for r in o for g in o for b in o]
base_color = random.choice(colors)

def base():
    im =  Image.new('RGB',size)
    target = random.randint(1,6)
    # 配色
    rgb = (random.choice(colors),random.choice(colors),random.choice(colors))
    # べた塗り
    if target ==1:
        [im.putpixel((x,y),rgb[0]) for x in range(size[0]) for y in range(size[1])]
    # 縦半分
    elif target ==2:
        [im.putpixel((x,y),rgb[0]) for x in range(0,size[0]//2) for y in range(size[1])]
        [im.putpixel((x,y),rgb[1]) for x in range(size[0]//2,size[0]) for y in range(size[1])]
    # 横半分
    elif target ==3:
        [im.putpixel((x,y),rgb[0]) for x in range(size[0]) for y in range(0,size[1]//2)]
        [im.putpixel((x,y),rgb[1]) for x in range(size[0]) for y in range(size[1]//2,size[1])]
    # 縦3分
    elif target ==4:
        [im.putpixel((x,y),rgb[0]) for x in range(0,size[0]//3) for y in range(size[1])]
        [im.putpixel((x,y),rgb[1]) for x in range(size[0]//3,size[0]//3*2) for y in range(size[1])]
        [im.putpixel((x,y),rgb[2]) for x in range(size[0]//3*2,size[0]) for y in range(size[1])]
    # 横3分
    elif target ==5:
        [im.putpixel((x,y),rgb[0]) for x in range(size[0]) for y in range(0,size[1]//3)]
        [im.putpixel((x,y),rgb[1]) for x in range(size[0]) for y in range(size[1]//3,size[1]//3*2)]
        [im.putpixel((x,y),rgb[2]) for x in range(size[0]) for y in range(size[1]//3*2,size[1])]
    # 4ブロック
    elif target ==2:
        [im.putpixel((x,y),rgb[0]) for x in range(0,size[0]//2) for y in range(0,size[1]//2)]
        [im.putpixel((x,y),rgb[1]) for x in range(size[0]//2,size[0]) for y in range(0,size[1]//2)]
        [im.putpixel((x,y),rgb[1]) for x in range(0,size[0]//2) for y in range(size[1]//2,size[1])]
        [im.putpixel((x,y),rgb[0]) for x in range(size[0]//2,size[0]) for y in range(size[1]//2,size[1])]
    # 9ブロック
    elif target ==6:
        [im.putpixel((x,y),rgb[0]) for x in range(0,size[0]//3) for y in range(0,size[1]//3)]
        [im.putpixel((x,y),rgb[1]) for x in range(size[0]//3,size[0]//3*2) for y in range(0,size[1]//3)]
        [im.putpixel((x,y),rgb[2]) for x in range(size[0]//3*2,size[0]) for y in range(0,size[1]//3)]
        [im.putpixel((x,y),rgb[1]) for x in range(0,size[0]//3) for y in range(size[1]//3,size[1]//3*2)]
        [im.putpixel((x,y),rgb[2]) for x in range(size[0]//3,size[0]//3*2) for y in range(size[1]//3,size[1]//3*2)]
        [im.putpixel((x,y),rgb[0]) for x in range(size[0]//3*2,size[0]) for y in range(size[1]//3,size[1]//3*2)]
        [im.putpixel((x,y),rgb[2]) for x in range(0,size[0]//3) for y in range(size[1]//3*2,size[1])]
        [im.putpixel((x,y),rgb[0]) for x in range(size[0]//3,size[0]//3*2) for y in range(size[1]//3*2,size[1])]
        [im.putpixel((x,y),rgb[1]) for x in range(size[0]//3*2,size[0]) for y in range(size[1]//3*2,size[1])]
    return im

def overlay():
    im_ol = Image.new('RGB',size,(100,100,100))
    target = random.randint(1,9)
    # 配色
    rgb = (random.choice(ol_colors),random.choice(ol_colors),random.choice(ol_colors))
    for x in range(size[0]):
        for y in range(size[1]):
            # 十字
            if target ==1:
                if (x >= size[0]//7*3 and x <= size[0]//7*4) or (y >= size[1]//5*2 and y <= size[1]//5*2+size[0]//7) :
                    im_ol.putpixel((x,y),rgb[0])
            # 左斜め
            elif target ==2:
                if (x >= y*size[0]//size[1] and x <= y*size[0]//size[1] + size[1]//4) :
                    im_ol.putpixel((x,y),rgb[0])
            # 右斜め
            elif target ==3:
                if (x >= size[0] - size[1]//4  - y*size[0]//size[1] and x <= size[0] - y*size[0]//size[1]) :
                    im_ol.putpixel((x,y),rgb[0])
            # クロス
            elif target ==4:
                if (x >= y*size[0]//size[1] and x <= y*size[0]//size[1] + size[1]//4)  or (x >= size[0] - size[1]//4  - y*size[0]//size[1] and x <= size[0] - y*size[0]//size[1]):
                    im_ol.putpixel((x,y),rgb[0])
            # なし
            elif target ==5:
                pass
            # 左三角
            elif target ==6:
                if (x=size[1]//2):
                    im_ol.putpixel((x,y),rgb[0])
            # ダイヤ
            elif target ==7:
                if (x >= (size[0]  - y*size[0]//size[1]) - size[0]//2 and x<= size[0]//2  + (y*size[0]//size[1])  and y<=size[1]//2):
                    im_ol.putpixel((x,y),rgb[0])
                if ( x >= y*size[0]//size[1]-size[0]//2 and x<= size[0] -(y*size[0]//size[1]-size[0]//2)  and y>=size[1]//2):
                    im_ol.putpixel((x,y),rgb[0])
            # ずれ十字
            if target ==8:
                if (x >= size[0]//7*1 and x <= size[0]//7*2) or (y >= size[1]//5*1 and y <= size[1]//5*1+size[0]//7) :
                    im_ol.putpixel((x,y),rgb[0])
            # 左上
            if target ==9:
                if (x >= 0 and x <= size[0]//3)  and   (y >= 0 and y <= size[1]//2):
                    im_ol.putpixel((x,y),rgb[0])
    if random.choice([True,False,False,False,False,False,False,False]):
        im_ol = Image.new('RGB',size,(100,100,100))
        draw = ImageDraw.Draw(im_ol)
        e_size = max(size)//3
        x1,y1 = (size[0]-e_size)//2 , (size[1]-e_size)//2
        x2,y2 = size[0]-(size[0]-e_size)//2 , size[1]-(size[1]-e_size)//2
        draw.ellipse((x1,y1,x2,y2), fill=rgb[0], outline=rgb[0])
    return im_ol

def simbol():
    rgb = (random.choice(ol_colors),random.choice(ol_colors),random.choice(ol_colors))
    s_size,gray = (size[0]//3,size[1]//2),(100,100,100)
    im_si = Image.new('RGB',s_size,gray)
    draw = ImageDraw.Draw(im_si)
    target = random.randint(1,8)
    if target==1:
        draw.ellipse((0,0,s_size[0],s_size[1]), fill=rgb[0], outline=rgb[0])
        draw.ellipse((30,10,s_size[0],s_size[1]-10), fill=gray, outline=gray)
    elif target ==2:
        draw.ellipse((0,0,s_size[0],s_size[1]), fill=rgb[0], outline=rgb[0])
        draw.ellipse((15,15,s_size[0]-15,s_size[1]-15), fill=gray, outline=gray)
    elif target==3:
        draw.ellipse((0,0,s_size[0],s_size[1]), fill=rgb[0], outline=rgb[0])
        draw.ellipse((0,10,s_size[0]-30,s_size[1]-10), fill=gray, outline=gray)
    elif target==4:
        draw.pieslice((0, 0, s_size[0],s_size[1]), start=     0, end=  60, fill=rgb[0], outline=gray)
        draw.pieslice((0, 0, s_size[0],s_size[1]), start=120, end=180, fill=rgb[0], outline=gray)
        draw.pieslice((0, 0, s_size[0],s_size[1]), start=240, end=300, fill=rgb[0], outline=gray)
    elif target ==5:
        draw.pieslice((0, 0, s_size[0],s_size[1]), start=30, end=150, fill=rgb[0], outline=gray)
        draw.pieslice((0, 0, s_size[0],s_size[1]), start=210, end= 330, fill=rgb[0], outline=gray)
    elif target ==6:
        for x in [i*60+15 for i in range(6)]:
            draw.pieslice((0, 0, s_size[0],s_size[1]), start=x, end=x+30, fill=rgb[0], outline=gray)
    elif target ==7:
        for x in [i*45 for i in range(8)]:
            draw.pieslice((0, 0, s_size[0],s_size[1]), start=x+15, end=x+30, fill=rgb[0], outline=gray)
    elif target ==7:
        pass
    if random.choice([True,False,False,False,False]):
        draw.pieslice((20, 20, s_size[0]-20,s_size[1]-20), start=     0, end= 360, fill=rgb[0])
    return im_si

# 実行部
im_base = base()
im_ol = overlay()
im_si = simbol()
si_x , si_y = im_si.size[0],im_si.size[1]
s_size = (size[0]//3,size[1]//2)
for x in range(size[0]):
    for y in range(size[1]):
        r,g,b = im_ol.getpixel((x,y))
        if all([r!=100,g!=100,b!=100]):
            im_base.putpixel((x,y),(r,g,b))

for x in range(s_size[0]):
    for y in range(s_size[1]):
        r,g,b = im_si.getpixel((x,y))
        if all([r!=100,g!=100,b!=100]):
            im_base.putpixel((x,y),(r,g,b))

file_name = 'fictional_flag.png'
im_base.save(file_name)

使い方は簡単
コードをJupyter Notebookにコピーして
実行するだけ

ランダムで架空の国旗pngファイルを生成します。

簡単に説明すると
base画像をPILで作ります。
この時点で何色かに色分けされた画像ができます。

そこにoverlayで画像を付け足します。
十字とか、斜め線が入ったりします。
最後にsimbolでシンボルマークを付け足して出来上がり。

シンボルはnumpyとImageDrawを使っていろいろやっています。
円を特定の角度分だけ塗ったりすれば
ピザのスライスになり
そこに円を足せば雪マークになったりしますね。


flag


シンボルが左上にしか描画していなかったり、
パターンが少なかったりで
全部のパターンを把握していませんが
まあまあの組み合わせになるんではないでしょうか?

1ピクセルづつ色を塗っているだけの
単純なプログラムで
複雑な形を表現できていないです。

も少しスマートに描く方法は
ないものだろうか・・・

片手間では
この程度でしょうか。