Hack Forums
[Python] Sending Image through Socket Programming - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Sending Image through Socket Programming (/showthread.php?tid=5832966)



Sending Image through Socket Programming - Vishnu™ - May 4, 2018

I am taking screenshot using ImageGrab from PIL library and want to send same from server to client.
I am able to send messages bidirectionally. but can't figure out sending objects or anything other than messages

Server Code :
Code
import socket
import cv2
from PIL import ImageGrab
mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysocket.bind(("xx.xx.xx.xx",6666))
mysocket.listen(5)
(client, (ip, port)) = mysocket.accept()
image = ImageGrab.grab()
msg = "test"
client.send(msg.encode())
#
#
#

Client Code:
Code
import socket
import cv2

mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysocket.connect(("xx.xx.xx.xx",6666))
data = mysocket.recv(1024)
print(data)

If you can, please replace code with image i am trying to send
Thanks  🎉


RE: Sending Image through Socket Programming - JahBless - May 5, 2018

Try something like this (sorry, i dont have time to replace into your code):
Code
From PIL import ImageGrab
import numpy as np

printscreen = ImageGrab.grab(bbox=(0, 0, 800, 600)
printscreen_np = np.array(printscreen.getData(), dtype='uint8')
This would take the pixels and puts them in an array
( see python GTA AI tutorial: https://www.youtube.com/watch?v=ks4MPfMq8aQ )


RE: Sending Image through Socket Programming - w33dt4k3r - May 5, 2018

The best method i think is to convert the img to a string.
After this u can use base64 to compress the string and send it.


RE: Sending Image through Socket Programming - .kr. - May 5, 2018

Read this post on Stackoverflow.com
https://stackoverflow.com/questions/42459499/what-is-the-proper-way-of-sending-a-large-amount-of-data-over-sockets-in-python


RE: Sending Image through Socket Programming - Blackshield_real - May 6, 2018

Hi , I need some help in socket programming as well, could anyone here tell me how can I make something like a client querying data from the server (maybe a dictionary storing stock and their prices) send data from server to the client (according to the request like stock first and then prices maybe price of multiple stocks at a time). Can I also make the data look in a tabular form as well on the client end?

I'm at the point where I have done simple client-server code did played with a dictionary and tried sending it over to the client but comes as a single vertical line not in a tabular form, is this even possible? I'm fairly new to python so..
please Guide me through some references if possible.