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使用urllib模块开发的多线程豆瓣小站mp3下载器
Jan 16 Python
详解Python3中的Sequence type的使用
Aug 01 Python
python 请求服务器的实现代码(http请求和https请求)
May 25 Python
python write无法写入文件的解决方法
Jan 23 Python
理想高通滤波实现Python opencv示例
Jan 30 Python
Django模型修改及数据迁移实现解析
Aug 01 Python
最新2019Pycharm安装教程 亲测
Feb 28 Python
Python网络爬虫信息提取mooc代码实例
Mar 06 Python
python实现图像拼接功能
Mar 23 Python
Python filter()及reduce()函数使用方法解析
Sep 05 Python
python基础之while循环语句的使用
Apr 20 Python
pytorch 两个GPU同时训练的解决方案
Jun 01 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
综合图片计数器
2006/10/09 PHP
数理公式,也可以这么唯美
2021/03/10 无线电
javascript实现日历控件(年月日关闭按钮)
2012/12/12 Javascript
window.open关于浏览器拦截问题分析及解决方法
2013/02/05 Javascript
JS 屏蔽按键效果与改变按键效果的示例代码
2013/12/24 Javascript
js查找某元素中的所有图片地址的方法
2014/01/16 Javascript
JS控制弹出新页面窗口位置和大小的方法
2015/03/02 Javascript
浅谈js 闭包引起的内存泄露问题
2015/06/22 Javascript
js实现文本框只允许输入数字并限制数字大小的方法
2015/08/19 Javascript
基于JS实现省市联动效果代码分享
2016/06/06 Javascript
JS中innerHTML和pasteHTML的区别实例分析
2016/06/22 Javascript
javascript 常用验证函数总结
2016/06/28 Javascript
微信小程序 MINA文件结构
2016/10/17 Javascript
JavaScript简单验证表单空值及邮箱格式的方法
2017/01/20 Javascript
React-intl 实现多语言的示例代码
2017/11/03 Javascript
浅谈React + Webpack 构建打包优化
2018/01/23 Javascript
jQuery实现下拉菜单动态添加数据点击滑出收起其他功能
2018/06/14 jQuery
解决vue-cli webpack打包后加载资源的路径问题
2018/09/25 Javascript
Python的randrange()方法使用教程
2015/05/15 Python
python 如何快速找出两个电子表中数据的差异
2017/05/26 Python
对python:print打印时加u的含义详解
2018/12/15 Python
python开发之anaconda以及win7下安装gensim的方法
2019/07/05 Python
python 按钮点击关闭窗口的实现
2020/03/04 Python
Python 实现键盘鼠标按键模拟
2020/11/18 Python
python Autopep8实现按PEP8风格自动排版Python代码
2021/03/02 Python
Piercing Pagoda官网:耳环、戒指、项链、手链等
2020/09/28 全球购物
初中地理教学反思
2014/01/11 职场文书
生产部厂长职位说明书
2014/03/03 职场文书
2014年道德讲堂实施方案
2014/03/05 职场文书
1亿有多大教学反思
2014/05/01 职场文书
建筑工程技术专业求职信
2014/07/16 职场文书
辩护词范文大全
2015/05/21 职场文书
CSS中Single Div 绘图技巧的实现
2021/06/18 HTML / CSS
MySQL如何解决幻读问题
2021/08/07 MySQL
Python接口自动化之文件上传/下载接口详解
2022/04/05 Python
从零开始在Centos7上部署SpringBoot项目
2022/04/07 Servers