Django-media-treeを使用して、画像をサイトの画像ライブラリにインポートしています。PILでバグが発生し、画像上の不明なEXIFデータが、サムネイル画像の生成で未処理の例外を引き起こしています。PILをハッキングするのではなく、PILで処理する前に、画像からすべてのEXIFデータを削除することを検討しています。
chilkat.CkXmp()を使用して、イメージをクリーンな形式で新しいディレクトリに書き直そうとしていますが、RemoveAllEmbedded()メソッドはNoneを返し、イメージはEXIFデータをそのままにして書き直されます。
import os
import sys
import chilkat
ALLOWED_EXTENSIONS = ['.jpg', 'jpeg', '.png', '.gif', 'tiff']
def listdir_fullpath(d):
list = []
for f in os.listdir(d):
if len(f) > 3:
if f[-4:] in ALLOWED_EXTENSIONS:
list.append(os.path.join(d, f))
return list
def trim_xmp_data(file, dir):
xmp = chilkat.CkXmp()
success = xmp.UnlockComponent("Anything for 30-day trial.")
if (success != True):
print xmp.lastErrorText()
sys.exit()
success = xmp.LoadAppFile(file)
if (success != True):
print xmp.lastErrorText()
sys.exit()
print "Num embedded XMP docs: %d" % xmp.get_NumEmbedded()
xmp.RemoveAllEmbedded()
# Save the JPG.
fn = "%s/amended/%s" % (dir, file.rsplit('/')[-1])
success = xmp.SaveAppFile(fn)
if (success != True):
print xmp.lastErrorText()
sys.exit()
for item in listdir_fullpath('/Users/harrin2/Desktop/tmp/'):
trim_xmp_data(item, '/Users/harrin2/Desktop/tmp')
誰かが私がどこで間違っているのか教えてもらえますか、または画像をきれいにするより良い方法があるかどうか私は提案を受け入れています.....
TIA