4

I have successfully sent an image through a socket and, on the receiving end, I have the exact same raw bytes that the image file that was sent had. This means that if I binary write those bytes to a file, I'll obtain the same file as the one that was sent. I have tried showing the image from Python without saving it first, but I'm having trouble doing so. If I understand correctly, matplotlib.imread() requires the path to a file and then decodes that file into several matrices. Doing something like this works fine:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

# data is the image data that was received from the socket

file = open("d:\\image.png", 'wb')
file.write(data)
file.close()

img = mpimg.imread("d:\\image.png")
plt.imshow(img)
plt.show()

Obviously I should use a temporary file for that, but I wrote that just for the sake of the example. Is there any way to call the imshow() and show() methods without having called imread() beforehand, provided I already have those bytes?

1 Answer 1

11

If it's OK for you to read from the socket directly, you can convert the socket to a file object using makefile(), then provide the socket to imread as you would with a regular file. Remember to set the codec when reading:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

# don't read from your socket, instead, call this where you would call read
fp = your_socket.makefile()

with fp:
    img = mpimg.imread(fp, format='jpeg')
plt.imshow(img)
plt.show()

I've searched and couldn't find a way to directly decode images from bytes in matplotlib. If it is not OK to use the above solution because you have already the bytes array, then use BytesIO to create a temporary buffer:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import io

fp = io.BytesIO(data)

with fp:
    img = mpimg.imread(fp, format='jpeg')
plt.imshow(img)
plt.show()
4
  • The first solution should work, but it doesn't fit too well with what I'm trying to do with this application. From the client, I request several images which I then either save, or first require to view them and save if wanted. I don't know if I can use your solution with more than one image. I'm currently trying out the second one. Commented Sep 28, 2020 at 10:12
  • I'm getting an exception on imread on your second solution: py3.8\lib\site-packages\PIL\PngImagePlugin.py", line 639, in _open raise SyntaxError("not a PNG file") File "<string>", line None SyntaxError: not a PNG file Commented Sep 28, 2020 at 10:20
  • I used your second solution with Pillow instead of matplotlib and it works! It turns out matplotlib.image.imread() can't take a BytesIO object as a parameter, but the following code worked for me: from PIL import Image img = Image.open(io.BytesIO(data)) img.show() Commented Sep 28, 2020 at 10:31
  • 2
    You have to specify the codec in imread, otherwise it assumes PNG format which is the error you had. Try mpimg.imread(fp, format="jpeg") if using JPEG. Or the file format you use to encode the image over the socket... Commented Sep 28, 2020 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.