2017-04-15 104 views
0

一直在那裏四處尋找仍然似乎沒有一個很好的文件和圖像exif /元數據操作的最新。圖片exif /元數據閱讀/寫作?

我必須通過文件夾重命名取決於它們的文件夾名稱的文件使用piexif

import os 
import piexif 

for root, dirs, files in os.walk("C:\\Users\\Alan Partridge\\Desktop\\images"): 
    if not files: 
     continue 
    prefix = os.path.basename(root) 
    for f in files: 
     #print os.path.join(root, f) 
     exif_dict = piexif.load(os.path.join(root, f)) 
     print exif_dict 
     for ifd in ("0th", "Exif", "GPS", "1st"): 
      for tag in exif_dict[ifd]: 
       print(piexif.TAGS[ifd][tag]["name"], exif_dict[ifd][tag]) 
     #os.rename(os.path.join(root, f), os.path.join(root, "{} ID {}".format(prefix, f))) 

下面的代碼它循環(註釋),但現在,它輸出的任何數據圖書館可以找到。

目前,它可以找到ImageDescription,這很好。我的問題是我將如何去修改它?不幸的是,我無法做到,也無法做到這一點。也許我錯過了一些明顯的東西?

回答

0

圖片EXIF信息可以在這個文件中找到:

http://www.cipa.jp/std/documents/e/DC-008-2012_E.pdf

要使用piexif庫構建EXIF信息,並寫入到目標文件:(蟒蛇)

建立EXIF信息,以保存來自源文件的方向,通過窗口讀取它以顯示正確的方向。

源:http://piexif.readthedocs.io/en/latest/functions.html?highlight=save

zeroth_ifd = {piexif.ImageIFD.Orientation: image_orientation, 
       piexif.ImageIFD.ImageWidth: int(new_width), 
       piexif.ImageIFD.ImageLength: int(new_height), 
       piexif.ImageIFD.Software: u"piexif" 
       } 

exif_dict = {"0th":zeroth_ifd} 
exif_bytes = piexif.dump(exif_dict) 

with open(dest, 'r+b') as f: 
    with Image.open(dest,'r') as image: 
     image.save(dest, "jpeg", exif=exif_bytes)