python Image resize 对iOS图片素材进行2X,3X处理
2017-04-19 17:06
489 查看
通常在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:
2. 处理逻辑
传入照片路径,设定生成照片的名字,1x的照片大小;
脚本处理
3. 使用的核心函数 resize
4. 完整的处理python脚本
使用示例:
下载脚本
下面利用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.7.tar.gz $ cd Imaging-1.1.7 $ python setup.py install
2. 处理逻辑
传入照片路径,设定生成照片的名字,1x的照片大小;
脚本处理
3. 使用的核心函数 resize
#打开指定路径的照片 img = Image.open(infile) #转换成RGBA 添加alpha,大多数照片还是要透明的 img = img.convert("RGBA") #转换照片大小 save1x = img.resize((inWidth*1,inHeight*1)) #保存到指定路径,输出 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$
下载脚本
相关文章推荐
- 一种简便的ios图片加密方法-对图片进行base64编码
- IOS学习笔记39——拍照、从相册选图并对图片进行裁剪
- 利用python进行批量导出时态图片
- iOS&Android素材图片网址
- ios UIWebView截获html并修改便签内容以及用js对图片进行缩放
- iOS&Android素材图片网址
- iOS: 对图片进行不规则剪裁
- IOS 对相册图片进行读取、存储到指定文件夹、从指定文件夹读取出来
- iOS 抓取 UIwebview 上 所有 图片 并进行滚动播放
- IOS获取图片进行压缩
- iOS开发有用代码(有判断邮箱格式是否正确的代码、图片压缩、亲测可用的图片上传代码和实现点击图片进行跳转的代码等。)
- 使用ImageMagick进行图片缩放、合成与裁剪(js+python)
- 使用ImageMagick进行图片缩放、合成与裁剪(js+python)
- Python查询MySQL进行远程采集图片实例
- python使用pyapns进行ios推送消息
- IOS 对UIImage图片进行缩放
- linux下用python进行opencv开发----简单的图片操作
- ios网络加载图片进行本地文件缓存开源插件sdwebimagecache
- IOS学习笔记39——拍照、从相册选图并对图片进行裁剪
- 使用python进行图像处理-调整图片大小