Search code examples
pythonimagebytepngbytesio

OSError: cannot identify image file <_io.BytesIO object at 0x00000222C8A21360>


import io
from io import BytesIO
from PIL import ImageGrab, Image

im = ImageGrab.grab()
image_bytes = im.tobytes()

img = Image.open(BytesIO(image_bytes))
img.show()

In the above mentioned code I am trying to get the image from ImageGrab. I want to convert it to bytes and want to send it to the server through sockets. But before that I want to show the image. I think there is something missing for image format(png or jpg) in this code. But I am not getting any clue about what should I do with this code to set the particular image format either png or jpg


Solution

  • I have used Image.Save() method instead. In that it saves to the disk first and then I copy from there and convert it to bytes and send it to the socket.