Python中的大数据图像处理

时间:2016-03-26 13:45:37

标签: python numpy image-processing tar

我有一个7GB .tgz文件,它是我想在Python中使用的数千张高分辨率照片的存档。我能够在单个图像的情况下执行以下所有操作,但我不确定如何使用这样的大数据和.tgz文件格式。我用Google搜索了,但也许我没有使用最好的搜索字词。显式代码对我来说是最有帮助的。

如何将此.tgz数据加载到Python中? (pickle,numpy,tarfile?pip install tarfile失败。)我最终希望将它们转换为numpy数组。

如何将所有图像设为分辨率?

如何将所有图像转换为灰度?

目标是操纵数据以用于卷积神经网络(CNN)。

2 个答案:

答案 0 :(得分:1)

我不确定处理存档是否是您的问题。很明显,应该使用tarfile来处理.tgz文件。 tarfile在python中的内置模块中,您不需要pip install

#!/usr/bin/env python

# import the tarfile
from tarfile import TarFile

# Open your tarfile for reading
itgz = TarFile.gzopen( "photos.tgz", 'r' )

# Open your tarfile for saving the images
otgz = TarFile.gzopen( "photos_edited.tgz", 'w' )

# Handle the images one-by-one
for img_name in itgz.getnames() :
    # Extract it to where ever you want
    itgz.extract( img_name )

    # Do the image processing numpy, PIL or any tool of your choice

    # If you want to save the edited images back to a tar file
    otgz.add( img_name )

else:
    itgz.close()
    otgz.close()

答案 1 :(得分:0)

我会使用tarfile标准模块(您无需安装它 - 它已经存在)来访问您的压缩数据,并使用scipy.ndimage来处理您的图像。

您可以here

开始