keras-team/keras-preprocessing

Pillow 8.3.0 breaks image.img_to_array()

matthewlehew opened this issue · 1 comments

Was incredibly frustrated with an error popping up out of nowhere today on a script that's worked in the past. Turns out that Pillow 8.3.0 was released today, the same day I created a new virtual environment for my project.

  • Check that you are up-to-date with the master branch of keras-preprocessing. You can update with:
    pip install git+git://github.com/keras-team/keras-preprocessing.git --upgrade --no-deps

  • Provide a link to a GitHub Gist of a Python script that can reproduce your issue (or just copy the script here if it is short).

Traceback:

Traceback (most recent call last):
  File "imgTest.py", line 11, in <module>
    image_array = image.img_to_array(img)
  File "/Users/matthewlehew/Developer/googleimages/imageprojectvenv/lib/python3.8/site-packages/keras/preprocessing/image.py", line 225, in img_to_array
    return image.img_to_array(img, data_format=data_format, **kwargs)
  File "/Users/matthewlehew/Developer/googleimages/imageprojectvenv/lib/python3.8/site-packages/keras_preprocessing/image/utils.py", line 309, in img_to_array
    x = np.asarray(img, dtype=dtype)
  File "/Users/matthewlehew/Developer/googleimages/imageprojectvenv/lib/python3.8/site-packages/numpy/core/_asarray.py", line 83, in asarray
    return array(a, dtype, copy=False, order=order)
TypeError: __array__() takes 1 positional argument but 2 were given

Reproduce:

  1. Fresh virtual environment (I use Python 3.8).
  2. Install packages:
pip install keras
pip install tensorflow
pip install pillow
  1. Run below script and get traceback listed above.
  2. Ensure keras preprocessing is up to date with master branch, rerun script, and get same traceback:

pip install git+git://github.com/keras-team/keras-preprocessing.git --upgrade --no-deps

  1. Go into the site-packages directory of the Python virtual environment, replace the PIL and Pillow-8.3.0.dist-info folders with the PIL and Pillow-8.2.0.dist-info folders from older virtual environment.
  2. Script executes successfully.

Test Script:

This is stripped down from a larger project, but I was able to reproduce with this code:

import numpy as np
from keras.preprocessing import image

img_size = (224, 224)
img = image.load_img('testImage.jpg', target_size=img_size)
image_array = image.img_to_array(img)
Cbezz commented

I also have this issue, sadly your fix did not work for me :(
Thanks for logging the ticket
I think I'll try creating a new virtual environment and using an older version of python (I just tried 3.9)