Skip to main content

How to Convert an Image to Grayscale in TensorFlow with rgb_to_grayscale

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A colour pixel has three values, red, green and blue; a gray one has one. tf.image.rgb_to_grayscale turns the three into one by weighing them, and the weights aren't equal. This guide converts four coloured pixels in TensorFlow 2.22.0, and shows how much each colour counts.

Syntax​

Syntax
tf.image.rgb_to_grayscale(<images>)
PartWhat it means
<images>One image or a batch of images whose last axis holds 3 values per pixel: red, green and blue
ResultThe same shape, except that the last axis has 1 value: the pixel's gray level. The type stays the same

The tf.image.rgb_to_grayscale API page: it "Converts one or more images from RGB to Grayscale", and "Outputs a tensor of the same DType and rank as images. The size of the last dimension of the output is 1, containing the Grayscale value of the pixels." The input's "last dimension must have size 3 and should contain RGB values."

The Example​

A picture 1 pixel high and 4 pixels wide: pure red, pure green, pure blue and white, each at full strength, 255.

Prerequisites​

  • A uv project with TensorFlow. This guide uses TensorFlow 2.22.0 on Python 3.14.10, with uv 0.13.10.

Step 1: Write the Script​

gray_pixels.py
import tensorflow as tf

# A picture 1 pixel high and 4 pixels wide: red, green, blue and white
pixels = tf.constant([[[255, 0, 0], [0, 255, 0], [0, 0, 255], [255, 255, 255]]], dtype=tf.uint8)

gray = tf.image.rgb_to_grayscale(pixels)
print(pixels.shape, "->", gray.shape)
print(gray)

The gray_pixels.py script in a macOS-style code window: a uint8 picture of one row with a red, a green, a blue and a white pixel, converted with tf.image.rgb_to_grayscale, then the two shapes and the gray tensor printed.

The picture has the shape (1, 4, 3): 1 row, 4 pixels, 3 colour values each. It's uint8, the type photos load as.

Step 2: Run It​

Terminal
uv run python gray_pixels.py
Output
(1, 4, 3) -> (1, 4, 1)
tf.Tensor(
[[[ 76]
[149]
[ 29]
[255]]], shape=(1, 4, 1), dtype=uint8)

The terminal after uv run python gray_pixels.py: the shape (1, 4, 3) became (1, 4, 1), and the gray values are 76 for red, 149 for green, 29 for blue and 255 for white, as uint8.

The last axis went from 3 values to 1, and the type stayed uint8. The gray levels show the weights: green became 149, almost twice red's 76, and blue only 29. White, all three at full strength, stayed 255. The command works unchanged in PowerShell.

The weights are in TensorFlow's source code, in image_ops_impl.py: rgb_weights = [0.2989, 0.5870, 0.1140], with a comment that points to the "luma" formula used for video. The function turns the pixels into decimals between 0 and 1, adds up red × 0.2989 + green × 0.5870 + blue × 0.1140, and converts the result back to uint8 by multiplying by 255.5 and dropping the fraction. For pure green, 0.5870 × 255.5 = 149.98, which became 149.

The first run in a new environment is slow, while TensorFlow loads for the first time.

Common Mistakes​

MistakeWhat happensDo this instead
Converting an image that is already grayFor a (1, 2, 1) image: InvalidArgumentError: … Matrix size-incompatible: In[0]: [2,1], In[1]: [3,1] [Op:MatMul]. The function multiplies the last axis by its 3 weights, and a gray image has only 1 value thereCheck the last axis first: image.shape[-1] is 3 for RGB and 1 for gray

FAQ​

How do I turn a gray image back into three channels?​

tf.image.grayscale_to_rgb copies the gray value into red, green and blue: gray pixels 76 and 149 became [76, 76, 76] and [149, 149, 149]. The image has 3 channels again, and still looks gray.

Can I convert a whole batch of photos at once?​

Yes. The function works on the last axis only: a batch of 2 images of shape (2, 5, 6, 3) became (2, 5, 6, 1).

Conclusion​

tf.image.rgb_to_grayscale turned red, green, blue and white into 76, 149, 29 and 255: a weighted sum with green counting most, 0.5870, then red, 0.2989, and blue, 0.1140. The shape kept everything but the last axis, which went from 3 to 1.

Additional Resources​