Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将ndarray(OpenCV中的图像)以.jpg或.png格式上载到google云存储_Python_Google Cloud Platform_Google Cloud Storage_Opencv3.0 - Fatal编程技术网

Python 将ndarray(OpenCV中的图像)以.jpg或.png格式上载到google云存储

Python 将ndarray(OpenCV中的图像)以.jpg或.png格式上载到google云存储,python,google-cloud-platform,google-cloud-storage,opencv3.0,Python,Google Cloud Platform,Google Cloud Storage,Opencv3.0,我也有类似的问题 我试过这个 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]/ bu

我也有类似的问题

我试过这个

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
>回溯(最近一次调用上次):文件
>“/Users/santhoshdc/Documents/ImageShapeSize/imageGcloudStorageUpload.py”,
>第13行,在
>blob=bucket.get_blob(gcs_image)文件“/Users/santhoshdc/.virtualenvs/test/lib/python3.6/site packages/google/cloud/storage/bucket.py”,
>第388行,在get_blob中
>**kwargs)文件“/Users/santhoshdc/.virtualenvs/test/lib/python3.6/site packages/google/cloud/storage/blob.py”,
>第151行,in_uuuinit__
>name=_bytes_to_unicode(name)文件“/Users/santhoshdc/.virtualenvs/test/lib/python3.6/site packages/google/cloud/_helpers.py”,
>第377行,单位为字节到unicode
>raise VALUERROR(“%r”无法转换为unicode“%”(值,))VALUERROR:无法转换
>转换为unicode

有谁能告诉我哪里出了问题,或者我做的不对吗?

您正在调用
blob=bucket。获取blob(gcs\u图像)
,这毫无意义
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)
根据@A.Queue的建议(29天后删除)

文件已上载,但上载的
numpy-ndarray
不会保存为
google云存储上的图像文件

附言:

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

这相当简单,使用创建的
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

Traceback(最近一次调用):文件“/Users/santhoshdc/Documents/ImageShapeSize/imageGcloudStorageUpload.py”,第34行,blob2.upload_from_filename(filename='gcs_image')文件“/Users/santhoshdc/.virtualenvs/test/lib/python3.6/site packages/google/cloud/storage/blob.py”,第1021行,upload_from_filename,打开(文件名'rb')作为文件\u obj:FileNotFoundError:[Errno 2]没有这样的文件或目录:“gcs\u image”
。这是我在删除这三条线条时遇到的错误。
filename='gcs\u image'
没有任何意义。我将在我的答案中添加我认为可能有用的代码。等等。@PrashantPanwar我添加了一些示例代码,应该可以实现这一点。该程序似乎运行良好,没有错误,但没有任何错误le正在上传到bucket中。尝试通过终端中的
gsutil
上传图像似乎是在上传图像。不知道哪里出了问题?尝试运行此代码。基本相同,但没有
google.auth
,这在这里是不必要的,因为google.cloud正在后台处理所有事情。这将是一个good参考点,因为我已经尝试过,并且它有效。@A.Queue请调查这一问题最终解决了吗?我问这个问题是因为在另一个问题中,您写道这是路径问题,但在这里您说它不可读。@A.Queue是的,问题解决了。
numpy array
直接上载不会存储在image fo中因此无法读取rmat。我已更新了
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