• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

python批量修改图片大小的方法

python 搞代码 4年前 (2022-01-08) 13次浏览 已收录 0个评论

这篇文章主要为大家详细介绍了python批量修改图片大小的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了python批量修改图片大小的具体代码,供大家参考,具体内容如下

引用的模块

 from PIL import Image

Image的使用

 def resize_image(img_path): try: mPath, ext = os.path.splitext(img_path) if astrcmp(ext, ".png-600") or astrcmp(ext, ".jpg-600"): 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-600" 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 fi<span style="color:transparent">来源gaodai#ma#com搞*!代#%^码$网</span>leCallback: fileCallback(tmp) return ret

第一个参数为图片的目录路径,第二个参数是(目录路劲的回掉方法),第三个参数是图片处理回掉方法

源代码参考:Python_Tool

以上就是python批量修改图片大小的方法的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python批量修改图片大小的方法

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址