Reading image from URL in Python

Learn how to read an image from URL in Python with this tutorial.

Many times a situation may arrive where you need to download images instantly from the internet, and not only one image there are a bunch of images, In this doing manual copy and pasting can be Boring and time-consuming task to do, You need a reliable and faster solution to this task.

So, In this tutorial, we will be learning how to read and download images using ‘URL’ in Python. Here we will be using Firstly, the ‘sys’ module so that we can give input URL directly on the command line while running our program. Second, we will be using the ‘Pillow’ library for opening an image as an object and lastly, the most important one is the ‘Requests’ Library of Python for opening and downloading the image from a specified URL.

 About Requests Library

Requests library is used for processing HTTP requests in python. This library allows request methods like get, put, post, delete, etc. Some of the features that are supported by the library are:

  •  Automatic URL formation consisting of post data using ‘urllib3’.
  •  International Domains and URLs can be accessed.
  •  SSL Certificate verification.
  •  Both Basic and Digest Authentication.
  •  Unicode Response bodies.
  •  Returns cookies in as Dictionary.

To learn More about Requests Library you read its detailed documentation.

How to read an image from URL in Python

Before we start with the actual code first we need to install required libraries or modules.

Installation:

$ pip3 install pillow
$ pip3 install requests

After installation, we can start with the code. I would suggest you first get through the code then I’ll be explaining to you the important things.

Source Code:

# Importing Required Modules
import sys
import requests
from PIL import Image

# Exception Handling for invalid requests
try:
        # Creating an request object to store the response
        # The URL is refrenced sys.argv[1]
  ImgRequest = requests.get(sys.argv[1])

        # Verifying whether the specified URL exist or not
  if ImgRequest.status_code == requests.codes.ok:

                # Opening a file to write bytes from response content
                # Storing this onject as an image file on the hard drive
    img = open("test.jpg","wb")
    img.write(ImgRequest.content)
    img.close()

                # Opening Inage file using PIL Image
    img = Image.open("test.jpg")
    img.show()
  else:
    print(ImgRequest.status_code)

except Exception as e:
  print(str(e))

Now here we will be supplying the image URL as command line argument which would we referenced later by the sys.argv object. After the request has been made the response status code is verified whether it is in the codes range (>200 & <=400). Once the status code is verified then the response content would be written into a binary file and saved as an image file. Later we open it using Pil Module for taking a view.

Input:

$ python3 request.py https://www.fujifilm.co.nz/products/digital_cameras/x/fujifilm_x_t1/sample_images/img/index/ff_x_t1_001.JPG

Here ‘request.py’ is the python source file.

Output:

How to read image from URL in Python

So In this way, you can read an image from URL using Python. I hope this tutorial was fruitful to you, Thank you ‘Keep Learning Keep Coding’.

Leave a Reply

Your email address will not be published. Required fields are marked *