分享一个自己写的图片转图标软件,13m大小

python语言编写,用的PythonMagick库

然后是pysimplegui显示,再用pyinstaller打包就好了

程序打开就一个界面,生成图标成功后会自动生成ico_output文件夹,放入生成的ico

下载路径:https://download.csdn.net/download/m_986883511/12082699

 

# -*- coding: utf-8 -*-
import PySimpleGUI as sg
import PythonMagick
import os


layout = [
    [sg.Text("提示:请将图片裁剪为正方形,才能输出1:1比例的图标!")],
    [sg.InputText('选择图片路劲', size=(40, 1)), sg.FileBrowse('浏览')],
    [sg.Text('选择图标尺寸'), sg.InputCombo(('256x256','128x128', '64x64', '32x32', '16x16',),
                                      key='ico_size', default_value='128x128', size=(21, 1)), sg.Button('生成图标')],
    [sg.Text('生成结果:', size=(40, 1), key='msg')]
]
window = sg.Window('图片转图标', layout)


def generate_ico(file, size):
    try:
        img = PythonMagick.Image(file)
        img.sample(size)
        if not os.path.exists('ico_output'):
            os.makedirs('ico_output')
        name = os.path.split(file)[1]
        name = '{}_{}.ico'.format(name.split('.')[0], size.replace('x', '_'))
        img.write('ico_output/{}'.format(name))
        return True, name
    except Exception as e:
        print(e)
        return False, '错误={}'.format(e)


def change_msg(flag, msg):
    print(flag, msg)
    if flag:
        message = '生成成功:{}'.format(msg)
    else:
        message = '生成失败:{}'.format(msg)
    window['msg'].update(message)


if __name__ == '__main__':
    # Create the event loop
    win2_active = False
    while True:
        event, values = window.read()
        if event in (None, 'Cancel'):
            # User closed the Window or hit the Cancel button
            break
        elif event == '生成图标':
            file_road = values['浏览']
            if file_road:
                if os.access(file_road, os.F_OK):
                    result, msg = generate_ico(file_road, values['ico_size'])
                    change_msg(result, msg)
                else:
                    change_msg(False, '{} 文件不存在'.format(file_road))
            else:
                change_msg(False, '请选择图片文件')
        print(event, values)
    window.close()

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值