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/
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).
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.
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).
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).