Skip to main content

How to Read a JPEG Image into a Tensor in TensorFlow with decode_jpeg

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

A JPEG file is compressed bytes, not pixels. To work on a photo, TensorFlow first reads the file and then decodes it into a tensor with one number per colour of every pixel. This guide does both with tf.io.read_file and tf.io.decode_jpeg in TensorFlow 2.22.0, on a painting from TensorFlow's own example images.

Syntax​

Syntax
tf.io.decode_jpeg(<bytes>, channels=<number>)
PartWhat it means
<bytes>The JPEG file's contents, as a string tensor, usually from tf.io.read_file
channels=<number>How many colour values per pixel: 3 for red, green and blue, 1 for gray, 0 (the default) for whatever the file has

The tf.io.decode_jpeg API page says it will "Decode a JPEG-encoded image to a uint8 tensor", and lists the channels values: "0: Use the number of channels in the JPEG-encoded image. 1: output a grayscale image. 3: output an RGB image."

The Example​

TensorFlow's example images include Composition VII (1913), a painting by Wassily Kandinsky that is in the public domain. The script reads it and prints the tensor's shape and type, and the colour of its top-left pixel.

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: Download the Image​

In the project folder, download the painting as composition.jpg:

Terminal
curl -o composition.jpg https://storage.googleapis.com/download.tensorflow.org/example_images/Vassily_Kandinsky%2C_1913_-_Composition_7.jpg
Output
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 190k 100 190k 0 0 145k 0 0:00:01 0:00:01 --:--:-- 145k

The terminal after the curl download: 190k received in about a second.

curl -o composition.jpg saves what it downloads under that name, and its table shows the progress: 190 KB in about a second. In PowerShell, Invoke-WebRequest -OutFile does the same and prints nothing when it's done.

Step 2: Write the Script​

read_image.py
import tensorflow as tf

data = tf.io.read_file("composition.jpg")
image = tf.io.decode_jpeg(data, channels=3)

print(image.shape, image.dtype)
print("Top-left pixel:", image[0, 0].numpy())

The read_image.py script in a macOS-style code window: tf.io.read_file reads composition.jpg, tf.io.decode_jpeg decodes it with channels=3, then prints the image's shape and dtype and its top-left pixel.

tf.io.read_file reads the file's bytes into a string tensor, and tf.io.decode_jpeg decodes them into pixels. image[0, 0] picks the pixel in row 0 and column 0, the top-left corner.

Step 3: Run It​

Terminal
uv run python read_image.py
Output
(657, 1000, 3) <dtype: 'uint8'>
Top-left pixel: [134 69 51]

The terminal after uv run python read_image.py: shape (657, 1000, 3) with dtype uint8, and the top-left pixel 134 69 51.

The shape (657, 1000, 3) is height, width and colours: the painting is 657 pixels high and 1000 wide, with 3 values per pixel. The type is uint8, whole numbers from 0 to 255. The top-left pixel is [134 69 51]: a lot of red, less green and little blue: a reddish brown. The command works unchanged in PowerShell.

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

Here is the picture those numbers describe:

Composition VII, a 1913 abstract painting by Wassily Kandinsky: a dense swirl of colours, lines and shapes in reds, blues, yellows and blacks, shown as the 1000 by 657 pixel JPEG that the tensor was decoded from.

Composition VII by Wassily Kandinsky (1913), public domain, from TensorFlow's example images.

Common Mistakes​

MistakeWhat happensDo this instead
Decoding a file that isn't an imageFor pyproject.toml: InvalidArgumentError: … Unknown image file format. One of JPEG, JPEG XL, PNG, GIF, BMP, WebP required. [Op:DecodeJpeg]Pass only image files; check the path
Leaving out channels=3With the default 0, a JPEG keeps the channels it has: a gray 64 × 32 JPEG decoded to (32, 64, 1), while colour photos decode to 3 channelsAsk for channels=3: the same gray JPEG decoded to (32, 64, 3)

FAQ​

Does decode_jpeg read PNG files?​

The API page says: "This op also supports decoding PNGs and non-animated GIFs since the interface is the same, though it is cleaner to use tf.io.decode_image." For PNG files, use tf.io.decode_png, or tf.io.decode_image for any of the formats.

Why are the values whole numbers from 0 to 255?​

That's how JPEG stores colour: one byte per colour per pixel. Models usually work on decimals, so the next step is often tf.cast to float32.

Conclusion​

tf.io.read_file read composition.jpg's bytes, and tf.io.decode_jpeg turned them into a (657, 1000, 3) tensor of uint8 values: 657 rows of 1000 pixels, each with a red, a green and a blue value.

Additional Resources​