'Trying to send screenshot over sockets using pyautogui

I tried to send screenshot, but when I get the screenshot I get this picture on my screen, instead of normal one...

What am I doing wrong?

Server:

elif data.decode().lower() == 'screenshot':
    try:
        image = pyautogui.screenshot()
        client.send(image.tobytes())
    except:
        pass

Client:

elif msg.lower() == 'screenshot':
    try:
        data = client.recv(1310720000)
        data = Image.frombytes('RGB', (1280, 720), data)
        while os.path.exists(r'c:\ScreenshotsPY\Screenshot ' + str(n) + '.png') == True:
            n += 1
        data.save(r'c:\ScreenshotsPY\Screenshot ' + str(n) + '.png')
        data.show()
    except:
        print('Screenshot has been failed')

Picture:  image



Solution 1:[1]

If you are sure the size and mode variables are correct, then maybe when sending the pictures some of the pixels come out as trash because utf-8 doesn't have a representation for them. Try to send the data in base64 and than decode on arrival like this:

At client and server:

`import base64`

At client:

client.send(base64.b64encode(image.tobytes()))

At server:

data = Image.frombytes('RGB', (1280, 720), base64.b64decode(data))

I don't know for sure that this is the problem but it did solve some problems for me.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lior Simionovici