3

I've wrote code that goes through every pixel of a low-res image and stores the Hue, Saturation and Brightness levels (HSL) of each pixel in an array. Now, I would like to go through each of these HSL levels to determine what colour it is closest to and store this information into a new array. I have written this code:

arrayCol = np.zeros((sHeight,sWidth), dtype = str)
for y in range(sHeight):
    for x in range(sWidth):
        #for n in range(3):
        if arrayHSL[y][x][2] <= 10:
            if arrayHSL[y][x][2] <= 10:
                arrayCol[y][x] = 'blk'

print(arrayCol)

which assigns the pixel as 'blk' or 'black' if the lightness is below a certain threshold. However, upon printing and viewing the array, rather than showing 'blk' in the correct place, it only shows the first letter 'b' as seen in the output:

[['' '' '' '' '' 'b' 'b' 'b' 'b' 'b' 'b' 'b' 'b' 'b' '' 'b' '' '' '' '']
 ['' '' '' '' '' '' 'b' 'b' 'b' 'b' '' 'b' 'b' '' '' '' '' '' '' '']
 ['b' '' '' '' '' '' '' '' 'b' 'b' 'b' 'b' 'b' '' '' '' '' '' '' '']
 ['' '' '' '' '' '' '' '' '' 'b' 'b' 'b' '' '' '' '' '' '' '' '']
 ['' '' '' '' '' '' '' '' '' '' 'b' '' '' '' '' '' '' '' '' '']
 ['' '' '' '' '' 'b' '' '' '' '' '' 'b' '' '' '' '' '' '' '' '']
 ['' 'b' '' 'b' '' '' '' '' '' '' 'b' 'b' '' '' '' '' '' '' '' '']
 ['' '' '' 'b' '' 'b' '' '' '' '' '' '' '' 'b' '' '' '' '' '' '']
 ['' '' 'b' '' '' 'b' 'b' 'b' '' '' '' '' '' 'b' '' '' '' '' '' '']
 ['' '' 'b' 'b' '' 'b' '' 'b' '' 'b' 'b' '' 'b' 'b' '' '' '' '' '' '']
 ['b' '' 'b' '' 'b' '' '' '' 'b' 'b' '' 'b' 'b' 'b' '' '' '' 'b' '' '']
 ['b' 'b' 'b' 'b' 'b' 'b' 'b' '' '' '' '' 'b' '' 'b' '' '' '' '' '' '']

Changing the name 'blk' to something else, such as 'rlk' does the same, only showing just an 'r' instead. This would not be much of a problem but once I start implementing other colours such as blue or brown neither I nor Python will be able to differentiate them. So my question is how can I get the array to store the full names of the colours rather than just the first letter? Thanks

0

2 Answers 2

2

If you type arrayCol.dtype it should print dtype('S1') meaning the strings of length 1. You can specify dtype='s256' for fixed length or dtype=object for abitrary length strings.

arrayCol = np.zeros((sHeight,sWidth), dtype = object)

Sign up to request clarification or add additional context in comments.

1 Comment

Great this does the trick. dtype=object is especially helpful because now I can store 'darkBlue' etc. Thanks!
0

There is a similar question answered here: Weird behaviour initializing a numpy array of string data

You have to replace dtype=str with dtype='S[number of characters]', which for 'blk' would be dtype='S3'

As per ArmorK's answer, you can also use dtype=object for arbitrary length strings.

1 Comment

Ah, I didn't find this. Thanks for pointing it out, it gives me a bit more background information about some of the functions.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.