【问题标题】:Big data image processing in Python [closed]Python中的大数据图像处理[关闭]
【发布时间】:2016-03-26 13:45:37
【问题描述】:

我有一个 7GB 的 .tgz 文件,其中包含我想在 Python 中处理的数千张高分辨率照片。对于单个图像,我可以执行以下所有操作,但我不确定如何处理如此大的数据和 .tgz 文件格式。我用谷歌搜索过,但也许我没有使用最好的搜索词。显式代码对我理解最有帮助。

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

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

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

目标是处理用于卷积神经网络 (CNN) 的数据。

【问题讨论】:

  • 虽然,这并不完全是一个答案,更多的是一个建议:如果您需要处理数据,也许值得先将数据解包一次,然后多次处理解包后的图像。

标签: python numpy image-processing tar


【解决方案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()

【讨论】:

  • 如果你使用itgz.extractfile,你会得到一个类似对象的文件,而不是itgz.extract。类文件对象是只读的,并提供以下方法:read()、readline()、readlines()、seek() 和 tell()
【解决方案2】:

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

你可以开始here

【讨论】:

    猜你喜欢
    • 2010-09-10
    • 1970-01-01
    • 1970-01-01
    • 2013-11-06
    • 2021-06-23
    • 2013-01-13
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多