python Image resize 对iOS图片素材进行2X,3X处理

通常在iOS上开发使用的图片素材1x,2x,3x三种

下面利用python Image 库 resize函数,由一个大图,自动生成1x,2x,3x的素材照片;

1. 首先你的python环境要安装有Image库, 即PIL

没有安装的,下载源码 http://effbot.org/downloads/Imaging-1.1.7.tar.gz

安装PIL:

     $ tar xvfz Imaging-1.1..tar.gz
$ cd Imaging-1.1.
$ python setup.py install

2. 处理逻辑

传入照片路径,设定生成照片的名字,1x的照片大小;

脚本处理

3. 使用的核心函数 resize

#打开指定路径的照片
img = Image.open(infile) #转换成RGBA 添加alpha,大多数照片还是要透明的
img = img.convert("RGBA") #转换照片大小
save1x = img.resize((inWidth*,inHeight*)) #保存到指定路径,输出
save1x.save(out1x)

4. 完整的处理python脚本

#coding=utf-8
#自动生成iOS上1x,2x,3x图
#dev.keke@gmail.com
#17-04-19 from sys import argv
import os.path,Image #verify input
if len(argv)!=5 or argv[1]=='-h':
print "调用错误,请参考如下说明"
print "使用该脚本自生成ios上 1x,2x,3x图片"
print "使用示例,生成loginBtn.png(100,100),loginBtn@2x.png(200,200),loginBtn@3x.png(300,300)"
print "python xxx.py ~/path/test.png loginBtn 100 100"
print ""
exit() #in
infile = argv[1]
inName = argv[2]
inWidth = int(argv[3])
inHeight = int(argv[4])
fpath,fname = os.path.split(infile)
img = Image.open(infile)
img = img.convert("RGBA")
x,y = img.size
print "infile: " + infile
print "inSize: (" + str(x) + "," + str(y) +")" #out
out1x = fpath + "/" + inName + ".png"
out2x = fpath + "/" + inName + "@2x.png"
out3x = fpath + "/" + inName + "@3x.png"
save1x = img.resize((inWidth*1,inHeight*1))
save1x.save(out1x)
save2x = img.resize((inWidth*2,inHeight*2))
save2x.save(out2x)
save3x = img.resize((inWidth*3,inHeight*3))
save3x.save(out3x)
print "out:"
print out1x + " size: (" + str(inWidth*1) +","+ str(inHeight*1) +")"
print out2x + " size: (" + str(inWidth*2) +","+ str(inHeight*2) +")"
print out3x + " size: (" + str(inWidth*3) +","+ str(inHeight*3) +")" print "SUCCESS"

使用示例:

cocoaPro-2:iostp cocoajin$ ls
howto.gif iosImg.py test.png
cocoaPro-2:iostp cocoajin$ python iosImg.py ./test.png download 100 100
infile: ./test.png
inSize: (473,473)
out:
./download.png size: (100,100)
./download@2x.png size: (200,200)
./download@3x.png size: (300,300)
SUCCESS
cocoaPro-2:iostp cocoajin$ ls
download.png download@3x.png iosImg.py
download@2x.png howto.gif test.png
cocoaPro-2:iostp cocoajin$

下载脚本

上一篇:Spark Streaming updateStateByKey和mapWithState源码解密


下一篇:轻装上阵Flink--在IDEA上开发基于Flink的实时数据流程序