Python: Random Pixel Color – P2
Learning : Python, Math
Subject: Random Coloring Pixels
[NOTE: To keep the code as simple as we can, We WILL NOT ADD any user input Varevecations. Assuming that our user will Enter the right inputs.]
Our last post about Random Pixel Color, we generate a Numpy Array of Row, Coloum and color then we Plot it on the screen [Read the Post Here], now in this post we will use some Math consepts to try if we can get some patterns out of ramdom Function.
Our Tools: In this post we will use the following:
1. Jupyter-NoteBook.
2. numpy.
3. random.
4. matplotlib.
5. PIL or Pillow.
In this version we will use “Fibonacci Sequence” Fibonacci Sequence is the sum of the two preceding ones, starting from 0 and 1 such as [1, 1, 2, 3, 5, 8, 13 … n], in our code we will have three variables:
cw: canvas width,
ch: canvas hight,
offset: the offset will be the value that will reset the Fibonacci Sequence to 1.
So, if we run the application, we will generate three numbers that will present the colors R,G,B (Will be Generated ONE time) then for each pixcel in (cw*ch) we will calculate a v as Fibonacci Sequence from fs1 =1, fs2 = 1 here is the code:
v = fs1 + fs2
fs1,fs2 = fs2, v
this value v will be added to the colors r,g,b (on each pixcel) untill the v is grater the the offset numbre that we pass to the Function. If v > offset then we will re-set the fs1 = 1, fs2 = 1,.. Here is the Code ..
![]() |
Run No.1![]() |
Run No.2![]() |
Run No.3![]() |
Run No.4![]() |
The above is just 25×25 and i change the offset, feel free to download the code and change the numbers .. see what you will get …
..:: Have Fun with Coding ::.. 🙂
To Download my Python code (.py) files Click-Here
By: Ali Radwani