Home > Software design >  How to convert uint8 in brackets to (r, g, b) value
How to convert uint8 in brackets to (r, g, b) value

Time:12-04

Hey guys i found my self stuck on this. I am making an obs spotify app that shows you album cover, name of the artist, and now i would like to add average color output but i need it in rgb.

this is the average code with this output: [112.62674316 103.23660889 98.91593262]

src_img = cv2.imread('icon.jpeg')
average_color_row = np.average(src_img, axis=0)
average_color = np.average(average_color_row, axis=0)
print(average_color)

d_img = np.ones((312,312,3), dtype=np.uint8)
d_img[:,:] = average_color

And i would like it to be converted to (122, 103, 98) yeah and im not also shure if thats right :)

Thanks for help

Tried converting with cv2 and of course searching the internet for alternatives

CodePudding user response:

It looks like the code you posted is using the NumPy library to calculate the average color of an image. The output you're seeing, [112.62674316 103.23660889 98.91593262], is an array of floating point values representing the average values of the red, green, and blue channels of the image, respectively.

To convert this array to a tuple of integers, you can use the NumPy round function to round each value to the nearest integer, and then use the astype function to convert the resulting array to a tuple of integers. Here's how you could do that:

# Calculate the average color of the image
src_img = cv2.imread('icon.jpeg')
average_color_row = np.average(src_img, axis=0)
average_color = np.average(average_color_row, axis=0)

# Round the average color values to the nearest integer
average_color = np.round(average_color)

# Convert the rounded values to a tuple of integers
average_color = average_color.astype(np.int)

# Print the resulting tuple
print(average_color)

This should give you the output you're looking for: (122, 103, 98).

  • Related