
In the first lecture of Harvard’s CS50 course [1], I came across the idea that digital images are basically a collection of “picture elements” or “pixels” and a pixel can be represented by integers, which itself is a combination of zeros and ones, called as “binary numbers”. Fascinated by the idea that images are basically a collection of integers, I wanted to experiment with digital images , but since, I didn’t knew much about digital image processing, so I started looking for resources [2] and stumbled across a computer vision library called “OpenCV”. Since, I knew a little bit of python, so I started working on it & the first thing, which I wanted to do was : (1) Capture an image using a webcam & (2) Store the pixels of the captured image in a text file.
Before jumping to code, let’s look at the mathematical fundamentals of images and digital images :
In my first code review, I try to cover two python scripts, one, which captures an image from my laptop’s webcam & stores it as an image file & another one, which converts an image into a TXT file which stores the [R, G, B] values of each pixel of the image :
#Program to capture an image & store it in a File
#Created by Asxyzp
import cv2
import time
#time.sleep(t) allows to freez a thread
noOfSec = 0
while noOfSec<=1:
noOfSec = int(input("Seconds to take photo stream after?\t"))
if noOfSec<=1:
print("Enter a number greater than 1\n")
for i in range(1,noOfSec):
print("PhotoStream to be taken in ",i," second(s).") #Self-timer before image is taken
time.sleep(1) #Will delay the execution by 1sec.
#VideoCapture class allows capturing of video from image sequences, videos or camera
#cv2.VideoCapture(index) returns an object
#index parameter of VideoCapture() allows to choose from primary/secondary camera
PhotoObj = cv2.VideoCapture(0)
#PhotoObj.read() will return a tuple w/ 2 values
#The 1st value of the tuple will be a boolean which will store whether the frame has been captured correcctly.
#The 2nd value of the tuple will be a numpy arrray which will store the frame.
PhotoTuple = PhotoObj.read()
if PhotoTuple[0]:
Frame = PhotoTuple[1] #Stores Image in a numpy array
nameOfFile = input("Name of file? ") #Accepts the name of the file in which image will be stored
nameOfFile = nameOfFile + ".jpeg" #Adding format in which the name will be stored
cv2.imwrite(nameOfFile,Frame) #For writing an image at a particular location
PhotoObj.release()
else:
print("The Frame has not been captured correctly")
PhotoObj.release()
2. Converting Image into array/list & storing it into a text file :
#Converting Image into array/list & storing it into a text file
#created by Asxyzp
import sys
import cv2
import os.path
nameOfFile = input("Name of file for array conversion (w/ file type)? ")
if os.path.isfile(nameOfFile): #To check whether the file is in the directory
Frame = cv2.imread(nameOfFile,cv2.IMREAD_COLOR) #Reading the photo frame & storing it into an array
ImgRow = len(Frame) #Stores the number of rows of pixels in the Image
ImgCol = len(Frame[0]) #Stores the number of columns of pixels in the Image
print("Number of rows of Pixel:\t",ImgRow)
print("Number of cols of Pixel:\t",ImgCol)
print("Image resolution:\t",ImgRow,"x",ImgCol)
print("Number of Pixels:\t",ImgRow*ImgCol)
#Writing Pixel's [R,G,B] array in the Image
ImgR = 0
ImgArrObj = open(nameOfFile+".txt",'a+') #Opening a file w/ open() & writing text into w/ 'w'
RowStr = '[' #String for storing the entire image's pixel lists as a string
while ImgR < ImgRow: #Traversing through the entire row of the image
ImgC = 0
ColStr = '[' #String for storing the columns of the image
while ImgC < ImgCol: #Traversing through the entire col of the image
PixStr = '['+str(Frame[ImgR][ImgC][0])+','+str(Frame[ImgR][ImgC][1])+','+str(Frame[ImgR][ImgC][2])+']' #Storing pixel's value
if ImgC < ImgCol -1:
ColStr = ColStr + PixStr + ',' #Adding Pixel + , after an intermediate pixel of a col
elif ImgC == ImgCol -1:
ColStr = ColStr + PixStr #Adding only Pixel, at the last value of the col
ImgC += 1
if ImgR < ImgRow -1:
ColStr+='],' #Adding ], at the end of col
elif ImgR == ImgRow -1:
ColStr+=']' #Adding ] at the end of last col
RowStr += ColStr #Adding col to row
ImgR += 1
RowStr+=']' #Ending row with ]
ImgArrObj.write(RowStr) #Writing Image array string into a text file
print("\n\nProcess completed.\nImage stored in ",nameOfFile,".txt")
else:
print(nameOfFile+" not found.")
sys.exit(1)
Output of program two converts the output of program one into a TXT file with [R,G,B] values of each pixel of the image :
Image 2 : Output of program two
In my next study session, I would try to do more experiments with images such as generating random images & more. Bye for now.
References :
[1] https://cs50.harvard.edu/college/2019/fall/
[2] https://www.studytonight.com/post/face-detection-introduction-to-opencv-using-python-part-1#
One thought on “Experiments with images #1 – Getting started”