如何将图像目录转换为.gz?

2024-06-11 00:38:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是Python,我有几个图像目录,我想把它们转换成.gz文件,这样我就可以学习宽面条教程了。本教程使用存储在单个.gz文件中的训练图像。我也在尝试将我的图像目录转换成.gz,这样我就可以模拟教程代码并更好地理解它。在

特别是,我试图理解MNIST.gz文件的格式,比如在Dr. LeCun's website找到的train-images-idx3-ubyte.gz。在

我可以将单个图像转换为.gz,但不能转换为目录。我在网上的搜索结果表明这是意料之中的。如何创建包含多个训练图像信息的.gz文件?在

如果你需要更多的信息,或者我问错了问题,或者你的方向是不理智的,请告诉我。谢谢。在


Tags: 文件代码图像目录信息格式train教程
1条回答
网友
1楼 · 发布于 2024-06-11 00:38:12

你不能。gzip是一种流压缩方法,它不是一个容器。在这种情况下,图像存储在文件容器中,该容器在页面底部描述:

the IDX file format is a simple format for vectors and multidimensional matrices of various numerical types. The basic format is magic number size in dimension 0 size in dimension 1 size in dimension 2 ..... size in dimension N data

The magic number is an integer (MSB first). The first 2 bytes are always 0.

The third byte codes the type of the data: 0x08: unsigned byte 0x09: signed byte 0x0B: short (2 bytes) 0x0C: int (4 bytes) 0x0D: float (4 bytes) 0x0E: double (8 bytes)

The 4-th byte codes the number of dimensions of the vector/matrix: 1 for vectors, 2 for matrices....

The sizes in each dimension are 4-byte integers (MSB first, high endian, like in most non-Intel processors).

The data is stored like in a C array, i.e. the index in the last dimension changes the fastest.

一种更典型的方法是使用tarball归档文件作为容器,然后压缩归档文件。这样做的好处是,这是创建gzip压缩归档文件的标准方法,不需要自定义脚本来提取文件。在

关于如何在给定的映像目录中执行此操作的示例如下(在*Nix系统上使用Bash):

tar -zcvf tar-archive-name.tar.gz source-folder-name

Gzip压缩内置了-z标志,或者您也可以使用gzip命令来执行自己的压缩。在

在Python中,还可以使用gzip压缩创建tarfile存档:

一个由documentation修改的简单示例如下:

^{pr2}$

模式'w:gz'指定存档将被gzip压缩,这将在任何操作系统上运行。在

相关问题 更多 >