Skip to content

Instantly share code, notes, and snippets.

@Ze1598
Created October 27, 2019 15:50
Show Gist options
  • Save Ze1598/1b75c33578833283027c03a2434e6c6e to your computer and use it in GitHub Desktop.
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
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