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 pickle类库介绍(对象序列化和反序列化)
Nov 21 Python
ubuntu系统下 python链接mysql数据库的方法
Jan 09 Python
python使用Tkinter实现在线音乐播放器
Jan 30 Python
使用requests库制作Python爬虫
Mar 25 Python
Python实现基于KNN算法的笔迹识别功能详解
Jul 09 Python
使用PyQtGraph绘制精美的股票行情K线图的示例代码
Mar 14 Python
PyQt5 QTableView设置某一列不可编辑的方法
Jun 25 Python
Python之虚拟环境virtualenv,pipreqs生成项目依赖第三方包的方法
Jul 23 Python
django框架auth模块用法实例详解
Dec 10 Python
Pytorch 多维数组运算过程的索引处理方式
Dec 27 Python
python数字类型math库原理解析
Mar 02 Python
Python 改变数组类型为uint8的实现
Apr 09 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中的正则表达式
2014/08/17 PHP
PHP 极验验证码实例讲解
2016/09/29 PHP
PHP发送邮件确认验证注册功能示例【修改别人邮件类】
2019/11/09 PHP
js函数的引用, 关于内存的开销
2012/09/17 Javascript
jQuery客户端分页实例代码
2013/11/18 Javascript
js获取对象为null的解决方法
2013/11/21 Javascript
JavaScript访问CSS属性的几种方式介绍
2014/07/21 Javascript
JavaScript对象之深度克隆介绍
2014/12/08 Javascript
Angular2-primeNG文件上传模块FileUpload使用详解
2017/01/14 Javascript
Bootstrap导航条学习使用(一)
2017/02/08 Javascript
jQuery 实时保存页面动态添加的数据的示例
2017/08/14 jQuery
VueJS 集成 Medium Editor的示例代码 (自定义编辑器按钮)
2017/08/24 Javascript
three.js中文文档学习之如何本地运行详解
2017/11/20 Javascript
小试SVG之新手小白入门教程
2019/01/08 Javascript
Vue单文件组件开发实现过程详解
2020/07/30 Javascript
[01:39:42]Fnatic vs Mineski 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/18 DOTA
[50:27]OG vs LGD 2018国际邀请赛淘汰赛BO3 第一场 8.26
2018/08/30 DOTA
PHP魔术方法__ISSET、__UNSET使用实例
2014/11/25 Python
python概率计算器实例分析
2015/03/25 Python
Python实现基于POS算法的区块链
2018/08/07 Python
python3+PyQt5 数据库编程--增删改实例
2019/06/17 Python
windows上安装python3教程以及环境变量配置详解
2019/07/18 Python
扩展Django admin的list_filter()可使用范围方法
2019/08/21 Python
Python tensorflow实现mnist手写数字识别示例【非卷积与卷积实现】
2019/12/19 Python
什么是Python变量作用域
2020/06/03 Python
eBay法国购物网站:eBay.fr
2017/10/21 全球购物
西班牙太阳镜品牌:Hawkers
2018/03/11 全球购物
什么是事务?为什么需要事务?
2012/01/09 面试题
解决方案设计综合面试题
2015/08/31 面试题
岳父生日宴会答谢词
2014/01/13 职场文书
yy司仪主持词
2014/03/22 职场文书
给校长的建议书400字
2014/05/15 职场文书
晚自修旷课检讨书怎么写
2014/11/17 职场文书
关于军训的感想
2015/08/07 职场文书
2021年国漫热度排行前十,完美世界上榜,第四是美国动画作品
2022/03/18 国漫
Win11 KB5015814遇安装失败 影响开始菜单性能解决方法
2022/07/15 数码科技