使用python生成iOS各规格icon

本文介绍了一款使用Python编写的工具,能够快速批量生成适用于iOS应用的各种尺寸图标,极大提升了工作效率。开发者仅需提供一张高清大图,通过执行简单的命令行指令即可完成全部导出工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用python可以很方便的一次导出所需的各种大小icon,省时省力,美术只需要准备一个最大的icon就行了。

把下面的代码保存成icon_maker.py文件,然后执行python icon_maker.py -i xx.png -d xxdir即可。
执行前需要安装好PIL(已经不维护了)库(改用pillow库)
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys
from getopt import getopt
from PIL import Image

# 所有需要生成的icon宽度
ALL_ICONS = [[20,"20"], [40,"20@2x"], [60, "20@3x"], [29,"29"], [58,"29@2x"],
             [87,"29@3x"], [40,"40"], [80,"40@2x"], [120,"40@3x"], [50,"50"], 
             [100, "50@2x"], [57, "57"], [114, "57@2x"], [120, "60@2x"], [180, "60@3x"], 
             [72,"72"], [144,"72@2x"], [76,"76"], [152, "76@2x"], [167, "83.5@2x"], 
             [1024,"1025"]]

def make_icon(argv):
    helpstr = "use icon_maker.py -i <image file> -d <output dir> --help"
    try:
        opts, args = getopt(argv[1:], "i:d:h", ["help"])
    except getopt.GetoptError:
        print("Error: %s" % helpstr)
    for opt, arg in opts:
        if opt in ("-i"):
            infile = arg
        elif opt in ("-d"):
            outdir = arg
        elif opt in ("-h", "--help"):
            print(helpstr)
            exit()
    if not infile or not outdir:
        print("输入正确的图片路径和导出路径")
        exit()
    # img = Image.open(infile)

    print('infile:' + infile)
    with Image.open(infile) as img:
        (w,h) = img.size
        print('originW:' + str(w) + ',originH:' + str(h))

        for icon in ALL_ICONS:
            newW = icon[0]
            newH = icon[0]
            outfile = os.path.join(outdir, 'Icon-' + icon[1] + '.png')
            out = img.resize((newW,newH),Image.ANTIALIAS) #resize image with high-quality
            out.save(outfile)
            print('output file ' + 'Icon-' + icon[1] + '.png...')
        print('Export all ICONS finished...')
        os.system('open ' + outdir)


if __name__ == "__main__":
    make_icon(sys.argv)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值