python实现批量按比例缩放图片效果


Posted in Python onMarch 30, 2018

本文实例为大家分享了python实现批量按比例缩放图片的具体代码,供大家参考,具体内容如下

把脚本文件放在要缩放的文件夹下面。

双击运行脚本,输入要缩放的系数。脚本会在当前目录下创建一个scaledImg_xxxx文件夹,如果已经存在,会强制删除,如果删除失败会提示手动删除这个文件夹,再双击运行就可以了。

resizeImg.py

#!/usr/bin/python 
# -*- coding:utf8 -*- 
 
#author@skillart www. 
 
import os 
import shutil 
import Image  
to_scale = 0.5 
processIndex = 0 
def resizeImg(imgPath): 
  global processIndex 
  fileList = [] 
  files = os.listdir(imgPath) 
  for f in files: 
    filePath = imgPath + os.sep + f 
    if(os.path.isfile(filePath)): 
      fileList.append(f) 
    elif(os.path.isdir(filePath)): 
      resizeImg(filePath) 
  for fileName in fileList: 
    processIndex+=1 
    fileFullName = imgPath+os.sep+fileName 
    suffix = fileName[fileName.rfind('.'):] 
    if(suffix == '.png' or suffix == '.jpg'): 
      print 'processing the '+str(processIndex)+'th file:'+fileFullName 
      img = Image.open(fileFullName) 
      w,h = img.size 
      tw = int(w * to_scale) 
      th = int(h * to_scale) 
      reImg = img.resize((tw,th),Image.ANTIALIAS) 
      reImg.save(fileFullName) 
      del reImg 
if __name__ == '__main__': 
  scaleStr = raw_input('input to_scale: ') 
  to_scale = float(scaleStr) 
  scaledPath = '.\\scaledImg_xxxx'; 
  if os.path.isdir(scaledPath): 
    flag = raw_input('the output dir is exist, sure to del it(y/n)') 
    if flag == 'y' or flag == 'yes': 
      try:   
        shutil.rmtree(scaledPath) 
      finally: 
        raw_input('remove dir failed , please removed the dir manually.') 
    else: 
      exit 
  shutil.copytree('.\\',scaledPath)   
  resizeImg(scaledPath) 
  raw_input("resize success")

生成Icon

generateIcon.py

#!/usr/bin/python 
# -*- coding:utf8 -*- 
 
#author@skillart www. 
 
import os 
import shutil 
import Image  
def resizeImg(imgPathName): 
  print imgPathName 
  iconDict = {'Icon.png':'72x72','Icon@2x.png':'144x144','Icon-29.png':'29x29','Icon-40.png':'40x40','Icon-50.png':'50x50', 
  'Icon-57.png':'57x57', 'Icon-58.png':'58x58','Icon-72.png':'72x72','Icon-76.png':'76x76','Icon-80.png':'80x80', 
  'Icon-100.png':'100x100','Icon-114.png':'114x114','Icon-120.png':'120x120','Icon-144.png':'144x144','Icon-152.png':'152x152', 
  'FlipCycleTileLarge.png':'300x300','FlipCycleTileMedium.png':'300x300','FlipCycleTileSmall.png':'300x300', 
  'IconicTileMediumLarge.png':'300x300','IconicTileSmall.png':'300x300','ApplicationIcon.png':'300x300','icon.png':'72x72'} 
  if os.path.isfile(imgPathName) == False: 
    print('open imgPathName failed , check the' + imgPathName + "is exist!") 
    exit 
  img = Image.open(imgPathName) 
  index = imgPathName.rfind(os.sep) 
  prefix = imgPathName[:index+1] 
  for key, value in iconDict.items(): 
    # print key,value 
    v_split = value.split('x') 
    w,h = int(v_split[0]),int(v_split[1]) 
    fileName = prefix + key 
    reImg = img.resize((w,h),Image.ANTIALIAS) 
    reImg.save(fileName) 
    print fileName,w,h 
  del img 
