Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

BytesIO compatibility for "image.load_img" broken in 1.1.1 release #293

Open
ndamclean opened this issue May 12, 2020 · 7 comments
Open
Labels
image Related to images

Comments

@ndamclean
Copy link

ndamclean commented May 12, 2020

The most recent release on PyPi (1.1.1) seems to break the compatibility of the tensorflow.keras.preprocessing.image.load_img function with python io.BytesIO arguments as input.

This code snippet was working in the previous release (1.1.0).
(requires pip-installable requests package to run)

import io
import requests
from tensorflow.keras.preprocessing import image

# random cat picture URL
# Use a different URL with an image if this stops working
image_url = 'https://tr.rbxcdn.com/893df9c3d669d75943b6089b6eec5278/420/420/Decal/Png'

response = requests.get(image_url)
response.raise_for_status()
f = io.BytesIO(response.content)
img_data = image.load_img(f)

print(img_data)

After updating to the 1.1.1 release, the above snippet crashes with the following error message:

Traceback (most recent call last):
  File "tmp2.py", line 13, in <module>
    img_data = image.load_img(f)
  File "/home/nmclean/.virtualenvs/charm/lib/python3.6/site-packages/keras_preprocessing/image/utils.py", line 113, in load_img
    with open(path, 'rb') as f:
TypeError: expected str, bytes or os.PathLike object, not _io.BytesIO
@ndamclean ndamclean added the image Related to images label May 12, 2020
@Dref360
Copy link
Contributor

Dref360 commented May 12, 2020

Hello!
This commit seems to be the issue.

I can fix it tomorrow, but PRs are welcome :)

@FadingWinds
Copy link

I'm using 1.1.2 in Google Colab but still encounter this problem... downgrade to 1.1.0 works.

@Dref360
Copy link
Contributor

Dref360 commented Jul 6, 2020

Oh right, I cannot merge the PR myself.
Would anyone be available to make a PR? The diff is in #294 .

@alphaAI-stack
Copy link

alphaAI-stack commented Sep 1, 2020

this is may mybe a Keras-Preprocessing bug, i update Newest version Keras-Preprocessing 1.1.2,has the same problem

last,i back to pip3 install -U Keras-Preprocessing===1.1.0 fix it

my code is

  
response = requests.get(image_url, timeout=5, verify=False, headers={'connection': 'close'})
image_io = io.BytesIO(response.content)
img = image.load_img(image_io, target_size=(256, 256))

@cryanp-usgs
Copy link

Thanks for figuring this out! I was buffering some images to memory while assessing them with a keras model and code that worked last year was throwing the same error:

keras_preprocessing/image/utils.py", line 113, in load_img with open(path, 'rb') as f:
TypeError: expected str, bytes or os.PathLike object, not _io.BytesIOBytesIO

...downgrading to keras-preprocessing===1.1.0 in both Windows and Ubuntu builds fixed it immediately!

@TreeKat71
Copy link
Contributor

I also encounter this issue ... the commit in #261 break io.ByteIo. Just want to know will fix it? Thanks

This was referenced Feb 4, 2021
@TreeKat71
Copy link
Contributor

I do a fix in the #339

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
image Related to images
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants