3

Is there any elegant way of displaying an image located in a non-public drive folder, beside downloading and displaying it using some python library?

Here my current solution:

import matplotlib.pyplot as plt
import cv2
import numpy as np
import io

from google.colab import files
from googleapiclient.http import MediaIoBaseDownload
from googleapiclient.discovery import build
from google.colab import auth
from oauth2client.client import GoogleCredentials
from urllib.request import urlopen

# Connect to Google Drive
auth.authenticate_user()

# Open image file as BGR Mat 
def gdrive_open_image(file_id):
  # Get byte stream
  data = gdrive_open_file(file_id)
  # Decode the array into an image
  return cv2.imdecode(np.fromstring(data, dtype='uint8'), cv2.IMREAD_COLOR)


# Open arbitary file as bytestream
def gdrive_open_file(file_id):
  drive_service = build('drive', 'v3')
  request = drive_service.files().get_media(fileId=file_id)
  stream = io.BytesIO()
  downloader = MediaIoBaseDownload(stream, request)
  done = False
  while done is False:
    status, done = downloader.next_chunk()

  return stream.getvalue()

# Display Image
plt.imshow(cv2.cvtColor(gdrive_open_image('some_file_id'), cv2.COLOR_BGR2RGB), aspect='equal', interpolation='bicubic')
plt.axis('off')
plt.show()
Florian Pilz
  • 337
  • 1
  • 12
  • Take a look to this post [https://stackoverflow.com/questions/48896573/link-to-images-within-google-drive-from-a-colab-notebook](https://stackoverflow.com/questions/48896573/link-to-images-within-google-drive-from-a-colab-notebook) – Nahuel Feb 23 '18 at 00:21

0 Answers0