Python Pygame:通过网络PodSixNet发送图像

Python Pygame:通过网络PodSixNet发送图像,python,image,networking,pygame,Python,Image,Networking,Pygame,我正试图用PodSixNet通过网络发送图像。 我是如何做到这一点的: 客户端: #... image = pygame.image.load("character.png") img = pygame.image.tostring(image, "RGBA") connection.Send({"action":"setPlayerCharacter", 'playerName': self.playerName, 'playerImage': img}) 服

我正试图用PodSixNet通过网络发送图像。 我是如何做到这一点的:

客户端:

    #...
    image = pygame.image.load("character.png")
    img = pygame.image.tostring(image, "RGBA")
    connection.Send({"action":"setPlayerCharacter", 'playerName': self.playerName, 'playerImage': img})
服务器端:

  def Network_setPlayerCharacter(self, data):
    self.playerName = data['playerName']

    img = data['playerImage']
    self.playerImage = pygame.image.fromstring(img, (50, 50), "RGBA")
    self._server.SendPlayers() # Send data to other players
但是PodSixNet不喜欢字节。获取此错误:

error: uncaptured python exception, closing channel <__main__.ClientChannel 127.0.0.1:54822 at 0x3925ef0> (<class 'UnicodeDecodeError'>:'utf-8' codec can't decode byte 0xff in position 1611: invalid start byte [C:\Users\Mikal\AppData\Local\Programs\Python\Python37-32\lib\asyncore.py|read|83] [C:\Users\Mikal\AppData\Local\Programs\Python\Python37-32\lib\asyncore.py|handle_read_event|422] [C:\Users\Mikal\AppData\Local\Programs\Python\Python37-32\lib\asynchat.py|handle_read|171] [C:\Users\Mikal\AppData\Local\Programs\Python\Python37-32\lib\site-packages\PodSixNet\Channel.py|found_terminator|21] [C:\Users\Mikal\AppData\Local\Programs\Python\Python37-32\lib\site-packages\PodSixNet\rencode.py|loads|333] [C:\Users\Mikal\AppData\Local\Programs\Python\Python37-32\lib\site-packages\PodSixNet\rencode.py|f|320] [C:\Users\Mikal\AppData\Local\Programs\Python\Python37-32\lib\site-packages\PodSixNet\rencode.py|decode_string|196])
错误:未捕获的python异常,关闭通道(:'utf-8'编解码器无法解码位置1611处的字节0xff:无效的开始字节[C:\Users\Mikal\AppData\Local\Programs\python\Python37-32\lib\asyncore.py | read | 83][C:\Users\Mikal\AppData\Local\Programs\python\Python37-32\lib\asyncore.py handle | read事件| 422][C:\Users\Mikal\AppData\Local\Programs\Python\Python37-32\lib\asynchat.py | handle|read | 171][C:\Users\Mikal\AppData\Local\Programs\Python\Python37-32\lib\site packages\PodSixNet\Channel.py | found|终止符| 21][C:\Users\Mikal\AppData\Local\Programs\Programs\Python37-32\lib\site packages\PodSixNet\rencode.py[C:\Users\Mikal\AppData\Local\Programs\Python\Python37-32\lib\site packages\PodSixNet\rencode.py | f | 320][C:\Users\Mikal\AppData\Local\Programs\Python\Python37-32\lib\site packages\PodSixNet\rencode.py | decode| u string | 196])

有没有办法解决这个问题?

我不熟悉PodSixNet,但解决这个问题的一个简单方法似乎是对二进制图像数据进行Base64编码

import base64
...

image       = pygame.image.load( "tiny_alien.png" )
image_bytes = pygame.image.tostring( image, "RGBA" )
image_str   = base64.b64encode( image_bytes ).decode('iso-8859-1') # ASCII(ish)
无论出于何种原因,base64模块都会返回类似字节的对象,而不是字符串,因此需要对其进行进一步解码

请注意,如果使用
base64.a85encode(…)
生成的字符串将显著更小

另一方面,使用
base64.b64解码(…)
再次将字符串恢复为二进制:

image_bytes = base64.b64decode( image_str ) # back to pygame.tostring() format

我不熟悉PodSixNet,但解决这个问题的一个简单方法似乎是对二进制图像数据进行Base64编码

import base64
...

image       = pygame.image.load( "tiny_alien.png" )
image_bytes = pygame.image.tostring( image, "RGBA" )
image_str   = base64.b64encode( image_bytes ).decode('iso-8859-1') # ASCII(ish)
无论出于何种原因,base64模块都会返回类似字节的对象,而不是字符串,因此需要对其进行进一步解码

请注意,如果使用
base64.a85encode(…)
生成的字符串将显著更小

另一方面,使用
base64.b64解码(…)
再次将字符串恢复为二进制:

image_bytes = base64.b64decode( image_str ) # back to pygame.tostring() format

谢谢你的回答,@Kingsley工作得很好!只需通过服务器发送图像大小:)不用担心,很高兴我能提供帮助。谢谢你的回答,@Kingsley工作得很好!只需通过服务器发送图像大小:)不用担心,很高兴我能提供帮助。