Python CLI 下的 icoconverter


為了一時的方便.不想每次轉換 PNG to ICO 時都打開網頁.
所以寫了個簡單的令命行程式來轉換.記錄一下大約的內容.

預設會轉換成 - Size: (16,32,48) 和 Bit depth: 32

用法是

1
python index.py -f /Users/username/Desktop/icon/favicon.png

代碼是

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python
# coding: utf-8

import os
import requests
import optparse

class ICOConverter(object):

url = 'http://www.icoconverter.com/index.php'

def __init__(self, file, sizes=(16, 32, 48, 64, 128, 256), bpp=32):
self.file = file
self.sizes = sizes
self.bpp = bpp

def convert(self):
if not os.path.exists(self.file):
print('Can not found the file: {0}'.format(self.file))
else:
filename = os.path.splitext(os.path.basename(self.file))[0]
iconfile = os.path.join(os.path.dirname(self.file), '{0}.ico'.format(filename))

files = { 'image': open(self.file, 'r') }
data = { 'sizes[]': self.sizes, 'bpp': 32 }

r = requests.post(self.url, files=files, data=data)

if r.status_code == 200:
with open(iconfile, 'w') as f:
f.write(r.content)
f.close()

print('Converted to {0}'.format(iconfile))
else:
print('Can not covert to ico file')

if __name__ == '__main__':
parser = optparse.OptionParser(usage="Usage: %prog -f [FILE]")

parser.add_option("-f", "--file", action="store", dest="file", help="Which image file want to convert to ico")

(options, args) = parser.parse_args()

if options.file:
ICOConverter(options.file, sizes=(16, 32, 48), bpp=32).convert()
else:
parser.print_help()