将 ndarray(OpenCV 中的图像)以 .jpg 或 .png 格式上传到谷歌云存储

问题描述 投票:0回答:3

我有类似的问题,例如如何从 Python 脚本在 Google Cloud Storage 上上传字节图像

我试过这个

from google.cloud import storage
import cv2
from tempfile import TemporaryFile
import google.auth
credentials, project = google.auth.default()
client = storage.Client()
# https://console.cloud.google.com/storage/browser/[bucket-id]/
bucket = client.get_bucket('document')
# Then do other things...
image=cv2.imread('/Users/santhoshdc/Documents/Realtest/15.jpg')
with TemporaryFile() as gcs_image:
    image.tofile(gcs_image)
    blob = bucket.get_blob(gcs_image)
    print(blob.download_as_string())
    blob.upload_from_string('New contents!')
    blob2 = bucket.blob('document/operations/15.png')

    blob2.upload_from_filename(filename='gcs_image')

这是出现的错误

> Traceback (most recent call last):   File
> "/Users/santhoshdc/Documents/ImageShapeSize/imageGcloudStorageUpload.py",
> line 13, in <module>
>     blob = bucket.get_blob(gcs_image)   File "/Users/santhoshdc/.virtualenvs/test/lib/python3.6/site-packages/google/cloud/storage/bucket.py",
> line 388, in get_blob
>     **kwargs)   File "/Users/santhoshdc/.virtualenvs/test/lib/python3.6/site-packages/google/cloud/storage/blob.py",
> line 151, in __init__
>     name = _bytes_to_unicode(name)   File "/Users/santhoshdc/.virtualenvs/test/lib/python3.6/site-packages/google/cloud/_helpers.py",
> line 377, in _bytes_to_unicode
>     raise ValueError('%r could not be converted to unicode' % (value,)) ValueError: <_io.BufferedRandom name=7> could not be
> converted to unicode

任何人都可以指导我出了什么问题或我做错了什么吗?

python google-cloud-platform google-cloud-storage opencv3.0
3个回答
6
投票

按照@A.Queue in的建议(29天后删除)

from google.cloud import storage
import cv2
from tempfile import TemporaryFile

client = storage.Client()

bucket = client.get_bucket('test-bucket')
image=cv2.imread('example.jpg')
with TemporaryFile() as gcs_image:
    image.tofile(gcs_image)
    gcs_image.seek(0)
    blob = bucket.blob('example.jpg')
    blob.upload_from_file(gcs_image)

文件已上传,但上传

numpy ndarray
不会在
google-cloud-storage

上保存为图像文件

PS:

numpy array
在保存之前必须转换为任何图像格式。

这相当简单,使用创建的

tempfile
来存储图像,这是代码。

with NamedTemporaryFile() as temp:

    #Extract name to the temp file
    iName = "".join([str(temp.name),".jpg"])

    #Save image to temp file
    cv2.imwrite(iName,duplicate_image)

    #Storing the image temp file inside the bucket
    blob = bucket.blob('ImageTest/Example1.jpg')
    blob.upload_from_filename(iName,content_type='image/jpeg')

    #Get the public_url of the saved image 
    url = blob.public_url

1
投票

你打电话给

blob = bucket.get_blob(gcs_image)
,这毫无意义。
get_blob()
应该获取一个字符串参数,即您想要获取的 blob 的名称。 一个名字。 但是你传递了一个文件对象。

我建议这个代码:

with TemporaryFile() as gcs_image:
    image.tofile(gcs_image)
    gcs_image.seek(0)
    blob = bucket.blob('documentation-screenshots/operations/15.png')
    blob.upload_from_file(gcs_image)

0
投票

上述答案似乎都没有上传格式正确的文件。将图像转换为字节并作为字符串上传对我有用:

def upload_image(self, directory_name: str, image_name: str, image: cv2.Mat):
    image_bytes = cv2.imencode(".jpg", image)[1].tobytes()
    blob = self._bucket.blob("{}/{}.jpg".format(directory_name, image_name))
    blob.upload_from_string(image_bytes, content_type="image/jpeg")
© www.soinside.com 2019 - 2024. All rights reserved.