if __name__ == '__main__': 
  scaledPath = '.\\createIcon' 
  if os.path.isdir(scaledPath): 
    flag = raw_input('the output dir is exist, sure to del it(y/n)') 
    if flag == 'y' or flag == 'yes': 
      try:   
        shutil.rmtree(scaledPath) 
      finally: 
        raw_input('remove dir failed , please removed the dir manually.') 
    else: 
      exit 
  shutil.copytree('.\\',scaledPath)  
  fileList = [] 
  files = os.listdir(scaledPath) 
  for f in files: 
    filePath = scaledPath + os.sep + f 
    if os.path.isfile(filePath) : 
      suffix = filePath[filePath.rfind('.'):] 
      if(suffix == '.png' or suffix == '.jpg'): 
        print filePath 
        resizeImg(filePath) 
        break 
  raw_input("resize success")

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python的时间模块datetime详解
Apr 17 Python
Python set常用操作函数集锦
Nov 15 Python
Linux下多个Python版本安装教程
Aug 15 Python
python中正则表达式 re.findall 用法
Oct 23 Python
python处理两种分隔符的数据集方法
Dec 12 Python
Python 编程速成(推荐)
Apr 15 Python
python+openCV调用摄像头拍摄和处理图片的实现
Aug 06 Python
Numpy的简单用法小结
Aug 28 Python
基于python实现查询ip地址来源
Jun 02 Python
python 线程的五个状态
Sep 22 Python
Python scrapy爬取起点中文网小说榜单
Jun 13 Python
Pandas搭配lambda组合使用详解
Jan 22 Python
python放大图片和画方格实现算法
Mar 30 #Python
python实现数独游戏 java简单实现数独游戏
Mar 30 #Python
简单实现python数独游戏
Mar 30 #Python
Python使用MD5加密算法对字符串进行加密操作示例
Mar 30 #Python
windows环境下tensorflow安装过程详解
Mar 30 #Python
Python切片工具pillow用法示例
Mar 30 #Python
Python实现OpenCV的安装与使用示例
Mar 30 #Python
You might like
PHP 第二节 数据类型之转换
2012/04/28 PHP
preg_match_all使用心得分享
2014/01/31 PHP
php输出1000以内质数(素数)示例
2014/02/16 PHP
Drupal7连接多个数据库及常见问题解决
2014/03/02 PHP
Laravel 5 框架入门(一)
2015/04/09 PHP
php实现微信支付之退款功能
2018/05/30 PHP
js实现屏蔽默认快捷键调用自定义事件示例
2013/06/18 Javascript
判断js对象是否拥有某一个属性的js代码
2013/08/16 Javascript
javascript实现 百度翻译 可折叠的分享按钮列表
2015/03/12 Javascript
javascript实现显示和隐藏div方法汇总
2015/08/14 Javascript
js实现人民币大写金额形式转换
2016/04/27 Javascript
使用jQuery制作Web页面遮罩层插件的实例教程
2016/05/26 Javascript
基于JavaScript实现轮播图代码
2016/07/14 Javascript
微信小程序 setData使用方法及常用错误解决办法
2017/05/11 Javascript
M2实现Nodejs项目自动部署的方法步骤
2019/05/05 NodeJs
vue Cli 环境删除与重装教程 - 版本文档
2020/09/11 Javascript
[42:25]EG vs Spirit Supermajor 败者组 BO3 第二场 6.4
2018/06/05 DOTA
python连接mongodb操作数据示例(mongodb数据库配置类)
2013/12/31 Python
使用Python的Tornado框架实现一个一对一聊天的程序
2015/04/25 Python
Python3实现发送QQ邮件功能(html)
2017/12/15 Python
pandas.DataFrame 根据条件新建列并赋值的方法
2018/04/08 Python
解决csv.writer写入文件有多余的空行问题
2018/07/06 Python
python爬虫爬取微博评论案例详解
2019/03/27 Python
Pytorch实现LSTM和GRU示例
2020/01/14 Python
python软件都是免费的吗
2020/06/18 Python
Python DES加密实现原理及实例解析
2020/07/17 Python
美国药妆网站:EDCskincare.com(防晒、痤疮、抗衰老等)
2017/04/28 全球购物
aden + anais官方网站:婴儿襁褓、毯子、尿布和服装
2017/06/21 全球购物
美国山地自行车、露营、户外装备和服装购物网站:Aventuron
2018/05/05 全球购物
Java面试题:请问一下代码输出是什么
2015/05/27 面试题
2019史上最全Database工程师题库
2015/12/06 面试题
日语专业个人的求职信
2013/12/03 职场文书
党员创先争优心得体会
2014/09/11 职场文书
大学生思想道德自我评价
2015/03/09 职场文书
繁星春水读书笔记
2015/06/30 职场文书
详细聊聊浏览器是如何看闭包的
2021/11/11 Javascript