magigen.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:AstralLoom 作者: Nydrath 项目源码 文件源码
def create_circle():
    IMAGE_WIDTH = 300
    IMAGE_HEIGHT = IMAGE_WIDTH
    IMAGE_SIZE = (IMAGE_WIDTH, IMAGE_HEIGHT)
    FONT_SIZE = 100

    NSYMBOLS = 15
    BORDER_WIDTH = int(0.6 * FONT_SIZE)

    linksize = 3
    full_link = "".join(["".join(random.sample(string.ascii_uppercase, linksize)) for i in range(NSYMBOLS)])
    links = [full_link[i:i+linksize] for i in range(0, len(full_link), linksize)]

    nrotations = 8
    rotations = [int(i*360/nrotations) for i in range(nrotations)]
    sigils = []
    totensor = torchvision.transforms.ToTensor()

    pool = torch.multiprocessing.Pool(3)
    result = pool.map_async(lambda x:createsymbol(x, IMAGE_SIZE), links)
    while not result.ready():
        print("# Symbols not yet processed: {}".format(result._number_left))
        time.sleep(5)
    sigils = result.get()
    pool.close()
    pool.join()


    maxwidth = 0
    for im in sigils:
        maxwidth = max(im.size[0], maxwidth)

    # 0.18 for touching
    radius = 0.25 * maxwidth * math.ceil(NSYMBOLS / 4)
    baseim = Image.new("L", (int(2*radius + 2*BORDER_WIDTH), int(2*radius + 2*BORDER_WIDTH)))
    center = BORDER_WIDTH + radius
    for idx,im in enumerate(sigils):
        t = Image.new("L", baseim.size)
        x = center + radius*math.sin(2*math.pi*idx/NSYMBOLS) - im.size[0]/2
        y = center + radius*math.cos(2*math.pi*idx/NSYMBOLS) - im.size[1]/2
        t.paste(im, (int(x), int(y)))
        baseim = ImageChops.lighter(baseim, t)

    IMAGE_WIDTH = 300
    IMAGE_HEIGHT = IMAGE_WIDTH
    IMAGE_SIZE = (IMAGE_WIDTH, IMAGE_HEIGHT)
    FONT_SIZE = 150
    im = createsymbol("".join(random.sample(string.ascii_uppercase, 3)))
    t = Image.new("L", baseim.size)
    x = center - im.size[0]/2
    y = center - im.size[1]/2
    t.paste(im, (int(x), int(y)))
    baseim = ImageChops.lighter(baseim, t)

    baseim.save("out.png")
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号