Reputation: 81
I'm getting this error trying to read a png image pulled from a webpage with requests...
UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7fa8aca13ef0>
Traceback:
File "/home/user/.conda/envs/my_env/lib/python3.8/site-packages/streamlit/script_runner.py", line 333, in _run_script
exec(code, module.__dict__)
File "/home/user/app/app.py", line 463, in <module>
img_details, message, title, invalid_file_paths, dates_df = pull_imgs(url=url, user=user, password=password, input_dir=input_dir)
File "/home/user/app/app.py", line 229, in pull_imgs
img = Image.open(img_bytes)
File "/home/user/.conda/envs/my_env/lib/python3.8/site-packages/PIL/Image.py", line 3023, in open
raise UnidentifiedImageError(
Here is the segment of code that is causing the error:
for photo in valid_file_paths:
# PULL PHOTO
response = requests.get(photo[0], auth=(user, password), stream=True)
# FILENAME
file = extract_file(photo[0])[1]
ext = photo[1]
filename = file + '.' + ext
img_bytes = io.BytesIO(response.content)
img = Image.open(img_bytes)
img.save(os.path.join(input_dir, filename), format=ext, optimize=True)
I have tried to resolve the issue using solutions provided here, but was unsuccessful. Any advice is appreciated. Simply using a try statement to avoid the problem files doesn't work for me. I need a way to read them even if they are corrupted, truncated or some other issue.
Upvotes: 1
Views: 868
Reputation: 81
print(response.content[:20]) showed that I was pulling some bits of HTML, rather than the actual image. Don't be like me and always check the output of your requests.
Upvotes: 2