Large Language Models for Lossless Image Compression: Next-Pixel Prediction in Language Space is All You Need

Kecheng Chen, Pingping Zhang, Hui Liu, Jie Liu, Yibing Liu, Jiaxin Huang, Shiqi Wang, Hong Yan, Haoliang Li

Next-Pixel Prediction in Language Space is All You Need

TL;DR

This paper introduces $P^2$-LLM, a novel approach that leverages Large Language Models (LLMs) for state-of-the-art lossless image compression. By treating RGB subpixels as numerical text tokens and using prompt-based in-context learning, the model predicts the next pixel with high enough accuracy to allow an arithmetic coder to compress the data more efficiently than existing classical and learned codecs.

Compression performance comparison

The Challenge: Compression is Intelligence

In information theory, "compression is intelligence"—the better a model can predict the next piece of data, the more tightly that data can be compressed. Recently, researchers have tried using pre-trained LLMs as general-purpose compressors. However, naive applications of LLMs to image compression (especially standard RGB images) have historically fallen short of traditional codecs like PNG or JPEG-XL.

Previous LLM-based attempts struggled because they processed color channels independently, used awkward ASCII character mappings that destroyed the numerical relationships between pixel values, and didn't provide the LLM with enough context about the task. This paper aims to bridge the gap between an LLM's theoretical compression limits and its practical performance on standard RGB images.

How $P^2$-LLM Works

The authors propose $P^2$-LLM (Next-Pixel Prediction-based LLM), which reformulates image compression into a language modeling task using several key innovations:

Overview of the P^2-LLM architecture
  • Pixel Prediction Chat Template: Instead of feeding raw data to the model, the authors flatten RGB images into 1D sequences while keeping the R, G, and B values together to preserve inter-channel correlations. They then wrap this sequence in a natural language prompt (e.g., "Every three values denote an RGB pixel... Predict the next RGB pixel"), leveraging the LLM's in-context learning abilities.
  • Two-step Lossless Pixel Tokenization: Instead of mapping pixel values (0-255) to arbitrary ASCII characters, they map them directly to numerical string tokens in the LLM's dictionary (e.g., the number 127 becomes the text token "127"). This preserves the semantic and numerical proximity of the values.
  • Predictive Distribution Sampling: For compression, the system extracts the LLM's raw logits (predictions) for the 256 possible pixel values before the softmax layer. This creates a highly accurate probability distribution that is fed into a standard Arithmetic Coder to generate the final compressed bitstream.
  • LoRA Fine-tuning: The authors fine-tune a Llama 3 (8B) model using Low-Rank Adaptation (LoRA) on image patches to specialize its pre-trained reasoning for next-pixel prediction.

Key Insights

  • Numbers Matter in Tokenization: LLMs already "know" that the number 126 is close to 127. By tokenizing pixels as actual numbers rather than arbitrary characters, the LLM's pre-trained numerical reasoning directly translates into better spatial and color understanding.
  • Prompting Improves Compression: Framing the compression task as a chat completion actually improves the model's predictive accuracy. Telling the model what the numbers represent helps it apply the right internal logic, lowering the final bit rate.
  • Joint Channel Modeling is Crucial: Predicting Red, Green, and Blue sequentially—rather than treating them as completely separate grayscale images—allows the LLM to exploit inter-channel correlations, a fundamental prior in traditional image processing.

Experimental Results

The model was trained on the DIV2K high-resolution dataset and evaluated across both in-distribution (DIV2K validation, CLIC.p, CLIC.m, Kodak) and out-of-distribution (SCID, BRACS24) datasets.

  • State-of-the-Art Compression: $P^2$-LLM achieves the best lossless compression rates across the board, outperforming top classical codecs (JPEG-XL, PNG, FLIF) and learned image compression (LIC) models (DLPR, L3C).
  • Quantitative Gains: On the CLIC.m dataset, $P^2$-LLM achieved 2.08 bits-per-subpixel (bpsp), beating the best learned codec (DLPR at 2.16 bpsp) and the best classical codec (JPEG-XL at 2.36 bpsp).
  • Superior Generalization: The model showed massive improvements in out-of-distribution generalization. On the SCID screen content dataset, it achieved 1.21 bpsp compared to DLPR's 1.58 bpsp and JPEG-XL's 1.26 bpsp.

Limitations and Future Work

While the compression rates are groundbreaking, the approach has notable limitations. Because $P^2$-LLM relies on autoregressive next-token prediction using an 8-billion parameter model, decoding is computationally expensive and slow. For example, it takes ~273 seconds to decode an image on the Kodak dataset, compared to just 0.08 seconds for JPEG-XL.

Due to this extreme computational cost, the method is currently only practical for offline, bandwidth-constrained storage scenarios (like archiving massive scientific or astronomical datasets) where real-time decoding isn't required. Future work will explore more lightweight, quantized LLMs and faster inference strategies to make LLM-based compression practical for everyday, real-time use.

Summary updated

BibTeX
@inproceedings{chen2025large,
  author = {Kecheng Chen and Pingping Zhang and Hui Liu and Jie Liu and Yibing Liu and Jiaxin Huang and Shiqi Wang and Hong Yan and Haoliang Li},
  title = {{Large Language Models for Lossless Image Compression: Next-Pixel Prediction in Language Space is All You Need}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2025},
  url = {https://openreview.net/forum?id=FXBBy1caOX},
}
Figure