2015-04-22 35 views
0

我正在編寫一個腳本來保存元數據到JPG圖像並調整它們的Wordpress。它一直很好,使用JPEG和PNG工作,直到我嘗試在使用PIL調整JPG(而不是PNG)之後直接在圖像上使用它。Pyexiv2和PIL文件阻止

我試圖壓縮它下載到一個易於閱讀的課程,以幫助任何可以幫助解答問題的人。主類是MetaGator(meta mitigator),它將屬性'title'和'description'寫入文件中相應的元數據字段。

import PIL 
from PIL import Image 
from PIL import PngImagePlugin 
from pyexiv2.metadata import ImageMetadata 
from PIL import Image 
import os 
import re 
from time import time, sleep 

class MetaGator(object): 

    """docstring for MetaGator""" 
    def __init__(self, path): 
     super(MetaGator, self).__init__() 
     if not os.path.isfile(path): 
      raise Exception("file not found") 

     self.dir, self.fname = os.path.split(path) 

    def write_meta(self, title, description): 
     name, ext = os.path.splitext(self.fname) 
     title, description = map(str, (title, description)) 
     if(ext.lower() in ['.png']): 
      try: 
       new = Image.open(os.path.join(self.dir, self.fname)) 
      except Exception as e: 
       raise Exception('unable to open image: '+str(e)) 
      meta = PngImagePlugin.PngInfo() 
      meta.add_text("title", title) 
      meta.add_text("description", description) 
      try:  
       new.save(os.path.join(self.dir, self.fname), pnginfo=meta) 
      except Exception as e: 
       raise Exception('unable to write image: '+str(e)) 

     elif(ext.lower() in ['.jpeg', '.jpg']): 
      try: 

       imgmeta = ImageMetadata(os.path.join(self.dir, self.fname)) 
       imgmeta.read() 
      except IOError: 
       raise Exception("file not found") 

      for index, value in (
       ('Exif.Image.DocumentName', title), 
       ('Exif.Image.ImageDescription', description), 
       ('Iptc.Application2.Headline', title), 
       ('Iptc.Application2.Caption', description), 
      ): 
       print " -> imgmeta[%s] : %s" % (index, value) 
       if index in imgmeta.iptc_keys:  
        imgmeta[index] = [value] 
       if index in imgmeta.exif_keys: 
        imgmeta[index] = value 
      imgmeta.write() 
     else: 
      raise Exception("not an image file") 



    def read_meta(self): 
     name, ext = os.path.splitext(self.fname) 
     title, description = '', '' 

     if(ext.lower() in ['.png']):  
      oldimg = Image.open(os.path.join(self.dir, self.fname)) 
      title = oldimg.info.get('title','') 
      description = oldimg.info.get('description','') 
     elif(ext.lower() in ['.jpeg', '.jpg']): 
      try: 
       imgmeta = ImageMetadata(os.path.join(self.dir, self.fname)) 
       imgmeta.read() 
      except IOError: 
       raise Exception("file not found") 

      for index, field in (
       ('Iptc.Application2.Headline', 'title'), 
       ('Iptc.Application2.Caption', 'description') 
      ): 
       if(index in imgmeta.iptc_keys): 
        value = imgmeta[index].value 
        if isinstance(value, list): 
         value = value[0] 

        if field == 'title': title = value 
        if field == 'description': description = value 
     else: 
      raise Exception("not an image file")  

     return {'title':title, 'description':description} 

if __name__ == '__main__': 
    print "JPG test" 

    fname_src = '<redacted>.jpg' 
    fname_dst = '<redacted>-test.jpg' 

    metagator_src = MetaGator(fname_src) 
    metagator_src.write_meta('TITLE', time()) 
    print metagator_src.read_meta() 

    image = Image.open(fname_src) 
    image.thumbnail((10,10)) 
    image.save(fname_dst) 

    metagator_dst = MetaGator(fname_dst) 
    metagator_dst.write_meta('TITLE', time()) 
    print metagator_dst.read_meta() 

    sleep(5) 

    metagator_dst = MetaGator(fname_dst) 
    metagator_dst.write_meta('TITLE', time()) 
    print metagator_dst.read_meta() 

    print "PNG test" 

    fname_src = '<redacted>.png' 
    fname_dst = '<redacted>-test.png' 

    metagator_src = MetaGator(fname_src) 
    metagator_src.write_meta('TITLE', time()) 
    print metagator_src.read_meta() 

    image = Image.open(fname_src) 
    image.thumbnail((10,10)) 
    image.save(fname_dst) 

    metagator_dst = MetaGator(fname_dst) 
    metagator_dst.write_meta('TITLE', time()) 
    print metagator_dst.read_meta() 

    sleep(5) 

    metagator_dst = MetaGator(fname_dst) 
    metagator_dst.write_meta('TITLE', time()) 
    print metagator_dst.read_meta() 

我用來測試它在主要,並給出了下面的輸出代碼:

JPG test 
-> imgmeta[Exif.Image.DocumentName] : TITLE 
-> imgmeta[Exif.Image.ImageDescription] : 1429683541.3 
-> imgmeta[Iptc.Application2.Headline] : TITLE 
-> imgmeta[Iptc.Application2.Caption] : 1429683541.3 
{'description': '1429683541.3', 'title': 'TITLE'} 
-> imgmeta[Exif.Image.DocumentName] : TITLE 
-> imgmeta[Exif.Image.ImageDescription] : 1429683541.31 
-> imgmeta[Iptc.Application2.Headline] : TITLE 
-> imgmeta[Iptc.Application2.Caption] : 1429683541.31 
{'description': '', 'title': ''} 
-> imgmeta[Exif.Image.DocumentName] : TITLE 
-> imgmeta[Exif.Image.ImageDescription] : 1429683546.32 
-> imgmeta[Iptc.Application2.Headline] : TITLE 
-> imgmeta[Iptc.Application2.Caption] : 1429683546.32 
{'description': '', 'title': ''} 
PNG test 
{'description': '1429683546.32', 'title': 'TITLE'} 
{'description': '1429683546.83', 'title': 'TITLE'} 
{'description': '1429683551.83', 'title': 'TITLE'} 

正如你所看到的,在JPG測試,它工作正常在一個正常的文件,但在PIL調整圖像大小後根本不起作用。這不會發生.PNG,它使用PIL來保存元數據,這將使我建議它是pyexiv2,這是問題。

我該怎麼辦?任何的意見都將會有幫助。

謝謝。

+0

如果有幫助: >>> pyexiv2 .__ version__ '0.3.2' >>> PIL.VERSION '1.1.7' >>> PIL.PILLOW_VERSION '2.7.0' – Derwent

回答

0

的問題是與線

if index in imgmeta.iptc_keys:  
    imgmeta[index] = [value] 
if index in imgmeta.exif_keys: 
    imgmeta[index] = value 

他們應該

if index[:4] == 'Iptc' :   
    imgmeta[index] = [value] 
if index[:4] == 'Exif' : 
    imgmeta[index] = value 

由於密鑰不會在imgmeta鍵列表中找到,因爲沒有人會寫信給鍵之前。另一個案例是我花了幾個小時在錯誤的地方尋找問題。