Removing EXIF ​​metadata in python

I am using Django-media-tree to import images into a site's image library. I find a bug in PIL where some unknown EXIF ​​data in the image throws an unhandled exception in the thumbnail generation. Rather than hacking the PIL, I just want to remove all EXIF ​​data from the image before processing the PIL.

Using chilkat.CkXmp () I'm trying to rewrite an image to a new directory in pure form, however the RemoveAllEmbedded () method returns None and the image is overwritten with unchanged EXIF ​​data.

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

      

Can anyone tell me where I am going wrong, or if there is a better way to clean up images, I am open to suggestions .....

TIA

+3


source to share





All Articles