我有一個從 numpy 保存的 npz 檔案,我可以使用numpy.load(mynpzfile). 但是,我想將此檔案保存為二進制檔案的一部分,并與另一個檔案打包在一起。就像是:
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:])
但是,在讀回 npz 時出現錯誤。我試過 load 和 frombuffer,都給我一個錯誤:
- 來自緩沖區:
ValueError: buffer size must be a multiple of element size - 加載:
ValueError: embedded null byte
我能做些什么?
uj5u.com熱心網友回復:
最后一行應該是input1,不是input2。另外,從錯誤訊息中猜測,您忘記輸入BytesIO.
layouts = np.load(BytesIO(input1[4 filesize2:]))
^^^^^^^ ^
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/461727.html
