【问题标题】:How to extract modify date from the EXIF?如何从 EXIF 中提取修改日期?
【发布时间】:2023-01-25 11:27:47
【问题描述】:
我尝试了以下代码:
from PIL import Image
def get_exif(filename):
image = Image.open(filename)
image.verify()
return image.getexif()
def get_exif_date(exif):
return exif.get_ifd(0x0132)
ff = 'path/to/file.jpg'
image_exif = get_exif(ff)
print(str(image_exif))
image_date = get_exif_date(image_exif)
print(str(image_date))
它返回以下输出:
{34853: 2068, 296: 2, 34665: 228, 271: 'Apple', 272: 'iPhone X', 305: '14.4.2', 274: 1, 306: '2021:05:02 17:27:18', 531: 1, 282: 72.0, 283: 72.0, 316: 'iPhone X'}
{}
所以,ModifyDate tag (306=0x0132) 在这里,但我无法提取它。知道为什么吗?
【问题讨论】:
标签:
python
python-imaging-library
exif
【解决方案1】:
from PIL import Image
import piexif
# change date in a image exif data
image = Image.open('image.jpg') # open image
exif_dict = piexif.load(image.info['exif']) # get info dict
print(exif_dict) # check info dict structure
# if there are not exif data you could set a structure like this
if not exif_dict:
exif_dict = {'0th': {}, 'Exif': {}, 'GPS': {}, 'Interop': {}, '1st':
{}, 'thumbnail': None}
new_date = b'2014:02:14 22:21:18' # choose new date
exif_dict['Exif'][36867] = new_date # this ->(['Exif'][36867])<- could be
# different, check in your info dict structure
print(exif_dict) # check new info dict structure
new_exif_data = piexif.dump(exif_dict) # set new exif data
image.save('new_image.jpg', exif=new_exif_data) # save a new image equal
# to original with new exif data