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中pandas.DataFrame对行与列求和及添加新行与列示例
Mar 12 Python
Python使用修饰器执行函数的参数检查功能示例
Sep 26 Python
python logging重复记录日志问题的解决方法
Jul 12 Python
用Python将结果保存为xlsx的方法
Jan 28 Python
python解释器spython使用及原理解析
Aug 24 Python
python fuzzywuzzy模块模糊字符串匹配详细用法
Aug 29 Python
Python + selenium + crontab实现每日定时自动打卡功能
Mar 31 Python
浅析NumPy 切片和索引
Sep 02 Python
python实现经纬度采样的示例代码
Dec 10 Python
详解Python之Scrapy爬虫教程NBA球员数据存放到Mysql数据库
Jan 24 Python
python中使用asyncio实现异步IO实例分析
Feb 26 Python
Selenium浏览器自动化如何上传文件
Apr 06 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
Yii实现显示静态页的方法
2016/04/25 PHP
快速解决PHP调用Word组件DCOM权限的问题
2017/12/27 PHP
laravel框架中路由设置,路由参数和路由命名实例分析
2019/11/23 PHP
一个js封装的不错的选项卡效果代码
2008/02/15 Javascript
JavaScript分析、压缩工具JavaScript Analyser
2014/12/31 Javascript
JSONP之我见
2015/03/24 Javascript
js 自带的 map() 方法全面了解
2016/08/16 Javascript
让html元素随浏览器的大小自适应垂直居中的实现方法
2016/10/12 Javascript
详解支持Angular 2的表格控件
2017/01/19 Javascript
JS变量中有var定义和无var定义的区别以及es6中let命令和const命令
2017/02/19 Javascript
JS实现根据密码长度显示安全条功能
2017/03/08 Javascript
JS对象与json字符串相互转换实现方法示例
2018/06/14 Javascript
Layui弹出层 加载 做编辑页面的方法
2019/09/16 Javascript
Python实现LRU算法的2种方法
2015/06/24 Python
python if not in 多条件判断代码
2016/09/21 Python
解决Django模板无法使用perms变量问题的方法
2017/09/10 Python
Python数据结构与算法之图的广度优先与深度优先搜索算法示例
2017/12/14 Python
Python DataFrame设置/更改列表字段/元素类型的方法
2018/06/09 Python
对Pycharm创建py文件时自定义头部模板的方法详解
2019/02/12 Python
python实现切割url得到域名、协议、主机名等各个字段的例子
2019/07/25 Python
浅谈pytorch torch.backends.cudnn设置作用
2020/02/20 Python
keras Lambda自定义层实现数据的切片方式,Lambda传参数
2020/06/11 Python
详解KMP算法以及python如何实现
2020/09/18 Python
Python用Jira库来操作Jira
2020/12/28 Python
eBay法国购物网站:eBay.fr
2017/10/21 全球购物
ProBikeKit德国:在线公路自行车专家
2018/06/03 全球购物
缓刑人员的思想汇报
2014/01/11 职场文书
白酒市场营销方案
2014/02/25 职场文书
法学院毕业生求职信
2014/06/25 职场文书
2014年公务员退休工资改革方案
2014/10/01 职场文书
2015大学生暑假调查报告
2015/07/13 职场文书
学雷锋活动简报
2015/07/20 职场文书
初中政教处工作总结
2015/08/12 职场文书
SQL Server内存机制浅探
2022/04/06 SQL Server
宝塔更新Python及Flask项目的部署
2022/04/11 Python
centos环境下nginx高可用集群的搭建指南
2022/07/23 Servers