Build a Random Image Fetcher Using Python

If you’re developing or designing a website, you probably need to get hold of some stock images. They can be helpful for prototyping, or even to use in your production app.

You can easily build a random image fetcher using the ever-friendly Python language. You can use it to showcase random visuals in a widget, test for resolution switching, or show off a product recommendation engine.

4

Follow this project to gain hands-on experience with the Requests and Pillow module. They will prove useful for future web work, including image processing.

The Requests and Pillow Modules

The Requests module makes it simple to make HTTP requests and returns a response object that contains data such as encoding, and status. With this, you’re able to develop many interesting applications such as awebsite status checker, web scraper, stock market monitor bot, and website performance tester. To install the Requests module, open the terminal and type:

The Pillow library—a fork of the Python Imaging Library (PIL)—provides image processing capabilities that help in editing, creating,converting file formats, and saving images. It offers broad file format compatibility and a useful internal representation. To install the Pillow module, open the terminal and type:

Polaroid camera and pictures on a wooden surface

How to Build a Random Image Fetcher Using Python

You can find the source code of Random Image Fetcher using Python in thisGitHub Repository.

Import the modules and define a function namedimg_requests()that takes txt as an input parameter. Send a GET method to Unsplash’s API URL and use theformatmethod to fill in the placeholder,{0}, with the value of txt. Save the response of the content in JPG format and open the image so that the user can view it. Finally, close the opened file.

Output of Random Image Fetcher Using Python With Input as Spiderman

Display the different options the program provides for the user. The first four options will fetch an image in HD, Full HD, 2K, or 4K resolution depending on the choice. If the user chooses the fifth option, he has to provide a keyword. Based on it, the program will select a suitable image and save it to the system.

Get the user’s choice, display an appropriate message, and call the img_requests function, passing the appropriate text for their query.

A cake featuring an illustration, in icing, of the Spider-Man character above the words “Happy 5th Birthday”

The fifth option is a bit more complicated. If the user chooses it, ask them to enter their keywords. Add a question mark in front of the keyword and call the function to fetch a random image according to the input.

If the user enters anything else, ask them to provide valid input:

Twitter bird logo designed in 3D atop a plain navy background

Put all the code together and enjoy fetching random pictures in high resolution.

Output of the Random Image Fetcher

On running the program above, the program displays five options. On choosing any of the options, the program saved an image and displays it on the screen.

If you choose option 5 and enter the keyword Spider-Man, the program fetched the following Spider-Man image from Unsplash.

Web Scraping Using Python

Many sites provide useful APIs, like Unsplash’s random image fetcher. But for those that don’t, it’s possible to always resort to web scraping, and the Requests module can help.

Other useful modules include Beautiful Soup, Selenium, Scrapy, Urllib, and Mechanize. You can use web scraping to extract information from any website, store it, and analyze it according to your requirements.

Some of the applications you can develop using this technique include a news scraper, a price tracker, and an image downloader. Web Scraping is also extensively used in Natural Language Processing to train models and perform sentiment analysis.

Scraping HTML can be fiddly, so make sure you understand this process with a bit of Python practice.

Your phone is a better editor than you give it credit for.

I gripped my chair the entire time—and then kept thinking about it when the screen turned off.

Quality apps that don’t cost anything.

My foolproof plan is to use Windows 10 until 2030, with the latest security updates.

When your rival has to bail out your assistant.

Technology Explained

PC & Mobile