1. dobrych
  2. mediatr

Source

mediatr / filters / exif.py

"""
Filter for jpg images only. Export some EXIF fields as meta fields for file item.
"""
import EXIF

def enabled():
    """
    Various cheks goes here (settings, imports, etc)
    """
    return True

def filter_obj(object, params=None):
    """
    Main filter body. Object is instance of mediatr.models.Item, params is a filter specific tunings.
    """
    tags = EXIF.process_file(object.file)
    if not tags:
        return
    fnum = str(tags.get('EXIF FNumber')).split('/')
    # calculate aperture
    if len(fnum) == 2:
        aperture = "f/%.1f" % (float(fnum[0])/float(fnum[1]))
    elif fnum[0] != 'None':
        aperture = "f/%s" % (fnum[0])
    else:
        aperture = None
    exposure = str(tags.get('EXIF ExposureTime'))
    focal = tags.get('EXIF FocalLength')
    if focal: # prettify focal length value
        focal = "%smm" % (str(focal))
    # trying to update exiting aperture value
    try:
        ma = object.meta_set.get(field='Aperture')
        if not aperture:
            ma.delete()
        else:
            ma.value=aperture
            ma.save()
    # add aperture value if empty
    except object.meta_set.model.DoesNotExist:
        if aperture:
            object.meta_set.create(field='Aperture', value=aperture)
    # trying to update exposure value
    try:
        me = object.meta_set.get(field='Exposure')
        if not exposure:
            me.delete()
        else:
            me.value=exposure
            me.save()
    # add exposure value if empty
    except object.meta_set.model.DoesNotExist:
        if exposure:
            object.meta_set.create(field='Exposure', value=exposure)
    # trying to update focal length value
    try:
        mf = object.meta_set.get(field='Focal Length')
        if not focal:
            mf.delete()
        else:
            mf.value=focal
            mf.save()
    # add focal length value if empty
    except object.meta_set.model.DoesNotExist:
        if focal:
            object.meta_set.create(field='Focal Length', value=focal)