Download any Image from URL in Python
With Python you can download any image from URL in a cool programming way. Python has a great module called the urllib.request which will help you to download any image from the web with its URL, in just a click.Hello Everyone,
Today i will show you how to download any image from URL in Python. First, let's have a look at the script which will download image from the web and i will explain what exactly each line of code is doing.
Image_downloader.py
import urllib.request
The first thing we have to do is to import necessary modules. For the purpose of downloading image from URL, we require a single module called the urllib.request. Basically urllib is a package that collects several modules for working with URLs. You can directly install it using pip by typing following command: pip install urllib. You can find its documentation here. The urllib.request module will help you to make a request with a URL.
url = str(input('Enter Url'))
filename = input('Enter File Name')
These lines of code will take user inputs and will store it to the corresponding variables. After running the script you just need to paste the URL of the image to be downloaded and provide a filename for it.
def downloader(image_url,file_name):
Here we defined a downloader function that will take the user inputs as its arguments.
final_file_name = str(file_name) + '.jpg'
urllib.request.urlretrieve(image_url,final_file_name)This will convert the filename that the user has provided to string and we will add an image extension at the end of this, finally store in a variable final_file_name.
The urllib.request.urlretrieve function will take two arguments. The first argument is the URL of the image from where it can be downloaded and the second argument is the filename with which you wish to store the image. You can also specify the local file path where you want to store the downloaded image like this:
urllib.request.urlretrieve(url_image, '/Users/techy/Downloads/car.jpg').I'm currently working in Ubuntu 18.04 LTS equipped with Python 3.6.7 and wants to download the image directly to my home folder. So no need to specify the path.
try:downloader(url,filename)
print('Done')
The try block lets you test this block of code for errors. This will call the downloader function and the image will be downloaded to your current working directory with the provided filename. After successfully saving the image in your hard disk it will print 'Done'.
except Exception as Error:
print("Can't Download Image")
print(Error)
In case of any error, the except block lets you handle that. Basically, It will print the error. One most common situation in which this script fails is, when you are trying to visit a website using a Python program or any other programming language, the website owners will block you if they sense you are not a real user. If this is the case, our program will print something like this: "HTTP Error 403: Forbidden"
Hey, don't worry friends if you got such an error. Because it is something which can be easily solved. I will show you exactly how you can bypass this error with some simple changes in the script, in my very next post. So stay tuned.
![]() |
| Screenshot Attached |
Hope it helps newbies to Python.
Thank you and have a wonderful weekend.
How to Download an Image from URL in Python
Reviewed by Cyril Tom Mathew
on
April 24, 2019
Rating:
Reviewed by Cyril Tom Mathew
on
April 24, 2019
Rating:


No comments: