2017-03-02 2 views
0

면책 조항 : 프로그래밍에 익숙하지 않습니다. 이 기능을 하나의 출력이 2다른 함수에 함수의 반환 값 python

def img_to_color(path_file): 

# takes an image and calculates avg red/green/blue value 
color_avg = [red, green, blue] 
return color_avg   

def extract_RGB(path_folder): 

j = 0 
RGB = [] 
file_count = len(os.listdir(path_folder)) 
files = os.listdir(path_folder) 
while j < file_count: 
    img_to_color(path_folder + "/" + files[j]) 
    RGB.append(color_avg) 
    j = j+1 
X_training.append(trainingDataFolder(pfad, RGB)) 

그러나 RGB 내가 함수를 호출에도 불구하고 [] 유지 작동 img_to_color 기능을 작동하도록 전달되지 않습니다 보인다 다음 코드는 작동하지 않습니다.

+0

이 X_training''무엇입니까? 선언 된 곳은 어디입니까? 'red','green','blue'도 마찬가지입니다. –

+0

빨강, 초록, 파랑의 계산을 제외하고 더 짧게 만들었습니다. 이 함수는 정상적으로 작동합니다. X_training도 존재합니다 (글로벌로 선언되었습니다). –

답변

0

변수 color_avgimg_to_color 로컬 함수입니다. extract_RGB 안에 존재하지 않습니다. img_to_color는 값을 반환합니다, 그래서 당신은 좋아하는 그것에 변수를 할당 중 하나가 :

color_avg = img_to_color(...)

을 직접처럼 함수에 반환 값을 전달합니다

RGB.append(img_to_color(...)

+0

고맙습니다. 스펜서. 완벽하게 작동하고 마침내 어떻게 작동하는지 이해합니다. –

0

당신은 문제가 있다면 들여 쓰기와. 먼저 this을 읽어야합니다.

변수 X_training은 어디에도 선언되어 있지 않은 것으로 보이므로 더 많은 글로벌 컨텍스트에 포함되는지 확실하지 않습니다.

코드는 다음과 같이 기록한다 :

def img_to_color(path_file): 
    # takes an image and calculates avg red/green/blue value 
    color_avg = [red, green, blue] 
    return color_avg   

def extract_RGB(path_folder): 
    j = 0 
    RGB = [] 
    file_count = len(os.listdir(path_folder)) 
    files = os.listdir(path_folder) 
    while j < file_count: 
     color_avg = img_to_color(path_folder + "/" + files[j]) 
     RGB.append(color_avg) 
     j = j+1 
    X_training.append(trainingDataFolder(pfad, RGB))