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()