Home > Software engineering >  cannot copy sequence with size 2 to array axis with dimension 3 (numpy)
cannot copy sequence with size 2 to array axis with dimension 3 (numpy)

Time:06-18

I'm having this error when going through an image with OpenCV and NumPy. Here's my code:

import math
import cv2
import numpy as np

palletes = [[255,85,85],[80,250,123],[255,184,108],[40,42,54],[68,71,90],[248,248,242],[98,114,164]]
def fcp(pixel, palletes): #Prototype for now
    ans = math.inf #Just a filler
    pal = [0,0,0] # Yet another filler
    for pallete in palletes:
        #Pythagorean Theorem (but it's 3D)
        result = math.sqrt(sum(list(map(lambda x,y: (x-y)**2, pixel, pallete))))
        if result < ans: ans = result; pal = pallete
    return pal, ans

# read the wallpaper.png image with opencv
img = cv2.imread('wallpaper.png')
# convert the image to rgb lists for every pixel
img_rgb = img.reshape((img.shape[0]*img.shape[1],3))
for p, pixel in enumerate(img_rgb):
    img_rgb[p] = fcp(list(pixel), palletes)
# undo the reshape
img_rgb = img_rgb.reshape(img.shape[0],img.shape[1],3)
# save the image to wallpaper_new.png
cv2.imwrite('wallpaper_new.png', img_rgb)

I get the error when trying to perform the img_rgb[p] = fcp(list(pixel),palletes).

CodePudding user response:

Never mind, the answer was quite simple - the fcp function returns two values, even though only the first one was expected. Python needs tracebacks.

  • Related