[Python] Why does an IndexError occur when I try to use a variable that is being iterated to...

Discussão em 'Python' iniciado por Stack, Abril 14, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to create a program that processes a screenshot into a downsized red image with only three colours. I expected to get an image, however, I ran into this error.


    IndexError: invalid index to scalar variable.


    This is referring to this part of the script.

    for x in range(29):
    for y in range(112):
    if x and y == 0:
    continue
    duplicate = False
    for z in range(colourFrequency.shape[0]):
    if colourFrequency[z][2] <= image[x][y][2] >= colourFrequency[z][3]:
    duplicate = True
    colourFrequency[z][1] += 1
    break
    if duplicate == False:
    colourFrequency = np.append(colourFrequency, [image[x][y][2], 1, image[x][y][2], image[x][y][2]])


    Specifically, this line.

    if colourFrequency[z][2] <= image[x][y][2] >= colourFrequency[z][3]:


    I have already searched StackOverflow and found someone who had a similar error (How to fix IndexError: invalid index to scalar variable), however I wasn't able to implement the suggestions found on that page without breaking the code.

    Is there a way to eliminate the error without having to compromise other parts of the script?

    Here is the full script just in case. Lines 4 to 15 were tested and work correctly, however, I wasn't able to test the rest so there are probably still some issues.

    import cv2
    import numpy as np

    #Crop and resize image

    image = cv2.imread("sample.png",cv2.IMREAD_COLOR)
    image = image[526:961, 0:1680]
    image = cv2.resize(image, (112,29))

    #Filter the B and G values from the image

    for x in range(29):
    for y in range(112):
    image[x][y][0] = 0
    image[x][y][1] = 0

    #Create an array where information about each colour present in the image is stored

    colourFrequency = np.array([[image[0][0][2], 1, image[0][0][2], image[0][0][2]]])

    #Find and store each unique colour in the array

    for x in range(29):
    for y in range(112):
    if x and y == 0:
    continue
    duplicate = False
    for z in range(colourFrequency.shape[0]):
    if colourFrequency[z][2] <= image[x][y][2] >= colourFrequency[z][3]:
    duplicate = True
    colourFrequency[z][1] += 1
    break
    if duplicate == False:
    colourFrequency = np.append(colourFrequency, [image[x][y][2], 1, image[x][y][2], image[x][y][2]])

    #Sort the array according to how high the R value is

    counterRed = 0

    for x in range(256):
    for y in range(colourFrequency.shape[0]):
    if colourFrequency[y][0] == x:
    np.insert(colourFrequency, counterRed, colourFrequency[int(y)])
    np.delete(colourFrequency, y+1)
    counterRed += 1
    continue

    #Simplifies the colours into 3 distinct colours based on how close it is in the R value and how common it is

    counterPixel = 1

    while colourFrequency.shape[0] > 3:
    index = -1
    for x in range(colourFrequency.shape[0]):
    if colourFrequency[x][1] == counterPixel:
    index = counterPixel

    if index < 0:
    counterPixel += 1
    elif index == 0:
    colourFrequency[index + 1][0] = round(((colourFrequency[index + 1][0] * colourFrequency[index + 1][1])+(colourFrequency[index][0] * colourFrequency[index][1])) / (colourFrequency[index + 1][1] + colourFrequency[index][1]))
    colourFrequency[index + 1][1] = colourFrequency[index + 1][1] + colourFrequency[index][1]
    colourFrequency[index + 1][2] = colourFrequency[index][2]
    np.delete(colourFrequency, index)
    elif index == 255:
    colourFrequency[index - 1][0] = round(((colourFrequency[index - 1][0] * colourFrequency[index - 1][1])+(colourFrequency[index][0] * colourFrequency[index][1])) / (colourFrequency[index - 1][1] + colourFrequency[index][1]))
    colourFrequency[index - 1][1] = colourFrequency[index - 1][1] + colourFrequency[index][1]
    colourFrequency[index - 1][3] = colourFrequency[index][3]
    np.delete(colourFrequency, index)
    else:
    diffBefore = (colourFrequency[index][1] - colourFrequency[index - 1][1])/(colourFrequency[index][0] - colourFrequency[index - 1][0])
    diffAfter = (colourFrequency[index + 1][1] - colourFrequency[index][1])/(colourFrequency[index + 1][0] - colourFrequency[index][0])
    if diffBefore > diffAfter:
    colourFrequency[index - 1][0] = round(((colourFrequency[index - 1][0] * colourFrequency[index - 1][1])+(colourFrequency[index][0] * colourFrequency[index][1])) / (colourFrequency[index - 1][1] + colourFrequency[index][1]))
    colourFrequency[index - 1][1] = colourFrequency[index - 1][1] + colourFrequency[index][1]
    colourFrequency[index - 1][3] = colourFrequency[index][3]
    else:
    colourFrequency[index + 1][0] = round(((colourFrequency[index + 1][0] * colourFrequency[index + 1][1])+(colourFrequency[index][0] * colourFrequency[index][1])) / (colourFrequency[index + 1][1] + colourFrequency[index][1]))
    colourFrequency[index + 1][1] = colourFrequency[index + 1][1] + colourFrequency[index][1]
    colourFrequency[index + 1][2] = colourFrequency[index][2]
    np.delete(colourFrequency, index)

    #Substitutes each colour for its simplified equivalent

    for x in range(29):
    for y in range(112):
    for z in range(colourFrequency.shape[0]):
    if colourFrequency[z][2] <= image[x][y][2] <= colourFrequency[z][3]:
    image[x][y][2] = colourFrequency[z][0]
    break

    #Output an image based on the array

    cv2.imwrite('changedSample.png', image)

    Continue reading...

Compartilhe esta Página