【发布时间】:2022-01-23 07:22:40
【问题描述】:
我的 python 内存中有很多图像,我想将它们放在 内存中 tar 中,之后我想将该 tar 刷新到磁盘。
我想在内存中执行此操作,以防止将大量小文件写入我的磁盘。
到目前为止,我有一种将图像保存为内存文件的方法
import imageio
im = np.zeros((256,256))
out_file = io.BytesIO()
imageio.imsave(out_file, im, format = 'jpg')
我也可以将这些内存文件放入内存tar中。
import tarfile
t = tarfile.TarInfo("helloworld.tif")
t.size = len(out_file.getbuffer())
tarBuffer = io.BytesIO()
tar = tarfile.TarFile(mode="w", fileobj=tarBuffer)
tar.addfile(t, io.BytesIO(out_file.getbuffer()))
但现在我不知道如何将内存中的 tar 文件转入我的磁盘。
有没有人对如何做到这一点有任何建议?
【问题讨论】:
-
好像你会在this 帖子中这样说;只需打开文件句柄
f指向您要写入磁盘的最终tarfile 的路径,然后在seek开始之后执行f.write(tarBuffer.getvalue())之类的操作。