【问题标题】:Load an image from an input stream in Python从 Python 中的输入流加载图像
【发布时间】:2012-02-04 09:54:57
【问题描述】:

我正在使用 Python Imaging Library 进行一些处理,并且我想通过管道向我的脚本提供数据,而不是将文件名作为参数传递。

我已经看到Image.open 可以接受除字符串之外的任何类似文件的对象作为参数。我试过直接传递sys.stdin,但似乎seek 有一些问题(逻辑上)。

那么,如何将流包装到提供所有文件操作的缓冲区中? (或其他任何直接从stdin 读取图像的合适解决方案)。

【问题讨论】:

    标签: python image file input stream


    【解决方案1】:

    使用StringIO

    import StringIO
    import sys
    
    filelike = StringIO.StringIO(sys.stdin.read())
    
    # Now use `filelike` as a regular open file, e.g.:
    filelike.seek(2)
    print filelike.read()
    

    【讨论】:

    • 我已经找到了解决方案,并且正准备自动回答这个问题,但还是谢谢! :-)
    猜你喜欢
    • 1970-01-01
    • 2012-12-01
    • 2015-02-27
    • 1970-01-01
    • 1970-01-01
    • 2011-04-20
    • 2018-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多