首页 > 代码库 > Python 批量修改图片exif属性

Python 批量修改图片exif属性

Python 批量修改图片exif属性

一共两个文件

config.ini  //放配置文件

exif.py     //主程序文件

config.ini 文件源码

[global]
Artist = www.blmm.com ;作者
DateTimeOriginal = now ;时间(now为当前时间,如指定时间则格式为:2017:04:09 14:13:22)
Software = Adobe Photoshop CS6 Windows ;程序名称

exif.py 文件源码

#ecoding:utf-8
import pyexiv2 as ev
import time
import os
import ConfigParser
class exif():
    def __init__(self):
        config = ConfigParser.ConfigParser()
        config.readfp(open(‘config.ini‘, "rb"))
        self.Artist = config.get("global", "Artist")
        self.DateTimeOriginal = config.get("global", "DateTimeOriginal")
        self.Software = config.get("global", "Software")
    def imgSave(self,dirname):
        for filename in os.listdir(dirname):
            path = dirname + filename
            if os.path.isdir(path):
                path += ‘/‘
                self.imgSave(path)
            else:
                self.imgExif(path)
    def imgExif(self,path):
        try:
            if self.DateTimeOriginal == "now":
                mytime = time.strftime(‘%Y:%m:%d %H:%M:%S‘,time.localtime(time.time()))
            else:
                mytime = self.DateTimeOriginal
            exiv_image = ev.ImageMetadata(path)
            exiv_image.read()
            exiv_image["Exif.Image.Artist"] = self.Artist
            exiv_image["Exif.Photo.DateTimeOriginal"] = mytime
            exiv_image["Exif.Image.Software"] = self.Software
            exiv_image.write()
            print u‘图片:‘,path,u‘操作成功‘
        except:
            print u‘图片:‘,path,u‘操作失败‘
    def star(self):
        path =  raw_input(unicode(‘请输入图片路径:‘,‘utf-8‘).encode(‘gbk‘))
        #newpath = unicode(path, ‘utf8‘)
        self.imgSave(path+‘/‘)
        self.star()
print u‘#------------------------------------‘
print u‘# 程序:批量修改图片exif信息‘
print u‘# 路径格式为:G:\图片‘
print u‘#------------------------------------‘
Exif = exif()
Exif.star()

运行效果:

技术分享

拖动文件夹到窗口回车

技术分享

效果图

技术分享

注:修改exif信息用到了pyexiv2 模块, 下载地址

https://launchpad.net/pyexiv2

 

Python 批量修改图片exif属性