@register.filter
def sorl_margin(file_, geometry_string):
    if not file_ or settings.THUMBNAIL_DUMMY:
        return 'auto'
    margin = [0, 0, 0, 0]
    image_file = default.kvstore.get_or_set(ImageFile(file_))
    x, y = parse_geometry(geometry_string, image_file.ratio)
    ex = x - image_file.x
    margin[3] = ex / 2
    margin[1] = ex / 2
    if ex % 2:
        margin[1] += 1
    ey = y - image_file.y
    margin[0] = ey / 2
    margin[2] = ey / 2
    if ey % 2:
        margin[2] += 1
    return ' '.join([ '%spx' % n for n in margin ])
This is copied from sorl/thumbnail/templatetags/thumbnail.py because of
dataset/ETHPy150Open fusionbox/django-widgy/widgy/contrib/page_builder/templatetags/thumbnail_libs.py/sorl_margin