What is the smallest valid jpeg file size (in bytes)

A 1x1 grey pixel in 125 bytes using arithmetic coding, still in the JPEG standard even if most decoders can't decode it:

ff d8 : SOI
ff e0 ; APP0
 00 10
 4a 46 49 46 00 01 01 01 00 48 00 48 00 00
ff db ; DQT
 00 43
 00
 03 02 02 02 02 02 03 02
 02 02 03 03 03 03 04 06
 04 04 04 04 04 08 06 06
 05 06 09 08 0a 0a 09 08
 09 09 0a 0c 0f 0c 0a 0b
 0e 0b 09 09 0d 11 0d 0e
 0f 10 10 11 10 0a 0c 12
 13 12 10 13 0f 10 10 10
ff c9 ; SOF
 00 0b
 08 00 01 00 01 01 01 11 00
ff cc ; DAC
 00 06 00 10 10 05
ff da ; SOS
 00 08
 01 01 00 00 3f 00 d2 cf 20
ff d9 ; EOI

I don't think the mentioned 134 byte example is standard, as it is missing an EOI. All decoders will handle this but the standard says it should end with one.

That file can be generated with:

#!/usr/bin/env bash
printf '\xff\xd8' # SOI
printf '\xff\xe0' # APP0
printf  '\x00\x10'
printf  '\x4a\x46\x49\x46\x00\x01\x01\x01\x00\x48\x00\x48\x00\x00'
printf '\xff\xdb' # DQT
printf  '\x00\x43'
printf  '\x00'
printf  '\x03\x02\x02\x02\x02\x02\x03\x02'
printf  '\x02\x02\x03\x03\x03\x03\x04\x06'
printf  '\x04\x04\x04\x04\x04\x08\x06\x06'
printf  '\x05\x06\x09\x08\x0a\x0a\x09\x08'
printf  '\x09\x09\x0a\x0c\x0f\x0c\x0a\x0b'
printf  '\x0e\x0b\x09\x09\x0d\x11\x0d\x0e'
printf  '\x0f\x10\x10\x11\x10\x0a\x0c\x12'
printf  '\x13\x12\x10\x13\x0f\x10\x10\x10'
printf '\xff\xc9' # SOF
printf  '\x00\x0b'
printf  '\x08\x00\x01\x00\x01\x01\x01\x11\x00'
printf '\xff\xcc' # DAC
printf  '\x00\x06\x00\x10\x10\x05'
printf '\xff\xda' # SOS
printf  '\x00\x08'
printf  '\x01\x01\x00\x00\x3f\x00\xd2\xcf\x20'
printf '\xff\xd9' # EOI

and opened fine with GNOME Image Viewer 3.38.0 and GIMP 2.10.18 on Ubuntu 20.10.

Here's an upload on Imgur. Note that Imgur process the file making it larger however if you download it to check, and as seen below, the width=100 image shows white on Chromium 87:


It occurs to me you could make a progressive jpeg with only the DC coefficients, that a single grey pixel could be encoded in 119 bytes. This reads just fine in a few programs I've tried it in (Photoshop, GNOME Image Viewer 3.38.0, GIMP 2.10.18, and others).

ff d8 : SOI
ff db ; DQT
 00 43
 00
 01 01 01 01 01 01 01 01
 01 01 01 01 01 01 01 01
 01 01 01 01 01 01 01 01
 01 01 01 01 01 01 01 01
 01 01 01 01 01 01 01 01
 01 01 01 01 01 01 01 01
 01 01 01 01 01 01 01 01
 01 01 01 01 01 01 01 01
ff c2 ; SOF
 00 0b
 08 00 01 00 01 01 01 11 00
ff c4 ; DHT
 00 14
 00
 01 00 00 00 00 00 00 00
 00 00 00 00 00 00 00 00
 03
ff da ; SOS
 00 08
 01 01 00 00 00 01 3F
ff d9 ; EOI

The main space savings is to only have one Huffman table. Although this is slightly smaller than the 125 byte arithmetic encoding given in another answer, the arithmetic encoding without the JFIF header would be smaller yet (107 bytes), so that should still be considered the smallest known.

The above file can be generated with:

#!/usr/bin/env bash
printf '\xff\xd8' # SOI
printf '\xff\xdb' # DQT
printf  '\x00\x43'
printf  '\x00'
printf  '\x01\x01\x01\x01\x01\x01\x01\x01'
printf  '\x01\x01\x01\x01\x01\x01\x01\x01'
printf  '\x01\x01\x01\x01\x01\x01\x01\x01'
printf  '\x01\x01\x01\x01\x01\x01\x01\x01'
printf  '\x01\x01\x01\x01\x01\x01\x01\x01'
printf  '\x01\x01\x01\x01\x01\x01\x01\x01'
printf  '\x01\x01\x01\x01\x01\x01\x01\x01'
printf  '\x01\x01\x01\x01\x01\x01\x01\x01'
printf '\xff\xc2' # SOF
printf  '\x00\x0b'
printf  '\x08\x00\x01\x00\x01\x01\x01\x11\x00'
printf '\xff\xc4' # DHT
printf  '\x00\x14'
printf  '\x00'
printf  '\x01\x00\x00\x00\x00\x00\x00\x00'
printf  '\x00\x00\x00\x00\x00\x00\x00\x00'
printf  '\x03'
printf '\xff\xda' # SOS
printf  '\x00\x08'
printf  '\x01\x01\x00\x00\x00\x01\x3F'
printf '\xff\xd9' # EOI

Tags:

Image

Jpeg