I am trying to read a static image from a web request. This is my test code which works fine:

import requests
from io import BytesIO
from PIL import Image

master_ip = '192.168.1.10'
url = 'http://' + master_ip + '/liveimage.jpg'
response = requests.get(url=url)
img = Image.open(BytesIO(response.content))
img.save('test.jpg')

However, when I embed this into a class like so:

import requests
from io import BytesIO
from PIL import Image

class foo:

    def get_image(self) -> None:
     """Read the last image from the web ui."""
     url = 'http://' + self.master_ip + '/liveimage.jpg'
     response = requests.get(url=url)
     img = Image.open(BytesIO(response.content))
     img.save('test1.jpg')

I get:

Traceback (most recent call last):
  File "/home/anon/code/test_keyence.py", line 9, in <module>
    img = driver.get_image()
  File "/home/anon/code/ambios-drivers/ambidrivers/keyence/sr5000.py", line 487, in get_image
    img = Image.open(BytesIO(response.content))
  File "/home/anon/ambi_venv/lib/python3.9/site-packages/PIL/Image.py", line 3298, in open
    raise UnidentifiedImageError(msg)
PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7fd0bd0e9630>
0

There are 0 best solutions below