Python Sockets: Image is cropped after transferring from client side to server side

Viewed 36

I have a sever.py file which runs in a while loop:

# SNIPPET
def receive_data(client_socket):
    img_id = random.randint(1, 10000)
    try:
        file = open(f'server_image_{img_id}.jpg', "wb")
        image_chunk = client_socket.recv(2048)

        while image_chunk:
            file.write(image_chunk)
            image_chunk = client_socket.recv(2048)

        file.close()
    except:
        return False
while True:
    read_sockets, _, exception_sockets= select.select(sockets_list, [], sockets_list)

    for notified_socket in read_sockets:
        if notified_socket == server_socket:
            client_socket, client_address= server_socket.accept()

            cli_data = receive_data(client_socket)
            if cli_data is False:
                continue

and main.py, which has threads running in the background:

# SNIPPET
img = open(img_param_path, 'rb')
image_data = img.read(2048)
while image_data:
    client_inst.send(image_data)
    image_data = img.read(2048)

img.close()

and this is the output: left image is the received image, right one is the original image

Note that I've tried several other images, and they seem to have the same problem too but it is barely noticeable, only this image is about 138kb. I have no idea if the problem is the size or the wrong code. Any help is appreciated!

0 Answers
Related