N
Horizon Insight News

Cannot import name 'load_img' from 'keras.preprocessing.image'

Author

David Richardson

Updated on March 10, 2026

Why am I getting this issue? I can import image module from kera.preprocessing. But cannot import image_dataset_from_directory. My TensorFlow version is 2.9.1, so I am not dealing with an old version, e.g. ImportError: cannot import name 'image_dataset_from_directory' from 'tensorflow.keras.preprocessing' (unknown location)

# make a prediction for a new image. from keras.preprocessing.image import load_img from keras.preprocessing.image import img_to_array from keras.models import load_model # load and prepare the image def load_image(filename): # load the image img = load_img(filename, target_size=(224, 224)) # convert to array img = img_to_array(img) # reshape into a single sample with 3 channels img = img.reshape(1, 224, 224, 3) # center pixel data img = img.astype('float32') img = img - [123.68, 116.779, 103.939] return img # load an image and predict the class def run_example(): # load the image img = load_image('test.jpg') # load model model = load_model('final_model.h5') # predict the class result = model.predict(img) print(result[0]) # entry point, run the example run_example() 

error

from keras.preprocessing.image import load_img ImportError: cannot import name 'load_img' from 'keras.preprocessing.image' 
0

3 Answers

keras.preprocessing API is deprecated in Tensorflow 2.9.1. Please use tf.keras.utils instead, to import load_img as follows:

from tensorflow.keras.utils import load_img 

To load dataset from directories please use tensorflow.keras.utils.image_dataset_from_directory. For more details, please refer to this link.

1
from tensorflow.keras.preprocessing.image import load_img 
0

replace

 from keras.preprocessing.image import load_img 

with

 from keras_preprocessing.image import load_img 

ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJobmtsZ259dYCOnJinpp%2Bpeqq5z6iprWWelrqmecuomJ1lmaK0brLRqKRmo5WnrrR5z6ucqaqfmLK0v8innmahnZa0pg%3D%3D