Download Images from Web Using Python

Python is a quite simple and powerful programming language in the sense that it can be applied to so many areas like Scientific Computing, Natural Language Processing but one specific area of application of Python which I found quite fascinating is => Using Python for dealing with Files and FileSystem in an Operating System.

In this article, I’ll discuss How you can use Python for downloading images from Web? Every unique thing on web have a URL address(representing where that file is located in Web) so using Python you need to send a Request to that Server. For sending request to a server, Python’s Requests module can be used which will get file from Server and will return it as an Request Object containing file. File from returned Request Object can be retrieved using content attribute. That’s it quite simple process as Python have already done most of work for us Developers. 😂 😂

Anyway, let’s put this together as Python Code for downloading images from Web.

# Import os and requests Python Modules
import os
import requests as reqs

# Sending request to Server for Image
download_image = reqs.get(
    'https://upload.wikimedia.org/wikipedia/commons/thumb/c/c3/Python-logo-notext.svg/165px-Python-logo-notext.svg.png')

# Saving downloaded image into current working directory
# Means Saving image to same folder as Python Code file is in
with open(os.path.join(os.getcwd(), 'downloaded_image.png'), 'wb') as f:
    f.write(download_image.content)

Above code will download Python Logo from Wikipedia.

Quite Simple Python Code, easy to write and just pasting in URL and boom image will be downloaded and saved into current working directory.
I hope that you find this article useful, if in case you have some question then let me know in comments. I would try to reply as soon as possible.
Happy Coding 🥳 🥳

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

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

Recent Posts