如何从EXIF提取修改日期?

发布于 2025-02-13 22:27:02 字数 733 浏览 2 评论 0原文

我尝试了以下代码:

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'}
{}

so, modifydate tag (306 = 0x0132 )在这里,但我不能提取它。知道为什么吗?

I tried the following code:

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))

it returns the following output:

{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'}
{}

So, ModifyDate tag (306=0x0132) is here, but I can not extract it. Any idea why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄居者 2025-02-20 22:27:04
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文