Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
need PIL to import image
#1
The following python code when run gives the error.

import cv2
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split

from tensorflow.keras import Model, Input, regularizers
from tensorflow.keras.layers import Dense, Conv2D, MaxPool2D, UpSampling2D
from tensorflow.keras.callbacks import EarlyStopping
from keras.preprocessing import image

import glob
from PIL import Image
from tqdm import tqdm
import warnings;
warnings.filterwarnings('ignore')


# In[22]:


#!wget https://vis-www.cs.umass.edu/lfw/lfw.tgz
get_ipython().system('tar -xvzf lfw.tgz')
face_images = glob.glob('lfw/**/*jpg')


# In[23]:


all_images = []

for i in tqdm(face_images):
  img = image.load_img(i, target_size=(80,80,3))
  img = image.img_to_array(img)
  img = img/255.
  all_images.append(img)


# In[ ]:


# function to reduce image resolution while keeping the image size constant

def pixalate_image(image, scale_percent = 40):
  width = int(image.shape[1] * scale_percent / 100)
  height = int(image.shape[0] * scale_percent / 100)
  dim = (width, height)

  small_image = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)
  
  # scale back to original size
  width = int(small_image.shape[1] * 100 / scale_percent)
  height = int(small_image.shape[0] * 100 / scale_percent)
  dim = (width, height)

  low_res_image = cv2.resize(small_image, dim, interpolation = cv2.INTER_AREA)

  return low_res_image


# In[ ]:


all_images = np.array(all_images)

# split data into train and validation data
train_x, val_x = train_test_split(all_images, random_state=32, test_size=0.1)
The code gives the error and traceback

Error:
ImportError Traceback (most recent call last) <ipython-input-26-043ef551abfc> in <module> 2 3 for i in tqdm(face_images): ----> 4 img = image.load_img(i, target_size=(80,80,3)) 5 img = image.img_to_array(img) 6 img = img/255. /opt/conda/lib/python3.7/site-packages/keras_preprocessing/image/utils.py in load_img(path, grayscale, color_mode, target_size, interpolation) 106 color_mode = 'grayscale' 107 if pil_image is None: --> 108 raise ImportError('Could not import PIL.Image. ' 109 'The use of `load_img` requires PIL.') 110 img = pil_image.open(path) ImportError: Could not import PIL.Image. The use of `load_img` requires PIL.
I did install PIL or should I say Pillow. It was no trouble to install. I ran the code and got this error. It says it needs PIL.image to load . What am I doing wrong? How do I fix it?

I am running python 3.7 as show below.

Python 3.7.6 | packaged by conda-forge | (default, Mar  5 2020, 15:27:18) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
Any help appreciated. Thanks in advance.

Respectfully,

ErnestTBass
Reply
#2
I don't know if this will help much but, when I was working with images and running the code from atom, I had to use full path. Running from the shell I did not.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
Gaming Collection
Homepage
my-python
Reply
#3
PIL is part of pillow.
install as pillow, import as PIL
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020