[英]Python: upload Pillow Image to Firebase storage bucket
我想弄清楚如何將Pillow
Image
實例上傳到 Firebase 存儲桶。 這可能嗎?
這是一些代碼:
from PIL import Image
image = Image.open(file)
# how to upload to a firebase storage bucket?
我知道有一個gcloud-python
庫,但這是否支持Image
實例? 將圖像轉換為字符串是我唯一的選擇嗎?
gcloud-python
庫是正確使用的庫。 它支持從字符串,文件指針和文件系統上的本地文件上傳(請參閱docs )。
from PIL import Image
from google.cloud import storage
client = storage.Client()
bucket = client.get_bucket('bucket-id-here')
blob = bucket.blob('image.png')
# use pillow to open and transform the file
image = Image.open(file)
# perform transforms
image.save(outfile)
of = open(outfile, 'rb')
blob.upload_from_file(of)
# or... (no need to use pillow if you're not transforming)
blob.upload_from_filename(filename=outfile)
這是直接將枕頭圖片上傳到firebase存儲的方法
from PIL import Image
from firebase_admin import credentials, initialize_app, storage
# Init firebase with your credentials
cred = credentials.Certificate("YOUR DOWNLOADED CREDENTIALS FILE (JSON)")
initialize_app(cred, {'storageBucket': 'YOUR FIREBASE STORAGE PATH (without gs://)'})
bucket = storage.bucket()
blob = bucket.blob('image.jpg')
bs = io.BytesIO()
im = Image.open("test_image.jpg")
im.save(bs, "jpeg")
blob.upload_from_string(bs.getvalue(), content_type="image/jpeg")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.