最近要处理一批图片,但某软件持续报错,发现必须得删除文件中的Exif信息才能正常运行。于是动手。
方案1:
用Python的piexif,很容易搞定去除Exif,看了看piexif的文档,用法也很简单。
piexif.remove(file)
找了五百张图片试了试,还是不行。仔细看了下,piexif只是去除了普通的Exif,还有不少扩展的信息没有去除。
方案2:
def clearExifInfo(photoAddress):
image = Image.open(photoAddress)
data = list(image.getdata())
image_without_exif = Image.new(image.mode, image.size)
image_without_exif.putdata(data)
image_without_exif.save(photoAddress)
return
网上找到这个办法,试了试,10万张图,去除的效果不错。
完整代码如下:
from PIL import Image
import os,time
import sys
def clearExifInfo(photoAddress):
image = Image.open(photoAddress)
data = list(image.getdata())
image_without_exif = Image.new(image.mode, image.size)
image_without_exif.putdata(data)
image_without_exif.save(photoAddress)
return
def clearExif(path):
startTime = time.time()
countNums = 0
for root, dirs, files in os.walk(path):
for name in files:
if name.endswith(".JPG") or name.endswith(".jpg"):
photoAddress = os.path.join(root,name)
clearExifInfo(photoAddress)
#print(photoAddress)
countNums += 1
if countNums % 1000 == 0:
print("已处理{0}目录中,总第{1}张图像".format(root, countNums ))
return
#命令行用法 python 去除exif.py E:\SB_PHOTO
if __name__ == '__main__':
photoDir = sys.argv[1]
clearExif(photoDir)
参考资料:
stackoverflowhttps://stackoverflow.com/questions/19786301/python-remove-exif-info-from-images