title: ImportError: Could not import PIL.Image. The use of `array_to_img` requires PIL
body: <p>This is very tricky. I have almost tried all solutions on the internet but still can't solve this problem.</p>
  
  <p>If I simply type</p>
  
  <pre><code>from PIL import ImageFile
  print(Image.__file__)
  </code></pre>
  
  <p>Everything seems work fine and the system will print</p>
  
  <pre><code>C:\Users\BenWang\Anaconda3\envs\tensorflow\lib\site-packages\PIL\Image.py
  </code></pre>
  
  <p>Like this</p>
  
  <p>But in my coding, if I use the following code:</p>
  
  <pre><code>import sys
  from PIL import Image
  ImageFile.LOAD_TRUNCATED_IMAGES = True                 
  
  train_tensors = paths_to_tensor(train_files).astype('float32')/255
  valid_tensors = paths_to_tensor(valid_files).astype('float32')/255
  test_tensors = paths_to_tensor(test_files).astype('float32')/255
  </code></pre>
  
  <p>The error will appear something like this:</p>
  
  <pre><code>ImportError                               Traceback (most recent call last)
  &lt;ipython-input-166-10a043c54408&gt; in &lt;module&gt;()
        4 
        5 
  ----&gt; 6 train_tensors = paths_to_tensor(train_files).astype('float32')/255
        7 valid_tensors = paths_to_tensor(valid_files).astype('float32')/255
        8 test_tensors = paths_to_tensor(test_files).astype('float32')/255
  
  &lt;ipython-input-149-36af157e68bd&gt; in paths_to_tensor(img_paths)
       11 
       12 def paths_to_tensor(img_paths):
  ---&gt; 13     list_of_tensors = [path_to_tensor(img_path) for img_path in tqdm(img_paths)]
       14     return np.vstack(list_of_tensors)
  
  &lt;ipython-input-149-36af157e68bd&gt; in &lt;listcomp&gt;(.0)
       11 
       12 def paths_to_tensor(img_paths):
  ---&gt; 13     list_of_tensors = [path_to_tensor(img_path) for img_path in tqdm(img_paths)]
       14     return np.vstack(list_of_tensors)
  
  &lt;ipython-input-149-36af157e68bd&gt; in path_to_tensor(img_path)
        4 def path_to_tensor(img_path):
        5     
  ----&gt; 6     img = image.load_img(img_path, target_size=(224, 224))
        7     
        8     x = image.img_to_array(img)
  
  ~\Anaconda3\envs\tensorflow\lib\site-packages\keras_preprocessing\image\utils.py in load_img(path, grayscale, color_mode, target_size, interpolation)
      100         color_mode = 'grayscale'
      101     if pil_image is None:
  --&gt; 102         raise ImportError('Could not import PIL.Image. '
      103                           'The use of `array_to_img` requires PIL.')
      104     img = pil_image.open(path)
  
  ImportError: Could not import PIL.Image. The use of `array_to_img` requires PIL.
  </code></pre>