def autocontrast(image):
    'Implements Autocontrast function from PIL using TF ops.'

    def scale_channel(channel):
        'Scale the 2D image using the autocontrast rule.'
        lo = tf.cast(tf.reduce_min(channel), tf.float32)
        hi = tf.cast(tf.reduce_max(channel), tf.float32)

        def scale_values(im):
            scale = (255.0 / (hi - lo))
            offset = ((- lo) * scale)
            im = ((tf.cast(im, tf.float32) * scale) + offset)
            return tf.saturate_cast(im, tf.uint8)
        result = tf.cond((hi > lo), (lambda : scale_values(channel)), (lambda : channel))
        return result
    s1 = scale_channel(image[:, :, 0])
    s2 = scale_channel(image[:, :, 1])
    s3 = scale_channel(image[:, :, 2])
    image = tf.stack([s1, s2, s3], 2)
    return image
Implements Autocontrast function from PIL using TF ops.
def autocontrast(image):
    

    def scale_channel(channel):
        'Scale the 2D image using the autocontrast rule.'
        lo = tf.cast(tf.reduce_min(channel), tf.float32)
        hi = tf.cast(tf.reduce_max(channel), tf.float32)

        def scale_values(im):
            scale = (255.0 / (hi - lo))
            offset = ((- lo) * scale)
            im = ((tf.cast(im, tf.float32) * scale) + offset)
            return tf.saturate_cast(im, tf.uint8)
        result = tf.cond((hi > lo), (lambda : scale_values(channel)), (lambda : channel))
        return result
    s1 = scale_channel(image[:, :, 0])
    s2 = scale_channel(image[:, :, 1])
    s3 = scale_channel(image[:, :, 2])
    image = tf.stack([s1, s2, s3], 2)
    return image