What's an image made of?

There are many correct answers.

But the most fascinating one is: << sines & cosines >>

Read on if you're intrigued👇🧵🪡


Any image can be reconstructed from a series of sinusoidal gratings.

A sinusoidal grating looks like this…


Show thread

It’s called a sinusoidal grating because the grayscale values vary according to the sine function.

If you plot the values along a horizontal line of the grating, you’ll get a plot of a sine function


Show thread

…and different frequencies—these are spatial frequencies, not temporal ones


Show thread

There's one more parameter that defines a sinusoidal grating: the phase. Gratings with a different phase are shifted with respect to each other…


Or even better, you can use a function of both x and y to make any grating


Show thread

You can find the parameters of a sinusoidal grating by using the 2D .

The dots shown contain the amplitude and phase of the grating. Their position from the centre gives the frequency, and their orientation represents the orientation of the grating.


Show thread

Now, if you have lots of gratings superimposed on each other, the gives you a pair of dots for each of the components


Show thread

Now, here’s the “magical” part of theory.

Any image is made up of lots of sinusoidal gratings. So, the 2D Fourier Transform of an image gives you thousands of pairs of dots, and each pair represent a sinusoidal grating.


Show thread

And therefore, you can reconstruct the image by adding all of those sinusoidal gratings together.

The more gratings you add, the closer the result is to the actual image


Show thread

There's a lot more than can fit in a single thread.

If you want to read more detail, and go through the step-by-step writing of the code to decompose & recostruct *any* image, read full article here:



Show thread

So, this thread could serve as my my

I'm Stephen. I used to be a physicist (as you can guess from the thread above) but now I focus on communicating about Python and programming and teaching coding

You can expect more varied content from me, all related to in , from science-y stuff like this, to fun animations using the `turtle` module (no not those boring ones!) and general Python for those learning to code at beginner and intermediate levels

Show thread

You'll find me easily on Twitter too if you're on there as well. That's a bigger account and get's a lot of interesting conversations going with others in the world

You can see the sort of things I'm interested it there (until this timeline fills up a bit): twitter.com/s_gruppetta_ct

Show thread

Not sure whether you’re only meant to get a single shot at an , but I’ll extend on mine a bit now that I’m starting to find my feet here

My main focus is on communicating about , specifically . That includes teaching!

However, my first career was as a physicist

After studying and and then getting my PhD in Physics from Imperial College, London, I went down the normal route of a few postdocs, an academic fellowship, and then a lectureship.

Most of my science work centred around novel retinal imaging technology with the primary aim of early disease diagnosis, plus a bit more about the optics of the eye.

I learnt to code as part of my PhD work and then relied on programming (mostly MATLAB, at the time, before the Python-era) for all of my research work, from simulation and modelling, to running the lab experiments, to analysing the data.

When I left academia, I decided to focus on teaching programming to both children and adults.

I spend a lot of time creating learning content, including The Python Coding Book and regular articles on the blog.

I also run codetoday which runs live lessons for children from age 7 to learn coding in Python (only Python, no kids-platforms. Yes, starting from age 7, you’d be surprised how well they pick the basics up)

I’ll be sharing bits on content regularly here too, typically aimed at intermediate learners (including those aiming to move from beginners to intermediate), including steering towards scientific programming and related fields.

OK, I’ve abused ’s longer character limit too much, so I’d better stop!

Show thread

@s_gruppetta In my 35 years as a sotfware engineer I have seen a lot of code that looks like it was written by seven year olds. 😆

@s_gruppetta Would it be feasible to include the code in the toot or as alt text? I'd love to try pasting it into Python, but of course I can't do that from an image.

@peterdrake I have been doing this (on Twitter) for a few months now, but this is an old post (from Twitter, recycled here) so the images didn’t have them. I now autogenerate these snippets which makes adding the code in ALT text easy

@peterdrake and yes, the article linked later has the full code etc…

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.