exif的使用

最近在使用python3操作exif的相关信息,找了相关的exif信息的写入,终于能满足自己的需求,在这里记录一下查询的结果。

exif的tag标签和属性:
http://exiv2.org/tags.html

pyexiv2是python2版本,window和linux都可用(资料相对较多,可以自行查找):
pyexiv2相关参考网址:
https://www.cnblogs.com/lijia168/p/6693862.html

 

python2和python3版本的都有,linux比较适用,没有可直接使用的window的安装包,需要借助其他软件进行编译,才能使用,如果是linux用户,可以使用比较方便。
gexiv2相关参考网址:
https://wiki.gnome.org/action/show/Projects/gexiv2?action=show&redirect=gexiv2
https://lazka.github.io/pgi-docs/#GExiv2-0.10

 

py3exiv2是根据pyexiv2的python2版本转化为python3版本,linux比较适用,没有可直接使用的window的安装包,需要借助其他软件进行编译,才能使用,如果是linux用户,可以使用比较方便。
py3exiv2相关参考网址:
https://github.com/mcmclx/py3exiv2
https://python3-exiv2.readthedocs.io/en/latest/developers.html
https://pypi.org/project/py3exiv2/
http://www.py3exiv2.tuxfamily.org/

piexif是python3版本,建议使用pyexiv2,操作相对简单,但是相关教程比较少,可使用window和linux版本。
piexif相关参考网址:
https://pypi.org/project/piexif/#description
https://piexifjs.readthedocs.io/en/latest/index.html      piexif的技术文档      
https://blog.csdn.net/cracker_zhou/article/details/51345649       参考
https://shkspr.mobi/blog/2017/11/adjusting-timestamps-on-images-with-python/     调整时间参数数https://programtalk.com/python-examples/piexif./          piexif的函数的相关demo
https://gist.github.com/c060604/8a51f8999be12fc2be498e9ca56adc72     GPS信息(代码在下面)
https://gist.github.com/NeoFarz/27f35ec2f84f5c52394cea3891c18832     时间信息(代码在下面)

备注:有些网站需要梯子,将相关网址复制到浏览器可进行查阅

读取exif信息并调整时间:

#! python3
# photoDateTime.py - Adds DateTime to images without EXIF DateTimeOriginal

import os, datetime, shutil, sys

try :
  import piexif
except ImportError:  
  exit("This script requires piexif.\nInstall with pip3 install piexif")

def get_datetime(dateFormat, f) :
  epoch_s = input('Enter DateTime (YYYY:MM:DD HH:MM:SS) for ' + f + ': ')
  try :
    epoch_t = datetime.datetime.strptime(epoch_s, dateFormat)
  except ValueError:
    exit('INVALID INPUT: \'' + epoch_s + '\' must but in ' + dateFormat + ' format')
  return(epoch_t.strftime(dateFormat).encode())

def main(argv) :
  dateFormat = '%Y:%m:%d %H:%M:%S'
  dir = sys.argv[1]
  suff = sys.argv[2]
  os.chdir(dir)
  srclist = os.listdir('.')
  
  for f in srclist :
    fUpper = f.upper()
    if (suff in fUpper) :
      ftags = piexif.load(f)
      if not piexif.ExifIFD.DateTimeDigitized in ftags['Exif']:
        epoch = get_datetime(dateFormat, f)
        ftags['Exif'][piexif.ExifIFD.DateTimeOriginal] = epoch
        ftags['Exif'][piexif.ExifIFD.DateTimeDigitized] = epoch
      elif not piexif.ExifIFD.DateTimeOriginal in ftags['Exif']:
        ftags['Exif'][piexif.ExifIFD.DateTimeOriginal] = tags['Exif'][piexif.ExifIFD.DateTimeDigitized]
      else :
        print(f + ' already has a DataTimeOriginal value: ' + ftags['Exif'][piexif.ExifIFD.DateTimeOriginal].decode('utf-8'))
        continue
      piexif.insert(piexif.dump(ftags), f)
      print(f + ' has an updated DateTime of: ' + epoch.decode('utf-8'))
      
    else :
      continue
  
  pass

