2017-11-13 3 views
-1
def arrayconv(a): 
    black = [0, 0, 0, 1] #RGBA value for black 
    white = [255, 255, 255, 1] #RGBA value for white 
    red = [255, 0, 0, 1] #RGBA value for red 
    b = np.zeros(shape=(10, 10), dtype=object) #creates an array of zeroes 
    for i in range(10): 
     for j in range(10): 
      if a[i][j] == 0: #Copies the values of a into b except in rgba. 
       b[i][j] = black 
      if a[i][j] == 1: 
       b[i][j] = white 
      if a[i][j] == 2: 
       b[i][j] = red 
    print(b) 
    return Image.fromarray(b, 'RGBA') #Makes a picture with PIL's fromarray(). 

나는 10 x 10 픽셀 미로를 해결하는 프로그램을 작성 중이다. 이 코드는 프로그램의 백엔드에서 왔으며 0, 1 및 2의 배열을 해당 rgba 값을 갖는 새로운 배열로 변환한다고 가정합니다. 그 부분은 잘 작동합니다.내 프로그램은 Python PIL 클래스를 사용하여 2D Array에서 RGBA로 이상한 색상을 출력하고 있습니다. 어떻게 수정해야합니까?

그러나이 사진을 인쇄 할 때 fromarray()을 사용하면 의도 한 것과 다른 색으로 보입니다. 이미지는 올바른 색상 (3 개) 및 올바른 배열로 인쇄되지만 파란색 음영 또는 녹색 음영으로 인쇄됩니다. 동일한 잘못된 색 구성표는 매번 인쇄되지 않습니다.

+0

'a' 매개 변수 나 그것을 생성하는 함수의 샘플 데이터를 제공 할 수 있습니까? 그들 중 일부가 0,1 또는 2가 아닌 경우'a'의 값을 검사해야합니다. –

답변

0

bdtype='uint8'의 3 차원 배열을 만들어야합니다.

이 코드는 나를 위해 작동 :

def arrayconv(a): 
    b = np.zeros(shape=(a.shape[0], a.shape[1], 4), dtype="uint8") #creates an array of zeroes 
    colormap = { 
     0: (0, 0, 0, 1), 
     1: (255, 255, 255, 1), 
     2: (255, 0, 0, 1), 
    } 
    for index_x, row in enumerate(a): 
     for index_y, pixel in enumerate(row): 
       b[index_x, index_y] = colormap[pixel] 
    return Image.fromarray(b, 'RGBA') #Makes a picture with PIL's fromarray(). 

#create test data 
data = [random.randint(0, 2) for i in range(100 * 100)] 
data = np.reshape(data, (100, 100)) 
img = arrayconv(data) 
img.show() 

코드가 잘 작동해야

b = np.zeros(shape=(10, 10), dtype=object) 

b = np.zeroes(shape=(10, 10, 4), dtype="uint8") 

에 만 변경하는 경우.

관련 문제