Python批量按比例缩小图片脚本分享

yipeiwu_com6年前Python基础

图片太大了,上百张图用photoshop改太慢,就想到用python写个简单的批处理。功能简单就是把原图按比例缩小

复制代码 代码如下:

# -*- coding: cp936 -*- 

import Image 
import glob, os 

#图片批处理 
def timage(): 
    for files in glob.glob('D:\\\\1\\\\*.JPG'): 
        filepath,filename = os.path.split(files) 
        filterame,exts = os.path.splitext(filename) 
        #输出路径 
        opfile = r'D:\\\\22\\\\'
        #判断opfile是否存在,不存在则创建 
        if (os.path.isdir(opfile)==False): 
            os.mkdir(opfile) 
        im = Image.open(files) 
        w,h = im.size 
        #im_ss = im.resize((400,400)) 
        #im_ss = im.convert('P') 
        im_ss = im.resize((int(w*0.12), int(h*0.12))) 
        im_ss.save(opfile+filterame+'.jpg') 

if __name__=='__main__': 
    timage() 

    print '哈哈完蛋啦'

相关文章

Python实现截屏的函数

本文实例讲述了Python实现截屏的函数。分享给大家供大家参考。具体如下: 1.可指定保存目录. 2.截屏图片名字以时间为文件名 3.截屏图片存为JPG格式图片,比BMP小多的,一个10...

Python中在for循环中嵌套使用if和else语句的技巧

for...[if]...构建List (List comprehension) 1.简单的for...[if]...语句 Python中,for...[if]...语句一种简洁的构建L...

浅谈python 四种数值类型(int,long,float,complex)

Python支持四种不同的数值类型,包括int(整数)long(长整数)float(浮点实际值)complex (复数),本文章向码农介绍python 四种数值类型,需要的朋友可以参考一...

pytorch 在sequential中使用view来reshape的例子

pytorch中view是tensor方法,然而在sequential中包装的是nn.module的子类, 因此需要自己定义一个方法: import torch.nn as nn c...

跟老齐学Python之编写类之四再论继承

跟老齐学Python之编写类之四再论继承

在上一讲代码的基础上,做进一步修改,成为了如下程序,请看官研习这个程序: 复制代码 代码如下: #!/usr/bin/env python #coding:utf-8 class Per...