I have a npz file saved from numpy that I can load by using numpy.load(mynpzfile)
. However, I would like to save this file as a part of a binary file, packed with another file. Something like:
import numpy as np
from PIL import Image
from io import BytesIO
# Saving file jointly
input1 = open('animagefile.png', 'rb').read()
input2 = open('npzfile.npz', 'rb').read()
filesize = len(input1).to_bytes(4, 'big')
output = filesize input1 input2
with open('Output.bin', 'wb') as fp:
fp.write(output)
# Open them
input1 = open('Output.bin', 'rb').read()
filesize2 = int.from_bytes(input1[:4], "big")
segmentation = Image.open(BytesIO(input1[4:4 filesize2]))
# THIS LINE GIVES ME AN ERROR
layouts = np.frombuffer(input2[4 filesize2:])
However, when reading back the npz I get an error. I have tried load and frombuffer, and both give me an error:
- frombuffer:
ValueError: buffer size must be a multiple of element size
- load:
ValueError: embedded null byte
What can I do?
CodePudding user response:
The last line should be input1
, not input2
. Also, guessing from the error message, you forgot to put BytesIO
.
layouts = np.load(BytesIO(input1[4 filesize2:]))
^^^^^^^ ^