Python conversion of PIL image to numpy array very slow Python conversion of PIL image to numpy array very slow numpy numpy

Python conversion of PIL image to numpy array very slow


You can just let numpy handle the conversion instead of reshaping yourself.

def pil_image_to_numpy_array(pil_image):    return np.asarray(pil_image)  

You are converting image into (height, width, channel) format. That is default conversion numpy.asarray function performs on PIL image so explicit reshaping should not be neccesary.


Thank you very much!! It works very fast!

def load_image_into_numpy_array(path):    """Load an image from file into a numpy array.    Puts image into numpy array to feed into tensorflow graph.    Note that by convention we put it into a numpy array with shape    (height, width, channels), where channels=3 for RGB.    Args:    path: a file path (this can be local or on colossus)    Returns:    uint8 numpy array with shape (img_height, img_width, 3)    """    img_data = tf.io.gfile.GFile(path, 'rb').read()    image = Image.open(BytesIO(img_data))    return np.array(image)

Image with (3684, 4912, 3) take 0.3~0.4 sec.