Home > Learning, Lesson, numpy, plotting, Problem, Projects/Experiments, Python > Python: Random Pixel Color

Python: Random Pixel Color


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.]

Last week I start reading and studying about “Image Processing”, in some point I was reading about converting an image to Array of numbers, and re-converting that Array back to an Image, at that moment popped into my mind what if we just generate an Array with random numbers and then show it as an Image.

In this article we will write a first simple Function to generate a random numbers presenting the three main color Red, Blue and Green; then storing the numbers in an Array then using matplotlib library to display the Image.

Our Tools: In this post we will use the following:
1. Jupyter-NoteBook.
2. numpy.
3. random.
4. matplotlib.
5. PIL or Pillow.

Coding I am user Jupyter NoteBook on samsung Tab S4. First we will do all imports we need as:
from PIL import Image
import numpy as np, matplotlib.pyplot as plt
import random
%matplotlib inline

Now, we will write a Function called rand_color, we will run a nested for loop to generate the Row and Column (width and height) and in/for each row we will generate thee numbers of colors range(0,255) as one pixel and storing them in an array, then we display the Array using:
Image.fromarray and plt.imshow(). Here is the Code ..

ali radwani python code project jupyter
Run No.1
ali radwani python project color pixel random
Run No.2
ali radwani python project color pixel random
Run No.3
ali radwani python project color pixel random
Run No.4
ali radwani python project color pixel random



The above is just 25×25 image with random color pixels, this is the first function using default random, in coming posts we will use some Math variables [such: log,sin], constants [such: pi, golden ratio] to see if we can get a pattern from random.

..:: Have Fun with Coding ::.. 🙂

To Download my Python code (.py) files Click-Here



ali radwani ahradwani.com python projects codeFollow me on Twitter..

By: Ali Radwani

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s