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开发中module模块用法实例分析
Nov 12 Python
Python中getattr函数和hasattr函数作用详解
Jun 14 Python
Python进度条实时显示处理进度的示例代码
Jan 30 Python
Python返回数组/List长度的实例
Jun 23 Python
Python实现根据日期获取当天凌晨时间戳的方法示例
Apr 09 Python
在win10和linux上分别安装Python虚拟环境的方法步骤
May 09 Python
基于Pytorch SSD模型分析
Feb 18 Python
使用Pycharm分段执行代码
Apr 15 Python
python中wheel的用法整理
Jun 15 Python
python软件都是免费的吗
Jun 18 Python
python matlab库简单用法讲解
Dec 31 Python
Python 一键获取电脑浏览器的账号密码
May 11 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中cookie的作用域
2008/03/27 PHP
php中Y2K38的漏洞解决方法实例分析
2014/09/22 PHP
PHP中基本HTTP认证技巧分析
2015/03/16 PHP
jquery获取多个checkbox的值异步提交给php的方法
2015/06/24 PHP
教你在header中隐藏php的版本信息
2016/08/10 PHP
php mysql数据库操作类(实例讲解)
2017/08/06 PHP
php 处理png图片白色背景色改为透明色的实例代码
2018/12/10 PHP
PHP从零开始打造自己的MVC框架之类的自动加载实现方法详解
2019/06/03 PHP
一段利用WSH修改和查看IP配置的代码
2008/05/11 Javascript
javascript 操作cookies及正确使用cookies的属性
2009/10/15 Javascript
如何让页面在打开时自动刷新一次让图片全部显示
2012/12/17 Javascript
jQuery图片滚动图片的效果(另类实现)
2013/06/02 Javascript
JavaScript获取网页表单action属性的方法
2015/04/02 Javascript
基于jQuery和CSS3制作响应式水平时间轴附源码下载
2015/12/20 Javascript
再谈JavaScript异步编程
2016/01/27 Javascript
jQuery实现图片局部放大镜效果
2016/03/17 Javascript
微信小程序 判断手机号的实现代码
2017/04/19 Javascript
Angular 4.x 动态创建表单实例
2017/04/25 Javascript
详解JavaScript的BUG和错误
2018/05/07 Javascript
layer弹出层显示在top顶层的方法
2019/09/11 Javascript
vue+高德地图实现地图搜索及点击定位操作
2020/09/09 Javascript
[04:20]DOTA2-DPC中国联赛 正赛 VG vs LBZS 选手采访 1月19日
2021/03/11 DOTA
python pyenv多版本管理工具的使用
2019/12/23 Python
Python OpenCV去除字母后面的杂线操作
2020/07/05 Python
jupyter 添加不同内核的操作
2021/02/06 Python
css3气泡 css3关键帧动画创建的动态通知气泡
2013/02/26 HTML / CSS
纯css3实现走马灯效果
2014/12/26 HTML / CSS
css3 中translate和transition的使用方法
2020/03/26 HTML / CSS
英国儿童设计师服装的领先零售商:Base
2019/03/17 全球购物
学生安全责任书范本
2014/07/24 职场文书
甜品店创业计划书
2014/08/14 职场文书
公安机关纪律作风整顿个人剖析材料材料
2014/10/10 职场文书
小数乘法教学反思
2016/02/22 职场文书
linux中nohup和后台运行进程查看及终止
2021/06/24 Python
Vue的过滤器你真了解吗
2022/02/24 Vue.js
Python编程中内置的NotImplemented类型的用法
2022/03/23 Python