def im_from_file(f): a = numpy.asarray(bytearray(f.read()), dtype=numpy.uint8) return cv2.imdecode(a, cv2.CV_LOAD_IMAGE_GRAYSCALE)