函数名称作为另一个函数的输入?

jle*_*and 2 python

我是一个图像处理工程师,我使用Python作为原型语言.

大多数时候,当我得到数以千计的图像时,名为"imagen.jpg",n是增量.

所以我的程序的主要结构可能被视为:

def main_IP(imgRoot, stop_ncrement):
  name = update_the_increment(imgRoot, stop_increment)
  img = load_the_image(name)
  out_img = process_image(img)
  displays_images(img, out_img)
  return out_img
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,从一个应用程序到另一个应用程序,唯一的变化是process_image函数.有没有办法让process_image作为输入插入?

我会得到一个泛型函数,原型为:main_IP(imgRoot,stop_increment,process_image)

谢谢 !朱利安

Bil*_*nch 8

函数可以在python中传递,就像字符串或任何其他对象一样.

def processImage(...):
    pass

def main_IP(imgRoot, stop_ncrement, process_image):
    name = update_the_increment(imgRoot, stop_increment)
    img = load_the_image(name)
    out_img = process_image(img)
    displays_images(img, out_img)
    return out_img

main_IP('./', 100, processImage)
Run Code Online (Sandbox Code Playgroud)