How to Download Image from URL using Python?

By Hardik Savani October 30, 2023 Category : Python

Hey Folks,

In this tutorial, we will go over the demonstration of python download image from url and save. This post will give you a simple example of how to download image from url using python. you can see python download image from url.

There are several ways to download image from url in python. we will use urllib and requests library to download image from link. urllib library urlretrieve method will use and requests library get method will use. so let's see the both example code:

Example 1:

main.py

import urllib.request
  
urllib.request.urlretrieve("https://www.itsolutionstuff.com/upload/itsolutionstuff.png", "demo.png")

Example 2:

main.py

import os
import requests
   
imageURL = 'https://www.itsolutionstuff.com/upload/itsolutionstuff.png'
page = requests.get(imageURL)
  
f_ext = os.path.splitext(imageURL)[-1]
f_name = 'img{}'.format(f_ext)
with open(f_name, 'wb') as f:
    f.write(page.content)

I hope it can help you...

Tags :
Shares