Python 将任意格式的图片转为icns格式,不使用插件。支持window、Linux
在做一个小工具,把图片转为icns格式的。 macOS上有个iconutil工具,可以转换。但是如果放在Linux或者Windows上就没法使用了。
于是各种谷歌,百度。没有找到解决办法。于是在github上发现了一个java版本的,就翻译成Python的了。
需要安装一个pillow图像处理库,除此之外无需依赖任何插件。
pillow 支持icns读取,但是不支持写入。后续我会把写入的功能push到pillow库中。
pip install pillow
pip install pillow
代码:
python
import io
from PIL import Image
import struct
p = '/Users/panjing/Downloads/a.webp'
bi = Image.open(p)
print(bi)
def toInt(s):
b = s.encode('ascii')
return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3]
MAGIC = toInt("icns")
HEADER_SIZE = 8
TOC = 'TOC '
# 几种尺寸大小
sizes = [128, 256, 512, 32, 64, 256, 512, 1024]
size_str = ['ic07', 'ic08', 'ic09', 'ic11', 'ic12', 'ic13', 'ic14', 'ic10']
fileSize = 0
entries = []
for index, s in enumerate(sizes):
temp = io.BytesIO()
nb = bi.resize((s, s))
nb.save(temp, 'png')
fileSize += len(temp.getvalue())
entries.append({
'type': size_str[index],
'size': len(temp.getvalue()),
'stream': temp
})
dos = io.BytesIO()
# Header
dos.write(struct.pack('i', MAGIC)[::-1])
dos.write(struct.pack('i', fileSize)[::-1])
# TOC
tocSize = HEADER_SIZE + (len(entries) * HEADER_SIZE)
dos.write(struct.pack('i', toInt(TOC))[::-1])
dos.write(struct.pack('i', tocSize)[::-1])
for e in entries:
dos.write(struct.pack('i', toInt(e.get('type')))[::-1])
dos.write(struct.pack('i', HEADER_SIZE + e.get('size'))[::-1])
# Data
for index, e in enumerate(entries):
dos.write(struct.pack('i', toInt(e.get('type')))[::-1])
dos.write(struct.pack('i', HEADER_SIZE + e.get('size'))[::-1])
dos.write(e.get('stream').getvalue())
dos.flush()
print(len(dos.getvalue()))
pp = '/Users/panjing/Downloads/a.icns'
ff = open(pp, 'wb')
ff.write(dos.getvalue())
ff.flush()
ff.close()
import io
from PIL import Image
import struct
p = '/Users/panjing/Downloads/a.webp'
bi = Image.open(p)
print(bi)
def toInt(s):
b = s.encode('ascii')
return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3]
MAGIC = toInt("icns")
HEADER_SIZE = 8
TOC = 'TOC '
# 几种尺寸大小
sizes = [128, 256, 512, 32, 64, 256, 512, 1024]
size_str = ['ic07', 'ic08', 'ic09', 'ic11', 'ic12', 'ic13', 'ic14', 'ic10']
fileSize = 0
entries = []
for index, s in enumerate(sizes):
temp = io.BytesIO()
nb = bi.resize((s, s))
nb.save(temp, 'png')
fileSize += len(temp.getvalue())
entries.append({
'type': size_str[index],
'size': len(temp.getvalue()),
'stream': temp
})
dos = io.BytesIO()
# Header
dos.write(struct.pack('i', MAGIC)[::-1])
dos.write(struct.pack('i', fileSize)[::-1])
# TOC
tocSize = HEADER_SIZE + (len(entries) * HEADER_SIZE)
dos.write(struct.pack('i', toInt(TOC))[::-1])
dos.write(struct.pack('i', tocSize)[::-1])
for e in entries:
dos.write(struct.pack('i', toInt(e.get('type')))[::-1])
dos.write(struct.pack('i', HEADER_SIZE + e.get('size'))[::-1])
# Data
for index, e in enumerate(entries):
dos.write(struct.pack('i', toInt(e.get('type')))[::-1])
dos.write(struct.pack('i', HEADER_SIZE + e.get('size'))[::-1])
dos.write(e.get('stream').getvalue())
dos.flush()
print(len(dos.getvalue()))
pp = '/Users/panjing/Downloads/a.icns'
ff = open(pp, 'wb')
ff.write(dos.getvalue())
ff.flush()
ff.close()