0

I try to send png image captured by ffmpeg throw RabbitMQ. When i sent just png in RabbitMQ body all was OK.

But now I need to add some information into message. I thought send png as json is easy, but png object is byte object and it is needed to be converted to string because if i don't convert it to string json.dumps throw exception "is not JSON serializable".

I tried to convert byte to str after that sending was successful, but when i revive, i can't save it as png file it saves as text file. But i need to save it as png file.

Sender

data['img']=str(image)
data['camera_id']=0
channel.basic_publish(exchange='',
                  routing_key='hello',
                  body=json.dumps(data))

Reciver

def callback(ch, method, properties, body):
    data= json.loads(body.decode("utf-8"))
    print(data['camera_id'])
    f = open("pypeg.png",'wb')
    f.write(data['img'].encode())
    f.close()

Maybe you now how to send just image in RabbitMQ body and send properties by another way. Or how to code/encode png image for correct converting to json and saving to file.

I tried use base64.encode() but json.dumps send same exception, so i use decode('utf-8')

data['img']=base64.b64encode(image).decode('utf-8')

therefore I've edited receiver

b1 = data['img'].encode('utf-8')
b2= base64.b64decode(b1)
f.write(b2)
SkiF
  • 45
  • 8

0 Answers0