How to Save a Tensor as a JPEG Image in TensorFlow with encode_jpeg
A tensor of pixels lives only in memory. To look at it in an image viewer, or send it to someone, it has to become a file. tf.io.encode_jpeg compresses the pixels into JPEG bytes, and tf.io.write_file saves them. This guide builds a gray gradient out of numbers in TensorFlow 2.22.0 and saves it as gradient.jpg.
Syntax
tf.io.encode_jpeg(<image>, quality=<number>)
| Part | What it means |
|---|---|
<image> | A uint8 tensor of shape (height, width, channels), with 1 channel for gray or 3 for red, green and blue |
quality=<number> | Optional. From 0 to 100: higher looks better and makes a bigger file. The default is 95 |
| Result | A string tensor holding the JPEG file's bytes, ready for tf.io.write_file |
The tf.io.encode_jpeg API page says it will "JPEG-encode an image", where "image is a 3-D uint8 Tensor of shape [height, width, channels]". With no format given, it picks one from the channels: "1: Output a grayscale image. 3: Output an RGB image." And for quality: "Quality of the compression from 0 to 100 (higher is better and slower)."
The Example
A gradient 64 pixels wide and 32 high, going from black on the left to almost white on the right. Every row is the same 64 grays: 0, 4, 8 and so on up to 252.
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
import tensorflow as tf
# 32 rows of the same 64 grays, from black (0) to almost white (252)
gradient = tf.constant(list(range(0, 256, 4)) * 32, dtype=tf.uint8, shape=[32, 64, 1])
print(gradient.shape, gradient.dtype)
jpeg = tf.io.encode_jpeg(gradient)
print(jpeg.dtype, len(jpeg.numpy()), "bytes")
tf.io.write_file("gradient.jpg", jpeg)

list(range(0, 256, 4)) is the 64 grays of one row, and * 32 repeats them for 32 rows. shape=[32, 64, 1] arranges those 2,048 values into 32 rows of 64 pixels, each pixel with 1 value: a gray image. tf.io.encode_jpeg compresses it, and len(jpeg.numpy()) counts the bytes the JPEG takes. tf.io.write_file saves them as gradient.jpg.
Step 2: Run It
uv run python save_gradient.py
(32, 64, 1) <dtype: 'uint8'>
<dtype: 'string'> 598 bytes

The image was (32, 64, 1) and uint8, the shape and type tf.io.encode_jpeg expects. The JPEG came out as a string tensor of 598 bytes, and the project folder now has gradient.jpg. The command works unchanged in PowerShell.
The first run in a new environment is slow, while TensorFlow loads for the first time.
Here is gradient.jpg, at its real size and enlarged 8 times:

Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Encoding a float32 image | InvalidArgumentError: cannot compute EncodeJpeg as input #0(zero-based) was expected to be a uint8 tensor but is a float tensor [Op:EncodeJpeg] | For values from 0 to 255, tf.cast(image, tf.uint8). For values from 0 to 1, tf.image.convert_image_dtype(image, tf.uint8): it turned 0.0, 0.5 and 1.0 into [0 127 255], where tf.cast gave [0 0 1] |
An image without a channel axis, shape (32, 64) | InvalidArgumentError: … image must be 3-dimensional[32,64] [Op:EncodeJpeg] | Add the axis: image[:, :, tf.newaxis] has the shape (32, 64, 1) and encodes |
| An image with 4 channels, such as a PNG with transparency | InvalidArgumentError: … image must have 1 or 3 channels, got [32,64,4] [Op:EncodeJpeg]. It writes only gray and RGB images | Keep the first 3 channels: image[:, :, :3] |
FAQ
Is the saved JPEG exactly the same as the tensor?
No. JPEG compression drops some detail to make the file small: decoding gradient.jpg back gave values up to 2 away from the originals. For an exact copy, use tf.io.encode_png: the same gradient as a PNG took 92 bytes, and decoding it with tf.io.decode_png gave back every value unchanged.
How do I make the file smaller?
Lower the quality. The gradient took 598 bytes at the default 95, 404 bytes with quality=50 and 698 bytes with quality=100. Lower quality drops more detail.
Conclusion
tf.io.encode_jpeg turned a (32, 64, 1) uint8 tensor into 598 bytes of JPEG, and tf.io.write_file saved them as gradient.jpg. The image must be uint8, have 3 axes, and have 1 or 3 channels.
Additional Resources
- tf.io.encode_jpeg — every argument, including
progressiveandoptimize_size. - tf.io.encode_png — lossless images.
- tf.image.convert_image_dtype — converting between pixel types.
