Created
October 27, 2019 15:50
-
-
Save Ze1598/1b75c33578833283027c03a2434e6c6e to your computer and use it in GitHub Desktop.
Download images from the web with Python using the Pillow and the requests libraries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from PIL import Image | |
from io import BytesIO | |
# Target image URL | |
url = "https://i.imgur.com/EdAGGFS.jpg" | |
# Get the text after the last slash in the URL, that is, the file name,\ | |
# which includes its extension | |
file_name = url.split("/")[-1] | |
# Make a GET request for the URL | |
img_request = requests.get(url) | |
# Parse the content of the request to an in-memory binary stream and then\ | |
# open that stream as an Image of the Pillow library | |
img = Image.open(BytesIO(img_request.content)) | |
# Now that the request's content has been "transformed" to an image, just\ | |
# save it as a new file in your computer | |
img.save(file_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment