How to read image of google drive using python - python

I want to read image from google drive and store in binary field.How can I do that?
I tried this code but I got too large image.Another option for reading image from drive?
link = urllib.request.urlopen(image_path).read()
image_base64 = base64.encodestring(link)

Related

How do I mount one Google Drive folder with images only onto Google Colab and iterate through it?

I have a folder of images I extracted for a computer-vision project, but don't know how to mount it onto Google Colab and iterate through the folder. I have a function I need to apply to each image inside the folder, but don't know how to get the images.
I've tried looking for resources but haven't found anything helpful to my situation, because most of them were for unzipping files that were not images. Can you please help me out? Thank you.
You can use the OpenCV library for this.
from google.colab import drive
import os
import cv2
First you need to change the current working directory to your image folder directory.
os.chdir("/content/drive/MyDrive/yourfolder")
You can iterate through every image, apply your function to them and save the final version like this:
for file in os.listdir():
img = cv2.imread(file)
result = myfunction(img)
cv2.imwrite(file, result)

How to read an image from a URL in Colab?

I have seen so many different threads about this topic but none of their solutions seems to work for me. I've tried several ways of reading an image from my Drive into Colab using its URL, with no success. I want to read it using its URL rather than mounting my Drive and using directories because multiple people share this Colab, and their directory to the image might not be the same as mine.
The first attempt comes from a popular thread on this issue: How do I read image data from a URL in Python?
from PIL import Image
import requests
url = 'https://drive.google.com/file/d/1z33YPsoMe0lSNNa2XWa0tiK2571j2tFu/view?usp=sharing'
im = Image.open(requests.get(url).raw) # apparently no need for bytes wrapping in new Python versions
im = Image.open(requests.get(url, stream=True).raw) # also does not work
The error I got was UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7f0189569770>
Then I tried:
from skimage import io
io.imshow(io.imread(url))
Which returned ValueError: Could not find a format to read the specified file in mode 'i'. Feeling very lost because all these approaches seem to work for everyone else. Would appreciate any feedback.
Using gdown to read an image from Google Drive into Colab.
If you have your image in your Google Drive and you are using colab.research.google.com, then you can follow these steps:
pip install gdown
Obtaining the share link from your image (remember to set the option "Share to anyone with the link"). For an example:
# https://drive.google.com/file/d/1WG3DGKAo8JEG4htBSBhIIVqhn6D2YPZ/view?usp=sharing
Extract the id of the share link from the URL 1WG3DGKAo8JEG4htBSBhIIVqhn6D2YPZ.
Download the image in the folder content of a particular user who has access to this Colab:
!gdown --id '1WG3DGKAo8JEG4htBSBhIIVqhn6D2YPZ' --output bird1.jpp
Read the image file from the folder content
from PIL import Image
im = Image.open("../content/bird1.jpg")
im

How to download image from google sheets?

I have image in cell in google sheet and I want to download it using python. It would be best if it could be done using API. I searched the internet, but I haven't found solution.
I have this sheet and I want to download image in cell A1. It cant be even accessed in sheets. The only thing I can do now is copy the cell to word and it copies the image.
Google's sheets API doesn't currently support the download of image files directly through python, sorry.

Uploading Csv FIle Google colab

So I have a 1.2GB csv file and to upload it to google colab it is taking over an hour to upload. Is that normal? or am I doing something wrong?
Code:
from google.colab import files
uploaded = files.upload()
df = pd.read_csv(io.BytesIO(uploaded['IF 10 PERCENT.csv']), index_col=None)
Thanks.
files.upload is perhaps the slowest method to transfer data into Colab.
The fastest is syncing using Google Drive. Download the desktop sync client. Then, mount your Drive in Colab and you'll find the file there.
A middle ground that is faster than files.upload but still slower than Drive is to click the upload button in the file browser.
1.2 GB is huge dataset and if you upload this huge dataset it take time no question at all. Previously i worked on one of my project and i face this same problem. There are multiple ways to handel this problem.
Solution 1:
Try to get your dataset in google drive and start doing your project in google colab. In colab you can mount your drive and just use file path and it works.
from google.colab import files
uploaded = files.upload()
df = pd.read_csv('Enter file path')
Solution 2:
I believe that you used this dataset for a machine learning project. So for developing the initial model, your first task is to check whether your model is working or not so what you do, you just open your CSV file in Excel and copy the first 500 or 1000 thousand rows and paste into another excel sheet and make small dataset and work with that dataset. Once you find everything is working then uploads your full dataset and train your model on it.
This technique is little bit tedious because you have to take care about EDA and Feature Engineering stuff, when you upload entire 1.2 GB dataset. Apart from that everything is fine and it work.
NOTE: This techinique very helpful when your first priority is performing experiment, because loading huge dataset and then start working is very time comsuming process.

GCP - Reading images in GCS into Datalab using Python

I have a store of images in Google Cloud Storage and I am looking to read them into OpenCV in Datalab. I can find information on how to read text files but can't find anthing on how I can read in an image. How would I go about doing this?
I am not really familiar with OpenCV, so let me cover the Datalab ⟷ GCS part and I hope that is enough for you to go on with the OpenCV part.
In Datalab, you can use two different approaches to access Google Cloud Storage resources. They are both documented (with working examples) in these Jupyter notebooks: access GCS using Storage commands ( %%gcs ) or access GCS using Storage APIs ( google.datalab.storage ).
I'll provide an example using Storage commands, but feel free to adapt it to the Datalab GCS Python library if you prefer.
# Imports
from google.datalab import Context
from IPython.display import Image
# Define the bucket and and an example image to read
bucket_path = "gs://BUCKET_NAME"
bucket_object = bucket_path + "/google.png"
# List all the objects in your bucket, and read the example image file
%gcs list --objects $bucket_path
%gcs read --object $bucket_object -v img
# Print the image content (see it is in PNG format) and show it
print(type(img))
img
Image(img)
Using the piece of code I shared, you are able to perform a simple object-listing for all the objects in your bucket and also read an example PNG image. Having its content stored in a Python variable, I hope you are able to consume it in OpenCV.

Categories

Resources