def METHOD_NAME(filename, dimension=THUMBNAIL_WIDTH):
    """
        Generates a base64 encoding for a thumbnail
        Args:
            filename (str): thumbnail to generate encoding from (must be in storage already)
            dimension (int, optional): desired width of thumbnail. Defaults to 400.
        Returns base64 encoding of resized thumbnail
    """
    if filename.startswith("data:image"):
        return filename
    checksum, ext = os.path.splitext(filename.split("?")[0])
    outbuffer = BytesIO()
    # make sure the aspect ratio between width and height is 16:9
    thumbnail_size = [dimension, round(dimension / 1.77)]
    try:
        if not filename.startswith(settings.STATIC_ROOT):
            filename = generate_object_storage_name(checksum, filename)
            inbuffer = default_storage.open(filename, 'rb')
        else:
            inbuffer = open(filename, 'rb')
        if not inbuffer:
            raise AssertionError
        with Image.open(inbuffer) as image:
            image_format = image.format
            # Note: Image.thumbnail ensures that the image will fit in the
            # specified thumbnail size, but it retains the original image's
            # aspect ratio. So a square image will remain square rather
            # than being distorted to a 16:9 aspect ratio. This removes
            # the need to make any changes like cropping the image.
            image.thumbnail(thumbnail_size, Image.ANTIALIAS)
            image.save(outbuffer, image_format)
        return "data:image/{};base64,{}".format(ext[1:], base64.b64encode(outbuffer.getvalue()).decode('utf-8'))
    finally:
        # Try to close the inbuffer if it has been created
        try:
            inbuffer.close()
        except UnboundLocalError:
            pass
        outbuffer.close()