Starting out with images #1 – Getting started

Image : Pixels of the image stored in a TXT file

Date(s) : 24-25 JAN

Time : 10:32-11:32 + 11:46-12:46 / 24-25 JAN to 12:50-01:25 / 25 JAN

In my previous post, I covered what I learned in the lecture 0 of CS50. In lecture 0, Dr. David J. Malan had talked about how images are stored in the form of Pixels & this really excited me, so I started learning more on how to capture images & store them using a library called as “OpenCV”.

In my first code review, I try to cover two python scripts, one, which captures an image from the 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 :

  1. Capture an image & store it in a file :
#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) 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 =
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
  print("The Frame has not been captured correctly")

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")
    print(nameOfFile+" not found.")

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 :

In my next study session, I would try to do more experiments with images such as generating random images & more. Bye for now.

One thought on “Starting out with images #1 – Getting started

Leave a Reply

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

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

Google photo

You are commenting using your Google 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