python 批量修改图片大小
来源:互联网 发布:网络访问安全设计 编辑:程序博客网 时间:2024/06/06 17:10
- 引用的模块
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 edef 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
第一个参数为图片的目录路径,第二个参数是(目录路劲的回掉方法),第三个参数是图片处理回掉方法
源代码参考:
https://github.com/lidc-lee/Python_Tool/blob/master/reSizeOfPic.py
阅读全文
1 0
- python 批量修改图片大小
- photoshop批量修改图片大小
- ps批量修改图片大小
- 批量修改图片大小
- 批量修改图片大小
- word批量修改图片大小
- 批量修改图片大小
- Python 图形处理库PIL批量修改图片大小和格式
- Python 图形处理库PIL批量修改图片大小和格式
- Python 图形处理库PIL批量修改图片大小和格式
- (后台)批量修改图片大小
- powershell + python 批量更改图片大小
- 如何使用Photoshop批量修改图片大小
- PS中如何批量修改图片大小
- 批量修改Excel中的图片大小方法
- word文档如何批量修改图片大小
- word 批量修改文档中的图片大小
- 【Ubuntu】批量修改图片大小的命令
- CentOS7网络配置——nmcli命令集
- 【库函数版本】基于STM32F103的MPU6050的原始数据读取程序详解
- JDK源码-CopyOnWriteArrayList.java 顺带 ArrayList Vector Collections.synchronizedList(List list)
- C/C++ EOF文件结束标记总结
- 区间翻转 bzoj 3223 文艺平衡树 (splay)
- python 批量修改图片大小
- Android中设置Ethernet为默认网络类型
- FTP服务器之间的文件传输(sftp连接)
- 民生银行2017校园招聘研发类笔试题
- stylus之混合书写(Mixins)
- java.lang.OutOfMemoryError: unable to create new native thread
- 不能声明为虚函数的函数
- $.extend中探索深拷贝和浅拷贝
- (113)数学表达式节点