if __name__ == "__main__":
    main(sys.argv)

读取exif信息写入GPS信息:

import os
import piexif
from fractions import Fraction

def to_deg(value, loc):
    """convert decimal coordinates into degrees, munutes and seconds tuple
    Keyword arguments: value is float gps-value, loc is direction list ["S", "N"] or ["W", "E"]
    return: tuple like (25, 13, 48.343 ,'N')
    """
    if value < 0:
        loc_value = loc[0]
    elif value > 0:
        loc_value = loc[1]
    else:
        loc_value = ""
    abs_value = abs(value)
    deg =  int(abs_value)
    t1 = (abs_value-deg)*60
    min = int(t1)
    sec = round((t1 - min)* 60, 5)
    return (deg, min, sec, loc_value)


def change_to_rational(number):
    """convert a number to rantional
    Keyword arguments: number
    return: tuple like (1, 2), (numerator, denominator)
    """
    f = Fraction(str(number))
    return (f.numerator, f.denominator)


def set_gps_location(file_name, lat, lng, altitude):
    """Adds GPS position as EXIF metadata
    Keyword arguments:
    file_name -- image file
    lat -- latitude (as float)
    lng -- longitude (as float)
    altitude -- altitude (as float)
    """
    lat_deg = to_deg(lat, ["S", "N"])
    lng_deg = to_deg(lng, ["W", "E"])

    exiv_lat = (change_to_rational(lat_deg[0]), change_to_rational(lat_deg[1]), change_to_rational(lat_deg[2]))
    exiv_lng = (change_to_rational(lng_deg[0]), change_to_rational(lng_deg[1]), change_to_rational(lng_deg[2]))

    gps_ifd = {
        piexif.GPSIFD.GPSVersionID: (2, 0, 0, 0),
        piexif.GPSIFD.GPSAltitudeRef: 1,
        piexif.GPSIFD.GPSAltitude: change_to_rational(round(altitude)),
        piexif.GPSIFD.GPSLatitudeRef: lat_deg[3],
        piexif.GPSIFD.GPSLatitude: exiv_lat,
        piexif.GPSIFD.GPSLongitudeRef: lng_deg[3],
        piexif.GPSIFD.GPSLongitude: exiv_lng,
    }

    exif_dict = {"GPS": gps_ifd}
    exif_bytes = piexif.dump(exif_dict)
    piexif.insert(exif_bytes, file_name)

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Photo Exif Editor 是一款功能强大的照片 EXIF 编辑软件,可以用于修改照片的元数据信息。使用这款软件非常简单,下面我来详细介绍一下使用方法。 首先,打开 Photo Exif Editor 软件。你可以从官方网站下载并安装它,然后在计算机上打开软件。 其次,导入要编辑的照片。你可以选择从计算机上的文件夹中直接拖拽照片到软件界面,或者点击软件界面上的 "导入" 按钮,然后选择要编辑的照片。 接下来,选择要编辑的照片的 EXIF 信息。软件界面会显示照片的元数据信息,如拍摄日期、照相机型号、光圈、曝光时间等等。你可以根据需要选择要编辑的特定信息。 然后,进行编辑。点击你选择要编辑的信息旁边的 "编辑" 按钮,然后输入你想要修改为的新值。例如,你可以编辑拍摄日期,将其修改为你想要的日期。同样地,你还可以编辑其他的元数据信息。 最后,保存所做的编辑。点击软件界面上的 "保存" 按钮,然后选择保存照片的路径和文件名。软件会将修改后的照片保存到指定的位置。 需要注意的是,修改照片的 EXIF 信息可能会导致一些问题,比如照片的日期与实际拍摄日期不一致等。因此,在使用 Photo Exif Editor 进行编辑之前,最好提前备份原始照片,以防不必要的麻烦。 综上所述,Photo Exif Editor 是一款非常方便易用的照片 EXIF 编辑软件。通过简单的几个步骤,你可以轻松地修改照片的元数据信息,实现你想要的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值