Turning Images Into Values (Python)

Function to turn data into a CSV of dictionary values

Open CV (https://opencv.org) is a library available to Python (and other languages) to assist with image based machine learning. I am looking to turn an image into pixel values and then turn a file of values back into an image.

I am going to be using XKCD #1696 (AI Research) as the image test for my project. It is available from: https://xkcd.com/1696/

XKCD #1696
XKCD #1696

Open Computer Vision (OpenCV) uses BGR values for pixels. This is the value of Blue (B), Green (G) and Red (R) of the pixels, and the value is between 0 and 255. Why does OpenCV use BGR and not RGB? It seems to be because BGR was a standard when the OpenCV library was being created (see: https://www.learnopencv.com/why-does-opencv-use-bgr-color-format/ ).

I used pip install opencv-python to install cv2 (OpenCV).

Function to turn data into a CSV of dictionary values
Function to turn data into a CSV of dictionary values

So far my Python code looks like this:

# geektechstuff Image CV
import cv2
import csv
def pic_data_strip_csv():
   img = cv2.imread(‘image.png’)
   img_height, img_width = img.shape[:2]
   for x in range (0, img_height,1):
      for y in range (0,img_width,1):
         pixel_value = img[x,y,:]
         with open(‘pic.csv’,mode=’a’) as pic_data:
            pic_data_writer = csv.writer(pic_data, delimiter=’,’, quotechar='”‘, quoting=csv.QUOTE_MINIMAL)
            write_pic_data = pic_data_writer.writerow([x,y,pixel_value])
   return()
def pic_data_strip_dict_csv():
   img = cv2.imread(‘image.png’)
   img_height, img_width = img.shape[:2]
   for x in range (0, img_height,1):
      for y in range (0,img_width,1):
         pixel_value = img[x,y,:]
         with open(‘pic_dict.csv’,mode=’a’,newline=”) as pic_data:
            field_names = [‘x’,’y’,’bgr’]
            pic_data_writer = csv.DictWriter(pic_data, fieldnames=field_names)
            write_pic_data = pic_data_writer.writerow({‘x’:x,’y’:y,’bgr’:pixel_value})
   return()
Please note: The screen grabs show the correct formatting as formatting is sometimes lost in my blog posts.
cv2.imread(”) opens the image file. If the image file is not in the same location as the Python program then the full file path should be used and not just the filename.
img_height, img_width = img.shape[:2] reads the size of the image file, which is then used to loop through the image pixel by pixel and gather the pixel values in the line pixel_value = img[x,y,:]. X, Y are the co-ordinate values and : transforms into the BGR value.
Function to turn image into CSV of data
Function to turn image into CSV of data
When run the functions create either a CSV containing lines of pixel values (X, Y co-ordinates and then BGR values) OR a CSV of pixel values from dictionary values (X, Y co-ordinates and then BGR values).
geektechstuff_cv_pics_to_data3
Breaking image down to pixel location (X,Y) and pixel value (B G R)

I originally wrote just the CSV function with each pixel being a line of data but after consideration added the dictionary CSV function as I started thinking about how to best extract the data from the CSV later on (a.k.a. part 2 of my project).

One thought on “Turning Images Into Values (Python)

Comments are closed.