I have a task where I need to extract image's pixel rgb or hex code value and store them in pandas dataframe in the same order as the pixel order in the image selected. anyone know if my project is even possible? even though it sounds so easy all I need is to store image's pixel values in excel file and additionally make vba code color fill each cell in the file with the rgb/hex code color
I used this code to get the rgb colors and it return a 2d array
img = Image.open(image_path)
# Get the size of the image
width, height = img.size
# Initialize an empty array to store RGB values
rgb_values = []
# Iterate through each pixel in the image
for y in range(height):
for x in range(width):
# Get the RGB values of the current pixel
pixel_rgb = img.getpixel((x, y))
# Append the RGB values to the array
rgb_values.append(pixel_rgb)
and the reason I used this is I think I can add dataframe code in between loop and it can give me the desired output what do you think because science this step I'm stock I don't know if this's even worth it
I believe you are asking how to save the RGB values in a pandas dataframe and then export them to an Excel file, in which case you are on the right track with your code. You can actually just convert your list to a dataframe after the for loop like this:
You can then use pandas to save the dataframe to an Excel file like this:
As a side note, when testing this on a sample image, I got an error that I had exceeded the maximum size for an Excel file. You may need to save your output to a CSV, or choose a smaller image, if you run into that as well.