Simplest way to download an image from it’s URL using Python wget module’s download method.
Contents
Prerequisites:
1. Install wget library
Syntax:
get.download(URL, Target Directory (optional))
Parameters:
URL – Image URL Example: https://gethowstuff.com/wp-content/uploads/2020/03/python-crash-course-001.jpg
Target Directory (optional) – Path where downloaded image will be saved. Otherwise, it will be saved to current working directory (the python script’s folder)
Also Read: download Instagram Images using Python
Examples
Code#1 : Download image to current working directory
import wget import os img = input("enter image URL \n") #Example:https://gethowstuff.com/wp-content/uploads/2020/03/python-crash-course-001.jpg # Calling wget.download method img = wget.download(img) # Printing Filename print(img) # Printing file path print(os.getcwd() + "\\" + img)
Output:
python-crash-course-001 (2).jpg
C:\Users\PycharmProjects\example\venv\Scripts\python-crash-course-001 (2).
Code#2: Download image to specific directory
import wget import os img = input("enter image URL \n") # downloading image img = wget.download(img, "C:\\TEMP") # printing downloaded image file path print(img)
Output:
enter image URL
https://gethowstuff.com/wp-content/uploads/2020/03/python-crash-course-001.jpg
C:\TEMP/python-crash-course-001 (1).jpg
References: Python wget.download() documentation
Notes: Above codes are validated with Python 3.7 on PyCharm 2019.3.3 on Windows 10 PC