python批量改图片_python实现批量修改图片格式和尺寸

#coding=utf-8

import sys

import os, glob

import platform

import win32file,win32con

from PIL import Image

from send2trash import send2trash

reload(sys)

sys.setdefaultencoding('utf-8')

#new_width =2048

#width =int(raw_input("the width U want:"))

#imgslist = glob.glob(path+'/*.*')

ShuiPing="水平"

ShiZhuang="矢状"

GuanZhuang="冠状"

def Py_Log(_string):

print "----"+_string.decode('utf-8')+"----"

def is_windows_system():

return 'Windows' in platform.system()

def is_hiden_file(file_Path):

if is_windows_system():

fileAttr = win32file.GetFileAttributes(file_Path)

if fileAttr & win32con.FILE_ATTRIBUTE_HIDDEN :

return True

return False

return False

def remove_hidden_file(file_path):

send2trash(file_path)

print "Delete hidden file path:"+file_path

def astrcmp(str1,str2):

return str1.lower()==str2.lower()

def resize_image(img_path):

try:

mPath, ext = os.path.splitext(img_path)

if (astrcmp(ext,".png") or astrcmp(ext,".jpg")):

img = Image.open(img_path)

(width,height) = img.size

if(width != new_width):

new_height = int(height * new_width / width)

out = img.resize((new_width,new_height),Image.ANTIALIAS)

new_file_name = '%s%s' %(mPath,ext)

out.save(new_file_name,quality=100)

Py_Log("图片尺寸修改为:"+str(new_width))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值