CgTd

图标生成TD

2016-11-22  本文已影响0人  N景波

朋友让我做一张ppt背景图,ps不熟咋办,只能程序试试了。想到用各种app的icon拼一个“TD”二字母。最后也没用上,过程还是挺有意思的。

运行条件,安装PIL库,脚本同目录下有“icons”文件夹存放小图标, 里面需要24个小图标。示例icon可以从这下载: 链接:http://pan.baidu.com/s/1slPAc6D 密码:45u8

import functools
import os
import random
from PIL import Image
import math

if __name__ == '__main__':
    # icons 存放目录
    icon_dirs = "icons"
    print icon_dirs

    # 获取icon目录
    join = functools.partial(os.path.join, icon_dirs)
    icons = map(join, os.listdir(icon_dirs))

    # T字母的坐标
    tword = [(0, 0), (300, 0), (600, 0), (900, 0), (1200, 0), (600, 300), (600, 600),
             (600, 900), (600, 1200), (600, 1500)]

    # D字母的坐标
    dword = [(0, 0), (0, 300), (0, 600), (0, 900), (0, 1200), (0, 1500),
             (300, 0), (600, 150), (900, 300), (1000, 600), (1000, 900),
             (300, 1500), (600, 1500-150), (900, 1500-300)]

    # 存放icon图像
    collections = []

    # icon resize到 256 256 规格
    for icon in icons:
        im = Image.open(icon)
        im = im.resize((256, 256))
        collections.append(im)

    # 新建图像,用于保存生成的图像
    destim = Image.new("RGBA", (300*13, 300*7))

    # icon乱序一下
    random.shuffle(collections)

    # 定义 T,D的左上角 坐标
    t_offsize = (500, 200)
    d_offsize = (2300, 200)

    # 生成 T,D的真实坐标
    t_real = map(lambda x: (x[0]+t_offsize[0], x[1]+t_offsize[1]), tword)
    d_real = map(lambda x: (x[0]+d_offsize[0], x[1]+d_offsize[1]), dword)

    # T,D合在一起,一次画出来。
    t_real.extend(d_real)

    # 画图
    for i, pos in enumerate(t_real):
        im = collections[i]
        # 随机旋转一下
        im = im.rotate(random.randint(0, 60), resample=Image.NEAREST)

        # 写入目标图像
        destim.paste(im.convert("RGBA"), pos)

    # 保存
    destim.save(r"td.png", "png")

效果如下图:


image
上一篇 下一篇

猜你喜欢

热点阅读