Save image properties to CSV using Python
In this article, we are going to write python scripts to find the height, width, no. of channels in a given image file and save it into CSV format. Below is the implementation for the same using Python3. The prerequisite of this topic is that you have already installed NumPy and OpenCV.
- First, we will load the required libraries into the python file ( NumPy, OpenCV, etc.).
- Create an empty CSV file with the column name only if there is no existing CSV file( Height, Width, Channels, colors, etc.). If the file data.csv does not exist then a new file will be created by the else statement.
- Now we will use argparse() function to get the directory path of the images from the user in the command line.
- Find the color properties using CV2.
- We will be using image.shape function to find out the Height, Width, Channels of the image.
- Then we will calculate the average red, average blue, average green of the image
- Then we will write the outputs to the csv file using writerow() function.
Below is the implementation:
- Save this code-named main.py.
- Shift(Key) + Right Click and Click open PowerShell window here.
python3 main.py --image /path/to/images/folder/:
CSV file output: