我正在尝试将 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 allgs://
paths. A very clean solution lies in using thefile_io
module intensorflow
.
示例:
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/