ValueError:字符串长度不等于格式和分辨率大小

2024-05-29 04:05:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要帮助我已尝试在当前项目中实现以下代码:Screen sharing in python但是客户端返回ValueError:字符串长度不等于格式和分辨率大小。除了来自像素的图像外,其他一切似乎都正常

澄清:VidStreamBroadCast是服务器,VidStreamReceive是客户端。在客户端类pygame.display.flip()中,正在生成错误,其明显来源于img=pygame.image.fromstring

class VidStreamBroadCast:

    def __init__(self, conn, width=800, height=600, public="public.pem", private="private.pem"):
        self.__width = width
        self.__height = height
        self.__ViralRSA = ViralRSA.ViralRSA(public, private)
        self.__conn = conn
        thread = Thread(target=self.__retreive_screenshot, args=(self.__conn,))
        thread.start()

    def __retreive_screenshot(self, conn):
        with mss() as sct:
            # The region to capture
            rect = {'top': 0, 'left': 0, 'width': self.__width, 'height': self.__height}

            while 'recording':
                # Capture the screen
                img = sct.grab(rect)
                # Tweak the compression level here (0-9)
                pixels = compress(img.rgb, 6)

                # Send the size of the pixels length
                size = len(pixels)
                size_len = (size.bit_length() + 7) // 8
                conn.send(bytes([size_len]))

                # Send the actual pixels length
                size_bytes = size.to_bytes(size_len, 'big')
                conn.send(size_bytes)

                # Send pixels
                print(pixels)
                conn.sendall(pixels)

class VidStreamReceive:

    def __init__(self, conn, width=800, height=600, public="public.pem", private="private.pem"):
        self.__conn = conn
        self.__width = width
        self.__height = height
        self.__ViralRSA = ViralRSA.ViralRSA(public, private)
        self.__initiate_window()
        self.__watching = True

    def __receive_all(self, length):
        """ Retreive all pixels. """

        buf = b''
        while len(buf) < length:
            data = self.__conn.recv(length - len(buf))
            if not data:
                return data
            buf += data
        return buf

    def __initiate_window(self):
        pygame.init()
        screen = pygame.display.set_mode((self.__width, self.__height))
        clock = pygame.time.Clock()
        watching = True

        while watching:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    watching = False
                    self.__watching = True
                    break

            # Retreive the size of the pixels length, the pixels length and pixels
            size_len = int.from_bytes(self.__conn.recv(1), byteorder='big')
            size = int.from_bytes(self.__conn.recv(size_len), byteorder='big')
            pixels = decompress(self.__receive_all(size))
            print(pixels)

            # Create the Surface from raw pixels
            img = pygame.image.fromstring(pixels, (self.__width, self.__height), 'RGB')
            # Display the picture
            screen.blit(img, (0, 0))
            pygame.display.flip()
            clock.tick(60)

    def get_watching(self):
        return self.__watching

注意:我知道ViralRSA类,它目前不做任何事情,它是为将来的RSA加密设计的。同样,在重要的情况下,通过TLS_v1.2 SSL套接字调用这些类。此外,所有周围的插座结构似乎都在工作

编辑原因显然是睡衣游戏是睡衣


Tags: theselfsizelendefprivatepublicconn

热门问题