2

Say I have a picture titled sunset.jpg stored at the following URL on google cloud storage gs://example-bucket/testing_data

so the full URL for the image is:

gs://example-bucket/testing_data/sunset.jpg

If I then do something like:

image = cv2.imread('gs://example-bucket/testing_data/sunset.jpg')

But while this doesn't crash or fail no image is loaded. How do I access/provide the right URL to cv2.imread to do this??

sometimesiwritecode
  • 2,993
  • 7
  • 31
  • 69

1 Answers1

5
import cv2
import numpy as np
import urllib

url = "https://i.stack.imgur.com/AVWX7.jpg";
resp = urllib.urlopen(url)
image = np.asarray(bytearray(resp.read()), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)

To convert gs to normal URL.

bucket = 'example-bucket'
file = 'sunset.jpg'

gcs_url = 'https://%(bucket)s.storage.googleapis.com/%(file)s' % {'bucket':bucket, 'file':file}

print gcs_url
VK321
  • 5,768
  • 3
  • 44
  • 47
  • Sadly this doesn't work. My url for the image is not just any url but a google cloud storage url with the format "gs://" so when I try and do this suggestion is crashes and says IOError: [Errno url error] unknown url type: 'gs' – sometimesiwritecode May 26 '17 at 23:36
  • @sometimesiwritecode another option is using https://github.com/Othoz/gcsfs – Narek Apr 28 '20 at 04:01
  • did this actually work for you? I tried this and am still having trouble reading the image – Nico Mar 21 '22 at 15:14