The code below takes an opencv image, converts it to rgb, and steps through the pixels converting them to hex using a list comprehension, and counts the number of pixels there are of each colour.
How can I reduce this code using list comprehension, and solve the TypeError at the bottom ?
import cv2
bgr_img = cv2.imread(img_input)
rgb_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2RGB) # Hex starts with Red
The file is of the shape (300, 700, 3) that is [[[ 90, 150, 140 ], [90 150 140], [90 150 140] ....
palette = dict()
for i in img:
for j in i:
colour_hex = [f"#{a:02x}{b:02x}{c:02}" for a,b,c in j]
if colour_hex in palette:
palette[colour_hex] +=1
else:
palette[colour_hex] = 1
So then the dictionary contains value key pairs of the hex colour and how many of each pixel in the image is that hex colour.
The error message reads
TypeError: cannot unpack non-iterable numpy.uint8 object
Thanks to response below, shorter code that works is like this:-
import cv2
bgr_img = cv2.imread(img_input)
rgb_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2RGB) # Hex starts with Red
palette = dict()
for i in img:
for j in i:
colour_hex = f"#{:02x}{:02x}{:02x}".format(*j)
if colour_hex in palette:
palette[colour_hex] +=1
else:
palette[colour_hex] = 1
can this be made any shorter ? / more Pythonic using a list comprehension ?