I want to temporarily save the stream (ByteIO data) obtained from PiCamera.

Asked 2 years ago, Updated 2 years ago, 97 views

*You may not understand the ByteIO completely.Please understand.
As a flow,

P Take a picture with PiCamera and save it to the variable stream
① Save stream to another variable stream_save
② Process stream with seek() or truncate() functions
stream_save is then another action
I would like to process it in this way, but the moment I do を, it changes to stream_save and bugs in で.

stream_saved=io.BytesIO()
    for foo in picamera.capture_continuous(stream, "png"):
        stream_saved=stream
        a number such as print(stream_saved.tell())#801234
        stream.seek(0)
        print(stream_saved.tell())#0

I thought about saving the value of stream_saved.tell() in advance, but I couldn't handle it considering using functions such as truncate().
I'm sorry for the lack of understanding, but I'd appreciate it if you could tell me how to save and process byte data obtained from the camera into two variables.

Additional
I used the ByteIO() function and was able to copy it.Thank you.
stream_saved that you copied
https://picamera.readthedocs.io/en/release-1.10/recipes1.html
I tried to send it in socket with the following code referring to 4.9 in , but it didn't work well...
When I copied it, it was not copied to the value .tell(), so I copied the reference location of the original data in seek().
Are there other elements that ByteIO(stream.read()) does not copy?

stream_saved=io.BytesIO()
   for foo in picamera.capture_continuous(stream, "png"):
       stream_saved=io.BytesIO(stream.read())
     stream_saved.seek (stream.tell)
       connection.write(structure.pack('<L',stream_saved.tell()))) 
       connection.flush()
       stream_saved.seek(0)
       connection.write(stream_saved.read())# It doesn't work here
       # Reset the stream for the next capture
       stream_saved.seek(0)

python3 raspberry-pi camera stream

2022-09-30 14:31

1 Answers

Try replicating byte[] data in io.BytesIO(stream.read()) by referring to the sample code at the end of your answer.

The second line declares stream_saved for the code in question, but stream_saved=stream immediately after the fourth line for statement discards the second line object, and stream_saved and stream refer to the same object.
If you change or clear the offset with seek or trunkate in stream, this will affectstream_saved` because it has the same object.

importio
stream=io.BytesIO(b'1234567890abcdef')

#Create a new BytesIO by replicating the byte array
stream_saved=io.BytesIO(stream.read())
# Stream.read() moves offset to end, so revert as needed
stream.seek(0)

# Below is the code to investigate whether stream and stream_saved are independent.
stream.seek(3)
print(stream_saved.tell())#0
stream.truncate()
print(stream_saved.read())#b'1234567890abcdef'


2022-09-30 14:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.