介绍
今天就跟大家聊聊有关使用python怎么批量修改图片大小,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
引用的模块
得到PIL import 图像
图像的使用
def resize_image (img_path): 尝试才能: ,,,,,,mPath ext =, os.path.splitext (img_path) ,,,if astrcmp (ext,“.png"),或是astrcmp (ext,“.jpg"): ,,,,,img =, Image.open (img_path) ,,,,,(宽度,高度),=img.size ,,,,,if width !=, new_width: ,,,,,,,new_height =, int (height *, new_width /,宽度) ,,,,,,,out =, img.resize ((new_height new_width也),Image.ANTIALIAS) ,,,,,,,new_file_name =, & # 39; % s % & # 39;, %, (mPath, ext) ,,,,,,,out.save (new_file_name,,质量=100) ,,,,,,,Py_Log(“图片尺寸修改为:“,+,str (new_width)) ,,,,,其他的: ,,,,,,,Py_Log(“图片尺寸正确,未修改“) ,,,: ,,,,,Py_Log(“非图片格式“) ,,except 例外,e: ,,,print e def printFile (dirPath): print 才能“文件:,“,+ dirPath resize_image才能(dirPath) return 才能真正的
引用
if __name__ ==, & # 39; __main__ # 39;: path =,才能“E: \ pp \ icon_setting.png" new_width 才能=50 尝试才能: ,,,b =, printFile(路径) ,,,Py_Log (“\ r \ n ,,,, * * * * * * * * * * \ r \ n", +,“* * * * * * * * *图片处理完毕* * * * * * * * *“,+,“\ r \ n ,,,, * * * * * * * * * * \ r \ n") 除了才能: ,,,print “Unexpected 错误:“,sys.exc_info ()
上述是修改单一的图片,若要批量修改文件夹下的所有图片,则要使用循环,在上面基础添加例如:
def BFS_Dir (dirPath, dirCallback=没有,fileCallback=None): 时间=queue 才能;[] 时间=ret 才能;[] queue.append才能(dirPath); while 才能;len(队列),祝辞,0: ,,,tmp =, queue.pop (0) ,,,if os.path.isdir (tmp): ,,,,,ret.append (tmp) ,,,,,for item 拷贝os.listdir (tmp): ,,,,,,,queue.append (os.path.join (tmp,,项目)) ,,,,,if dirCallback: ,,,,,,,dirCallback (tmp) ,,,elif os.path.isfile (tmp): ,,,,,ret.append (tmp) ,,,,,if fileCallback: ,,,,,,,fileCallback (tmp) return 才能ret
python主要应用领域有哪些
1,云计算,典型应用OpenStack.2,网前端开发,众多大型网站均为python开发。3。人工智能应用,基于大数据分析和深度学习而发展出来的人工智能本质上已经无法离开python.4,系统运维工程项目,自动化运维的标配就是python + Django/flask.5,金融理财分析,量化交易,金融分析。6,大数据分析。
看完上述内容,你们对使用python怎么批量修改图片大小有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。