【问题标题】:saving python PIL image from Google cloud datalab to Google cloud storage将 python PIL 图像从谷歌云数据实验室保存到谷歌云存储
【发布时间】:2017-07-20 22:25:09
【问题描述】:

我正在尝试将 PIL 图像从 Datalab 保存到谷歌云存储。

from PIL import Image
out_image = Image.open( StringIO( gs_buck_obj.read_stream() ) )

# run through cloud vision api and do some stuff
....

my_file = StringIO()
out_image.save(my_file , "PNG")
**This is where I am not sure what to do but have tried next line
gs_buck_obj_out.write_stream(my_file.getvalue(), 'text/plain')

【问题讨论】:

    标签: google-cloud-platform google-cloud-storage python-imaging-library google-cloud-datalab google-cloud-vision


    【解决方案1】:

    这个stackoverflow question 提出了一个python open 包装器用于所有gs:// 路径的问题。一个非常干净的解决方案在于使用tensorflow 中的file_io 模块。

    例子:

    from tensorflow.python.lib.io.file_io import FileIO
    
    with FileIO('gs://my-bucket/20180101/my-file.txt', 'r') as f:
       print(f.readlines()) # works
    
    with FileIO('gs://my-bucket/20180101/my-file.txt', 'w') as f:
       f.write('I love roti prata.') # works
    
    with FileIO('my-file.txt', 'w') as f:
       f.write('I love palak paneer.') 
       # also works with local files in reading and writing
    

    正如post 所述,Cloud Datalab 确实支持 tensorflow。FileIO 支持'wb'写入字节)模式。因此,解决您的问题的方法是

    with FileIO('out_image.png', 'wb') as f:
        out_image.save(f, "PNG")
    

    【讨论】:

    • 关于此解决方案的说明:它似乎只对 App Engine 柔性环境有用,因为 tensorflow 依赖于无法在标准环境中安装的 C 库。我错过了什么吗?
    猜你喜欢
    • 1970-01-01
    • 2018-10-11
    • 1970-01-01
    • 2015-07-17
    • 1970-01-01
    • 2020-09-10
    • 2020-10-05
    • 2013-06-26
    • 2020-08-25
    相关资源
    最近更新 更多