温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用python怎么批量修改图片大小

发布时间:2021-05-19 17:28:35 来源:亿速云 阅读:462 作者:Leah 栏目:开发技术

今天就跟大家聊聊有关使用python怎么批量修改图片大小,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

引用的模块

from PIL import Image

Image的使用

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))       else:         Py_Log("图片尺寸正确,未修改")     else:       Py_Log("非图片格式")   except Exception, e:     print e def printFile(dirPath):   print "file: " + dirPath   resize_image(dirPath)   return True

引用

if __name__ == '__main__':   path = "E:\pp\icon_setting.png"   new_width = 50   try:     b = printFile(path)     Py_Log("\r\n     **********\r\n" + "*********图片处理完毕*********" + "\r\n     **********\r\n")   except:     print "Unexpected error:", sys.exc_info()

上述是修改单一的图片,若要批量修改文件夹下的所有图片,则要使用循环,在上面基础添加 例如:

def BFS_Dir(dirPath, dirCallback=None, fileCallback=None):   queue = []   ret = []   queue.append(dirPath);   while len(queue) > 0:     tmp = queue.pop(0)     if os.path.isdir(tmp):       ret.append(tmp)       for item in os.listdir(tmp):         queue.append(os.path.join(tmp, item))       if dirCallback:         dirCallback(tmp)     elif os.path.isfile(tmp):       ret.append(tmp)       if fileCallback:         fileCallback(tmp)   return ret

python主要应用领域有哪些

1、云计算,典型应用OpenStack。2、WEB前端开发,众多大型网站均为Python开发。3.人工智能应用,基于大数据分析和深度学习而发展出来的人工智能本质上已经无法离开python。4、系统运维工程项目,自动化运维的标配就是python+Django/flask。5、金融理财分析,量化交易,金融分析。6、大数据分析。

看完上述内容,你们对使用python怎么批量修改图片大小有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI