Save images on the web to Drive with Python (Colab)

This is the code to save the photo below to the drive.

https://www.pakutaso.com/shared/img/thumb/nyannko458A3685_TP_V.jpg 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.

Pass drive authentication

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 the image

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

If you get an error

Note that if you forget to add the extension to the file name, the following error will occur.

2020-09-26_11h03_41.png

What you can do with PIL

https://pillow.readthedocs.io/en/latest/handbook/tutorial.html#using-the-image-class

Recommended Posts

Save images on the web to Drive with Python (Colab)
Download files on the web with Python
Upload images to Google Drive with Python
Introduction to Python with Atom (on the way)
[Part.2] Crawling with Python! Click the web page to move!
The fastest way to get camera images regularly with python opencv
Function to save images by date [python3]
The road to compiling to Python 3 with Thrift
[Python] Save the result of web scraping the Mercari product page on Google Colab to Google Sheets and display the product image as well.
[Python] I tried to visualize the night on the Galactic Railroad with WordCloud!
Put Cabocha 0.68 on Windows and try to analyze the dependency with Python
Add 95% confidence intervals on both sides to the diagram with Python / Matplotlib
Use python on Raspberry Pi 3 to light the LED with switch control!
I tried with the top 100 PyPI packages> I tried to graph the packages installed on Python
Strategy on how to monetize with Python Java
Convert PDFs to images in bulk with Python
I tried to save the data with discord
Vienna with Python + Flask web app on Jenkins
Specify the Python executable to use with virtualenv
Say hello to the world with Python with IntelliJ
The easiest way to use OpenCV with python
Save the object to a file with pickle
Upload files to Google Drive with Lambda (Python)
Save the search results on Twitter to CSV.
Connect to MySQL with Python on Raspberry Pi
Embed other images on the raster with ArcPy
Think about how to program Python on the iPad
Try to solve the programming challenge book with python3
[Introduction to Python] How to iterate with the range function?
I tried to implement Minesweeper on terminal with python
Try to solve the internship assignment problem with Python
The first algorithm to learn with Python: FizzBuzz problem
I tried to touch the CSV file with Python
Steps to install the latest Python on your Mac
I tried to solve the soma cube with python
Yum command to access MySQL with Python 3 on Linux
[Python] How to specify the download location with youtube-dl
Easy way to scrape with python using Google Colab
Make a breakpoint on the c layer with python
Convert the image in .zip to PDF with Python
Information for controlling the motor with Python on RaspberryPi
I want to AWS Lambda with Python on Mac!
Specify MinGW as the compiler to use with Python
I tried to solve the problem with Python Vol.1
[Python] How to rewrite the table style with python-pptx [python-pptx]
How to enjoy Python on Android !! Programming on the go !!
Extract images and tables from pdf with python to reduce the burden of reporting
An easy way to pad the number with zeros depending on the number of digits [Python]
Make it easy to install the ROS2 development environment with pip install on Python venv
[For beginners] Web scraping with Python "Access the URL in the page to get the contents"
Starting with Python 3.10, the form returned by inspect.signature () seems to be based on typing.get_type_hints ().
How to deal with the phenomenon that Python (Jupyter notebook) executed on WSL becomes Aborted
I tried to find the entropy of the image with python
Connect to BigQuery with Python
Save images using python3 requests
I tried to simulate how the infection spreads with Python
Web scraping with python + JupyterLab
How to install Python2.7 python3.5 with pyenv (on RHEL5 CentOS5) (2016 Nov)
Test.py is not reflected on the web server in Python3.
Memo to get the value on the html-javascript side with jupyter
I wanted to solve the Panasonic Programming Contest 2020 with Python