3
votes

I am loading image through PIL open load() function and changing the pixels. I have to then send this to client through socket. How can I do this without saving the image first. In my current approach I save the image using save function and then open it as file in rb mode and send it. pic = Image.open (image_name) pixels = pic.load() #code to manipulate pixels of the image

#want to avoid this section
pic.save("anyname","png")
f = open("anyname.png","rb")
data = f.read()
client_connection.sendall(data)
1

1 Answers

2
votes

You can create an in-memory file handle and write to that:

from io import BytesIO
fd = BytesIO()
pic.save(fd, "png")
client_connection.sendall(fd.getvalue())