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装饰器的函数式编程详解
Feb 27 Python
Pycharm学习教程(5) Python快捷键相关设置
May 03 Python
浅谈numpy数组中冒号和负号的含义
Apr 18 Python
python2和python3在处理字符串上的区别详解
May 29 Python
浅谈pycharm使用及设置方法
Sep 09 Python
解决pycharm最左侧Tool Buttons显示不全的问题
Dec 17 Python
pyenv虚拟环境管理python多版本和软件库的方法
Dec 26 Python
Python-opencv 双线性插值实例
Jan 17 Python
Django 构建模板form表单的两种方法
Jun 14 Python
关于Theano和Tensorflow多GPU使用问题
Jun 19 Python
Python经典五人分鱼实例讲解
Jan 04 Python
Pytorch可视化的几种实现方法
Jun 10 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的配置文件php.ini
2006/10/09 PHP
php设计模式 Delegation(委托模式)
2011/06/26 PHP
php中用date函数获取当前时间有误的解决办法
2013/08/02 PHP
本地机apache配置基于域名的虚拟主机详解
2013/08/10 PHP
浅谈Laravel队列实现原理解决问题记录
2017/08/19 PHP
PHP观察者模式定义与用法实例分析
2019/03/22 PHP
javascript 面向对象的JavaScript类
2010/05/04 Javascript
用js模拟struts2的多action调用示例
2014/05/19 Javascript
jQuery 2.0.3 源码分析之core(一)整体架构
2014/05/27 Javascript
jquery实现向下滑出的二级导航下滑菜单效果
2015/08/25 Javascript
javascript性能优化之事件委托实例详解
2015/12/12 Javascript
详细探究ES6之Proxy代理
2016/07/22 Javascript
Backbone View 之间通信的三种方式
2016/08/09 Javascript
js转html实体的方法
2016/09/27 Javascript
Vuex2.0+Vue2.0构建备忘录应用实践
2016/11/30 Javascript
如何获取元素的最终background-color
2017/02/06 Javascript
浅析jsopn跨域请求原理及cors(跨域资源共享)的完美解决方法
2017/02/06 Javascript
js实现时间轴自动排列效果
2017/03/09 Javascript
JS实现的小火箭发射动画效果示例
2018/12/08 Javascript
解决$store.getters调用不执行的问题
2019/11/08 Javascript
举例讲解Python面向对象编程中类的继承
2016/06/17 Python
python+pillow绘制矩阵盖尔圆简单实例
2018/01/16 Python
对numpy的array和python中自带的list之间相互转化详解
2018/04/13 Python
使用 Python 快速实现 HTTP 和 FTP 服务器的方法
2019/07/22 Python
Python制作词云图代码实例
2019/09/09 Python
如何通过Django使用本地css/js文件
2020/01/20 Python
Django启动时找不到mysqlclient问题解决方案
2020/11/11 Python
CSS3中HSL和HSLA的简单使用示例
2015/07/14 HTML / CSS
摩顿布朗英国官方网上商店:奢华沐浴、身体和头发护理
2016/10/29 全球购物
如何通过 CSS 写出火焰效果
2021/03/24 HTML / CSS
办理暂住证介绍信
2014/01/11 职场文书
在校生证明
2015/06/17 职场文书
2016暑期政治学习心得体会
2016/01/23 职场文书
python 爬取豆瓣网页的示例
2021/04/13 Python
golang特有程序结构入门教程
2021/06/02 Python
游戏《铁拳》动画化!2022年年内播出
2022/03/21 日漫