How to perform image compression using run-length encoding (RLE)

Run-length encoding (RLE) is a lossless compression technique where the original data can be perfectly reconstructed from the compressed data. It’s suitable for scenarios where data integrity must be preserved.

It is a data compression technique that simplifies data by replacing repeating occurrences of a unique symbol with an occurrence of that symbol along with its frequency. This method efficiently reduces the data size, especially where long runs of the same symbol are prevalent. By identifying and substituting these runs with symbol-frequency pairs, RLE transforms the data into a more compact representation, making it easier to store and transmit while retaining the essential information.

How run-length encoding works
How run-length encoding works

RLE is most effective when there are long series of identical pixels. Let’s look at the example below on how the shortest code is generated for the row with the same color.

Image compression using RLE
Image compression using RLE

For the uncompressed image, the binary values represent each pixel value, i.e., 0 for black and 1 for white. In the compressed image representation, the code format is the frequency followed by the pixel value.

Code

Let’s look at how we can perform RLE in Python.

def run_length_encoding(image):
encoded_image = ""
for row in image:
current_pixel = row[0]
run_length = 1
for pixel in row[1:]:
if pixel == current_pixel:
run_length += 1
else:
encoded_image += f"{run_length}{current_pixel}"
current_pixel = pixel
run_length = 1
encoded_image += f"{run_length}{current_pixel}\n"
return encoded_image
# Example 5x5 binary image
image = [
[0, 0, 1, 1, 0],
[0, 1, 1, 0, 0],
[0, 0, 0, 1, 1],
[1, 1, 0, 0, 0],
[0, 0, 0, 1, 0]
]
encoded_image = run_length_encoding(image)
print(encoded_image)

The function rle_encode takes a binary image as input and iterates through each row of the image, calculating runs of consecutive pixels with the same value.

Pros and cons of using RLE

Let's explore some pros and cons of using RLE for image compression.

Benefits

Limitations

Simplicity

The encoding and decoding process is relatively easy to understand.

Inefficient for Complex Data

Compression is not efficient where there are no repetitions or existing patterns.

Efficiency

RLE is particularly efficient when applied to images with repeated consecutive pixel sequences.

Limited Compression Ratio

Compared to other compression techniques, the reduction in the image sizes is not always high.

Fast Encoding and Decoding

This is possible due to minimal computation.

Lack of Adaptability

RLE doesn’t adapt to the content of the image.

No Overheads

RLE doesn’t introduce any additional metadata or header information.


Large RLE Values

If very long sequences are present in the image, the large values can increase the size of the compression data.

In image compression, RLE provides simplicity and efficiency for images with repetitive patterns, but it’s less effective for more complex images. The choice of compression method depends on the image’s characteristics and the trade-offs between compression ratio and computational complexity.

Test your knowledge

Answer the following questions to test your knowledge on RLE.

1

Which type of image would likely benefit the least from run length encoding compression?

A)

A scanned black-and-white document with text and simple line art

B)

A color photograph of a scenic landscape with rich textures

C)

An icon set with various symbols and shapes in different colors

D)

A binary image representing a QR code

Question 1 of 30 attempted

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved