对512*512的图片,生成各种尺寸android等的图标,放到对应文件夹。
import Image
import os,sys
ICON_FOR_ANDROID = 0
ICON_FOR_IOS = 1
androidIconSize = [36,48,72,96,144,196]
iosIconSize = [29,40,50,57,58,72,76,80,100,114,120,144,152]
iconDir = ['ldpi','mdpi','hdpi','xhdpi','xxhdpi','xxxhdpi']
class Converter:
def readFile(self,name):
if name.find('.png') == -1:
name += '.png'
try:
self.im = Image.open(name)
return 'success'
except:
print "read file(%s) error!"%name
newName = raw_input("please input your origin file name(or input 'q' to exit):")
if newName == 'q':
return 'error'
else:
self.readFile(newName)
def convert(self,platform):
if len(sys.argv)>1:
infile = sys.argv[1]
else:
infile = 'icon.png'
code = self.readFile(infile)
if code == 'error':
print 'exit success!'
return
##
if platform == ICON_FOR_ANDROID:
path = "icon-android/res"
path1= "icon-android-mipmap/res"
try:
os.mkdir(path)
os.mkdir(path1)
except:
# do nothing
pass
index = 0
for size in androidIconSize:
try:
os.makedirs(path+'/drawable-'+iconDir[index])
os.makedirs(path1+'/mipmap-'+iconDir[index])
except:
# do nothing
pass
out = self.im.resize((size,size),Image.ANTIALIAS) #resize image with high-quality
out.save(path+'/drawable-'+iconDir[index]+'/icon.png')
out.save(path1+'/mipmap-'+iconDir[index]+'/ic_launcher.png')
index = index +1
print 'convert to android icon ,success!'
elif platform == ICON_FOR_IOS:
try:
os.mkdir("icon-ios")
except:
# do nothing
pass
index = 0
for size in iosIconSize:
out = self.im.resize((size,size),Image.ANTIALIAS) #resize image with high-quality
out.save('icon-ios/'+'icon-%d.png'%size)
index = index +1
print 'convert to ios icon ,success!'
def __init__(self):
print "start convert"
self.convert(ICON_FOR_IOS)
self.convert(ICON_FOR_ANDROID)
if __name__ == '__main__':
cvt = Converter()