google-cloud-platform - 将 python PIL 图像从 Google 云数据实验室保存到 Google 云存储

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

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

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')

最佳答案

This stackoverflow question raises the question of a python open wrapper for all gs:// paths. A very clean solution lies in using the file_io module in tensorflow.

示例:

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")

关于google-cloud-platform - 将 python PIL 图像从 Google 云数据实验室保存到 Google 云存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45226161/

相关文章:

kubernetes - 优先购买通知如何处理?

database - 在 Cloud Spanner 中,如何查看与我的实例的连接

macos - Tensorflow 错误 tensorflow/core/platform/cloud/http_request.cc :334

python - 在使用 Flask uwsgi 提供 PIL 图像时尝试隐式序列转换

python - 枕头属性错误

google-cloud-platform - Google 函数服务帐号

go - Cloud-Run 进程失败,出现 500 状态代码和 membarrier gvisor 错误

python - 对于Python, "import gcsfs"或 "from google.cloud import storage"与GCS交互

android - 使用 Android、App 引擎 + 数据存储存储个人资料图像的最佳方式

django - 尝试使用 elastic beanstalk 安装 libjpeg-devel 时出错