Search code examples
pythoncolorstensorflowgrayscalemnist

How can I invert BLACK and WHITE (or grayscale) images in tensorflow? (black <->white change)


I want to change some pictures which is consist of black and white elements only (MNIST).

I can easily convert black to white, white to black by these code in python element

def inverseImageBW_array(originalImage):
    temp = 1 - originalImage
    temp = -1.* originalImage
    return temp

however, I cannot do this when I am treating tensorflow code. I tried to do this by

tf.multiply(1- originalImage, -1)

buy it does not work.

I know we can easily change brightness or contrast by tf.image functions but I think there is no functions which support black and white invert.

Please help me.


Solution

  • Not sure why something does not work for you. If your image is a 2d tensor or 3d tensor with a batch, then broadcasting should take care of everything:

    import tensorflow as tf
    
    a = tf.constant([[0.5, 0.3], [0.2, 0.8]])
    b = 1 - a
    with tf.Session() as sess:
        print sess.run(b)