In order to avoid duplicated code, it would be best to define the ensure_Image function just once, and then import it where you need it. pylearn2/utils/image.py might be a good place to put it.