python各种尺寸图标生成

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

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值