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 生成不重复的随机数的代码
May 15 Python
python求解水仙花数的方法
May 11 Python
python django事务transaction源码分析详解
Mar 17 Python
神经网络python源码分享
Dec 15 Python
python中实现数组和列表读取一列的方法
Apr 03 Python
Python列表解析配合if else的方法
Jun 23 Python
Python用5行代码写一个自定义简单二维码
Oct 21 Python
Python告诉你木马程序的键盘记录原理
Feb 02 Python
实例讲解Python中整数的最大值输出
Mar 17 Python
matplotlib命令与格式之tick坐标轴日期格式(设置日期主副刻度)
Aug 06 Python
python 通过文件夹导入包的操作
Jun 01 Python
keras 简单 lstm实例(基于one-hot编码)
Jul 02 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 smarty的预保留变量总结
2008/12/04 PHP
php中批量替换文件名的实现代码
2011/07/20 PHP
php中计算程序运行时间的类代码
2012/11/03 PHP
PHP utf-8编码问题,utf8编码,数据库乱码,页面显示输出乱码
2013/04/08 PHP
浅谈web上存漏洞及原理分析、防范方法(安全文件上存方法)
2013/06/29 PHP
PHP采用curl模仿用户登陆新浪微博发微博的方法
2014/11/07 PHP
Laravel如何使用数据库事务及捕获事务失败后的异常详解
2017/10/23 PHP
JQuery 1.6发布 性能提升,同时包含大量破坏性变更
2011/05/10 Javascript
JS 页面计时器示例代码
2013/10/28 Javascript
js鼠标点击图片切换效果代码分享
2015/08/26 Javascript
Node.js+Express配置入门教程
2016/05/19 Javascript
JS跨域交互(jQuery+php)之jsonp使用心得
2016/07/01 Javascript
require.js 加载 vue组件 r.js 合并压缩的实例
2016/10/14 Javascript
vue-cli项目修改文件热重载失效的解决方法
2018/09/19 Javascript
JavaScript单线程和任务队列原理解析
2020/02/04 Javascript
JS实现选项卡插件的两种写法(jQuery和class)
2020/12/30 jQuery
python利用hook技术破解https的实例代码
2013/03/25 Python
python静态方法实例
2015/01/14 Python
python+opencv+caffe+摄像头做目标检测的实例代码
2018/08/03 Python
Django框架 querySet功能解析
2019/09/04 Python
通过字符串导入 Python 模块的方法详解
2019/10/27 Python
pytorch+lstm实现的pos示例
2020/01/14 Python
Python中使用socks5设置全局代理的方法示例
2020/04/15 Python
Python Matplotlib绘图基础知识代码解析
2020/08/31 Python
CSS3 Flexbox中flex-shrink属性的用法示例介绍
2013/12/30 HTML / CSS
德国电子产品购物网站:TechInTheBasket德国
2018/12/07 全球购物
服务员自我评价
2014/01/25 职场文书
家长对孩子的评语
2014/04/18 职场文书
保护环境倡议书500字
2014/05/19 职场文书
生物工程专业求职信
2014/09/03 职场文书
党员教师四风问题对照检查材料
2014/09/26 职场文书
主持人开场白台词
2015/05/29 职场文书
欠条格式范本
2015/07/03 职场文书
Node.js实现断点续传
2021/06/23 Javascript
python人工智能human learn绘图可创建机器学习模型
2021/11/23 Python
引用计数法和root搜索算法以及JVM中判定对象需要回收的方法
2022/04/19 Java/Android