def jpeg_linqual(quality):
    """
    See int jpeg_quality_scaling(int quality)
    in libjpeg-turbo source (file ijg/jcparam.c).
    Convert quality rating to percentage scaling factor
    used to scale the quantization table.
    """
    quality = max(quality, 1)
    quality = min(quality, 100)
    if quality < 50:
        quality = 5000 // quality
    else:
        quality = 200 - quality * 2
    return quality