This is the code to save the photo below to the drive.
https://www.pakutaso.com/shared/img/thumb/nyannko458A3685_TP_V.jpg
As a prerequisite, the code to be executed in Colab, If you do it with Python, you need to set OAuth with GCP.
Certification with Colab
from google.colab import auth
auth.authenticate_user()
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from oauth2client.client import GoogleCredentials
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
Save image as jpeg
import requests
from io import BytesIO
from PIL import Image
url = 'https://www.pakutaso.com/shared/img/thumb/nyannko458A3685_TP_V.jpg'
file_name = 'cat.jpg'
FOLDER_ID = '' #Specify the folder ID you want to save
r = requests.get(url)
i = Image.open(BytesIO(r.content))
i = i.resize(size=(200, 100)) #Used when you want to resize
i.save(file_name)
#UPLOAD by specifying ID
f = drive.CreateFile({'title' : file_name, 'parents':[{'id' : FOLDER_ID }]})
f.SetContentFile(file_name)
f.Upload()
You can check the image by executing i in another cell.
If you want to save other than jpeg, change the extension of the file name and it will change. A list of supported formats can be found below. https://pillow.readthedocs.io/en/latest/handbook/image-file-formats.html
Note that if you forget to add the extension to the file name, the following error will occur.
https://pillow.readthedocs.io/en/latest/handbook/tutorial.html#using-the-image-class
Recommended Posts