liad inon
liad inon

Reputation: 263

How i send big messages faster in python socket?

I am trying to create some kind of screen share with python socket. The problem is that the images of my screen are very big (3,110,482‬ bytes) and it takes a lot of time for the socket to send them to the server. For making the sending more efficient I lowered the resolution of the images I am sending, but it is not enough. So I need to make the sending process more efficient.

Here is the function that takes images of my screen:

import numpy as np # for array manipulation
import pyautogui   # for recording images of the screen
import cv2         # for change the resolution of the images
from screeninfo import get_monitors # for getting the screen size

def get_screen_img(): # take an image of the screen and return it
    img = pyautogui.screenshot() # take a screenshot

    img = np.array(img) # convert to numpy array

    monitor = get_monitors()[0] # get info on the screen monitor
    # lowered the resolution by half
    img = cv2.resize(img, dsize=(monitor.width//2, monitor.height//2), interpolation=cv2.INTER_CUBIC)

    # do some manipulation for seeing the image right
    img = np.fliplr(img) # flip the image array
    img = np.rot90(img)  # rotate the image array in 90 degrees

    return img # return the image

Here is the function that sends the images:

import socket # for sending data
import pickle # for converting any kind of data to bytes

    def send_image(): # send a message
        # send the image and the type because I am sending more than just images so I need to tell the server what kind of info it gets
        msg = {"type": "image", "content": get_screen_img()}            

        msg = pickle.dumps(msg) # convert the message to bytes

    cSocket.send(msg) # send the msg

Edit: I am 99% sure that the problem is the size of the message. When I lowered the resolution more it works fine, but I need to send images in normal resolution.

Upvotes: 1

Views: 1362

Answers (1)

liad inon
liad inon

Reputation: 263

So I find a solution. I compress the image with numpy and io like this:

img = get_screen_img()
compressed_img = io.BytesIO()
np.savez_compressed(compressed_img, img)

And decompress like this:

compressed_img.seek(0)
decompressed_img = np.load(compressed_img])['arr_0']

So if you have a efficiency problem with sending big messages with socket. I think that the best solution is to compress the message. You can also can compress with zlib or ather library.

Also in my case is work more faster if you dont send new frame if is the same to previous frame I send.

Upvotes: 2

Related Questions