def determine_scale(scale, img, mark):
    """
    Scales an image using a specified ratio, 'F' or 'R'. If `scale` is
    'F', the image is scaled to be as big as possible to fit in `img`
    without falling off the edges.  If `scale` is 'R', the watermark
    resizes to a percentage of minimum size of source image.  Returns
    the scaled `mark`.

    """
    if scale:
        try:
            scale = float(scale)
        except (ValueError, TypeError):
            pass

        if isinstance(scale, six.string_types) and scale.upper() == 'F':
            # scale watermark to full, but preserve the aspect ratio
            scale = min(
                float(img.size[0]) / mark.size[0],
                float(img.size[1]) / mark.size[1]
            )
        elif isinstance(scale, six.string_types) and scale.upper() == 'R':
            # scale watermark to % of source image and preserve the aspect ratio
            scale = min(
                float(img.size[0]) / mark.size[0],
                float(img.size[1]) / mark.size[1]
            ) / 100 * settings.WATERMARK_PERCENTAGE
        elif type(scale) not in (float, int):
            raise ValueError('Invalid scale value "%s"! Valid values are "F" '
                             'for ratio-preserving scaling, "R%%" for percantage aspect '
                             'ratio of source image and floating-point numbers and '
                             'integers greater than 0.' % scale)

        # determine the new width and height
        w = int(mark.size[0] * float(scale))
        h = int(mark.size[1] * float(scale))

        # apply the new width and height, and return the new `mark`
        return (w, h)
    else:
        return mark.size
Scales an image using a specified ratio, 'F' or 'R'. If `scale` is
    'F', the image is scaled to be as big as possible to fit in `img`
    without falling off the edges.  If `scale` is 'R', the watermark
    resizes to a percentage of minimum size of source image.  Returns
    the scaled `mark`.