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 11 Python
Python Xml文件添加字节属性的方法
Mar 31 Python
Python实现ping指定IP的示例
Jun 04 Python
使用python语言,比较两个字符串是否相同的实例
Jun 29 Python
windows下安装Python虚拟环境virtualenvwrapper-win
Jun 14 Python
python 通过手机号识别出对应的微信性别(实例代码)
Dec 22 Python
Pytorch对Himmelblau函数的优化详解
Feb 29 Python
详解Pycharm出现out of memory的终极解决方法
Mar 03 Python
python定义类的简单用法
Jul 24 Python
Python 内存管理机制全面分析
Jan 16 Python
解决jupyter notebook启动后没有token的坑
Apr 24 Python
Python matplotlib绘制雷达图
Apr 13 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中$this->含义分析
2009/11/29 PHP
简单理解PHP的面向对象编程方式
2016/05/17 PHP
php生成mysql的数据字典
2016/07/07 PHP
php微信支付接口开发程序
2016/08/02 PHP
通过ifame指向的页面高度调整iframe的高度
2006/10/05 Javascript
JavaScript prototype属性使用说明
2010/05/13 Javascript
javascript 正则表达式相关应介绍
2012/11/27 Javascript
简单几行JS Code实现IE邮件转发新浪微博
2013/07/03 Javascript
让网页跳转到指定位置的jquery代码非书签
2013/09/06 Javascript
jQuery中:submit选择器用法实例
2015/01/03 Javascript
js计算德州扑克牌面值的方法
2015/03/04 Javascript
javascript实现炫酷的拖动分页
2015/05/11 Javascript
jQuery实现的兼容性浮动层示例
2016/08/02 Javascript
Bootstrap对话框使用实例讲解
2016/09/24 Javascript
谈谈VUE种methods watch和compute的区别和联系
2017/08/01 Javascript
bootstrap select下拉搜索插件使用方法详解
2017/11/23 Javascript
解决Vue2.x父组件与子组件之间的双向绑定问题
2018/03/06 Javascript
Layer+Echarts构建弹出层折线图的方法
2019/09/25 Javascript
vue vant Area组件使用详解
2019/12/09 Javascript
使用js实现单链解决前端队列问题的方法
2020/02/03 Javascript
JS实现图片幻灯片效果代码实例
2020/05/21 Javascript
微信小程序 button样式设置为图片的方法
2020/06/19 Javascript
Python Mysql数据库操作 Perl操作Mysql数据库
2009/01/12 Python
Python中使用中文的方法
2011/02/19 Python
Python脚本获取操作系统版本信息
2016/12/17 Python
python多进程使用及线程池的使用方法代码详解
2018/10/24 Python
Python 虚拟空间的使用代码详解
2019/06/10 Python
Django 缓存配置Redis使用详解
2019/07/23 Python
wxPython:python首选的GUI库实例分享
2019/10/05 Python
Jmeter调用Python脚本实现参数互相传递的实现
2021/01/22 Python
Omio英国:搜索并比较便宜的巴士、火车和飞机
2019/08/27 全球购物
文艺演出策划方案
2014/06/07 职场文书
佛光寺导游词
2015/02/10 职场文书
银行稽核岗位职责
2015/04/13 职场文书
2015年公务员试用期工作总结
2015/05/28 职场文书
导游词之北京明十三陵
2019/10/28 职场文书