要手动修改图片的EXIF信息,可以使用Python的PIL库(Pillow)来实现。以下是一个示例代码,演示如何使用PIL库修改图片的拍摄日期(DateTime)的EXIF信息:
from PIL import Image from PIL.ExifTags import TAGS def modify_exif(image_path, new_datetime): # 打开图片 image = Image.open(image_path) # 获取图片的EXIF信息 exif_data = https://www.yisu.com/ask/image._getexif()'DateTimeOriginal'] = new_datetime exif_dict['DateTimeDigitized'] = new_datetime # 将修改后的EXIF信息转换回元组形式 new_exif_data = https://www.yisu.com/ask/{TAGS[key]: exif_dict[TAGS[key]] for key in exif_dict.keys()}"modified_image.jpg", exif=new_exif_data) print("修改成功") # 示例用法 image_path = "example.jpg" new_datetime = "2022:01:01 12:00:00" modify_exif(image_path, new_datetime)
在上面的示例中,modify_exif
函数接受一个图片路径和一个新的拍摄日期作为参数。函数通过PIL库打开图片,并使用_getexif
方法获取图片的EXIF信息。然后,将EXIF信息转换为字典形式,修改拍摄日期的值,并将修改后的EXIF信息转换回元组形式。最后,创建一个新的图片对象,将修改后的EXIF信息写入其中,并保存为一个新的图片文件。
请注意,要使用PIL库,你需要通过pip install pillow
命令安装它。