
Understanding and writing a JPEG decoder in Python - sfpoet
https://yasoob.me/posts/understanding-and-writing-jpeg-decoder-in-python/
======
yasoob
Hi everyone! OP here.

Why write another article on JPEG when there are already hundreds of articles
on the internet? Well, normally when you read articles on JPEG, the author
just gives you details about what the format looks like. You don’t implement
any code to do the actual decompression and decoding. Even if you do write
code, it is in C/C++ and not accessible to a wide group of people. I tried to
change that through this article. I give you a guided tour of JPEG
encoding/decoding process and show you how it can be implemented in Python3.

I mainly focus on decoding baseline encoded JPEG images.

~~~
userbinator
It's great to see more people complete the "JPEG decoder challenge" \--- and
document it too. Have you read the official T.81 spec[1]? It is one of the
easier standards to read, and it has flowcharts of all the algorithms which
mean you can implement without really understanding the theory.

As you mentioned there are lots of other articles about writing JPEG decoders,
as well as for the other popular image formats GIF and PNG. What I haven't
seen much of if at all, however, are articles about _video_ decoding; if
you're interested to try another challenge, I recommend a decoder for H.261:
[https://www.itu.int/rec/T-REC-H.261-199303-I/en](https://www.itu.int/rec/T-REC-H.261-199303-I/en)

(I've written one --- in C --- and it is actually far simpler than JPEG in
many ways, although it shares many similarities. A H.261 decoder in Python,
with associated article, would definitely be very interesting to see, and
AFAIK also a world-first!)

[1]
[https://www.w3.org/Graphics/JPEG/itu-t81.pdf](https://www.w3.org/Graphics/JPEG/itu-t81.pdf)

~~~
yasoob
Thanks for the H.261 decoder suggestion! I will actually seriously consider it
because I am personally curious how video decoders work as well. I will make
sure to document the process if I end up working on it.

~~~
jart
If you understand JPEG then you already understand the important bits of MPEG.
It's also fun to study scaling and colorspaces. See
[https://justine.storage.googleapis.com/printimage.html](https://justine.storage.googleapis.com/printimage.html)
and
[https://justine.storage.googleapis.com/printvideo.html](https://justine.storage.googleapis.com/printvideo.html)

------
pratio
This is a fantastic writeup, love the attention to detail and thank you so
much for not posting it on medium.

------
greggman3
This is great!

I first learned the general ideas from this Computerphile video

[https://www.youtube.com/watch?v=n_uNPbdenRs](https://www.youtube.com/watch?v=n_uNPbdenRs)

While we're on it does anyone know of articles/videos that go into similar
detail for HEVC?

------
Ahmd72
Thanks for making this, a couple of months ago I was working on a jpeg project
and needed to understand how it works. I was searching wildly for a tutorial
with a python implementation, unfortunately never found one. I found this one
in C++ though, it goes a lot more in depth
[https://www.youtube.com/playlist?list=PLpsTn9TA_Q8VMDyOPrDKm...](https://www.youtube.com/playlist?list=PLpsTn9TA_Q8VMDyOPrDKmSJYt1DLgDZU4)

------
iAmAPencilYo
Thank you for this; lots of details, references and detailed diagrams!

One small note - the first image you mention that we'll need to reference is a
bit small to read. Zooming in on the page does not zoom it in due to format of
the page. I had to right click -> View image, in a separate tab in order to be
able to zoom in enough to read the righthand side of it. edit: this is on the
latest version of Firefox

------
jonny789
Thanks a lot for your article. I think,for me it will be a perfect start for
understanding JPEG decoders & making one too. :)

------
subins2000
This was a really good read to understand JPEG with the right videos and
images included. Thanks!

~~~
yasoob
Glad to hear that. :) IMO some content is just better suited as visualizations
in the form of videos and images.

------
vmilner
It always puzzles me that the quantisation matrices don't have symmetry along
the diagonal. I guess it suggests the distribution of vertical and horizontal
lines in typical pictures differs.

