Python修复图像文件后缀名

网上爬了很多图片,有很多错误。

有的不是图片文件,需要删除

有的后缀名错误,需要更正

用的的python脚本

#!/usr/bin/env python
#-*- coding: utf-8 -*-
# fixImageExt.py
from PIL import Image imoport os import sys f = open(sys.argv[1]) # 文本文件,每一行是一个文件路径 for i, line in enumerate(f): fullName = line.strip() if not os.path.exists(fullName): continue try: img = Image.open(fullName) except: # remove broken image files print "Broken: %d %s"%(i, fullName) os.remove(fullName) else: newName = os.path.dirName(fullName) + str(i) + img.format.lower() os.rename(fullName, newName) print 'Rename ' + fullName + ' --> ' + newName

步骤:

1. 生成图片列表

 find ImageDir -type f > images.txt 

2. 运行脚本

 python fixImageExt.py images.txt 

Suzzz博客园作品,转载请注明 http://www.cnblogs.com/Suzzz
原文地址:https://www.cnblogs.com/Suzzz/p/5289622.html