Marco
Marco

Reputation: 1493

Is there a way to convert an image from grayscale to RGB in "pure" Keras

I'd like to know if there is a way to convert an image from grayscale to RGB in Python using "pure" Keras (i.e. without importing Tensorflow).

What I do now is:

x_rgb = tf.image.grayscale_to_rgb(x_grayscale)

Upvotes: 4

Views: 4140

Answers (1)

benjaminplanche
benjaminplanche

Reputation: 15139

Maybe you would consider this "cheating" (as keras.backend may end up calling Tensorflow behind the scene), but here's a solution:

from keras import backend as K

def grayscale_to_rgb(images, channel_axis=-1):
    images= K.expand_dims(images, axis=channel_axis)
    tiling = [1] * 4    # 4 dimensions: B, H, W, C
    tiling[channel_axis] *= 3
    images= K.tile(images, tiling)
    return images

(supposing your grayscale images have a shape B x H x W and not e.g. B x H x W x 1 ; otherwise just remove the first line of the function)

Upvotes: 2

Related Questions