01 logo

Python Program To Download Profile Picture (DP) Of Your Instagram ID

A Python Script to Download Instagram Profile Pictures (DPs) Directly to Your Computer

By jeet mundadaPublished 2 years ago 3 min read
Python Program To Download Profile Picture (DP) Of Your Instagram ID
Photo by Mohammad Rahmani on Unsplash

Hi everyone, are you curious about how you can download your Instagram profile picture (DP) using Python? In this tutorial, we’ll walk through a simple Python script that utilizes the Instaloader library to accomplish this task. By the end of this guide, you’ll be able to fetch your Instagram profile picture effortlessly. Let's Get Started!!!

PS: All the lines in the blockquote are part of the code. I have also put the entire code at the end of the post. You can copy and paste that into your Python editor. If you encounter any errors, make sure the code is copied correctly. If that doesn’t resolve the issue, check the indentation.

----------------------------------------------------------------------------------------

Before diving in the coding, we need to ensure that a library named “Instaloder” is installed in your computer for proper functioning of the program otherwise you will face errors. To install the library follow the following steps:

1. Go in the command prompt or terminal if your using Windows or MacOs respectively.

2. If you are using Windows, type ‘pip install instaloader’ in your command prompt.

3. If you are using MacOS, type 'pip3 install instaloader' in your terminal.

4. Let the downloading finish.

5. You can write the code now.

----------------------------------------------------------------------------------------

Now follow the following steps to complete the code:

  1. Create a new Python file and save it in a appropriate folder.
  2. Import the following necessary modules:

import instaloader

import glob

import os

import flet as ft

3. Next, define the main function where the core functionality of downloading the profile picture will reside:

def main(page):

def btn_click(e):

if not txt_name.value:

txt_name.error_txt = "Please enter a username"

page.update()

else:

username = txt_name.value

obj = instaloader.Instaloader()

obj.download_profile(username, profile_pic_only=True)

path = os.getcwd()

user_path = path + f"/{username}"

print(user_path)

file_path = glob.glob(f"{user_path}/*.jpg")

for i in file_path:

page.add(ft.Image(src=i, width=300, height=300))

page.update()

txt_name = ft.TextField(label="Username")

page.add(txt_name, ft.ElevatedButton("View Image", on_click=btn_click))

page.update()

ft.app(target=main)

----------------------------------------------------------------------------------------

The explanation of the program are as follows:

1. Main Function:

def main(page):

This is the main function of the program, which is called when the application starts. It takes 'page' as an argument, which presumably represents the GUI page where elements are added.

2. Button Click Event:

def btn_click(e):

if not txt_name.value:

txt_name.error_txt = "Please enter a username"

page.update()

else:

username = txt_name.value

obj = instaloader.Instaloader()

obj.download_profile(username, profile_pic_only=True)

path = os.getcwd()

user_path = path + f"/{username}"

file_path = glob.glob(f"{user_path}/*.jpg")

for i in file_path:

page.add(ft.Image(src=i, width=300, height=300))

page.update()

  • This function is triggered when the button is clicked.
  • It checks if a username is entered in the text field. If not, it displays an error message.
  • If a username is provided, it uses Instaloader to download the profile picture.
  • It then gets the current working directory and constructs the path for the downloaded images.
  • Using glob.glob(), it retrieves the path of all JPG files in the user's directory.
  • Finally, it adds the profile pictures to the GUI page.

3. Creating GUI Elements:

txt_name = ft.TextField(label="Username")

page.add(txt_name, ft.ElevatedButton("View Image", on_click=btn_click))

page.update()

  • Here, we create a text field (txt_name) where the user can enter the username.
  • An elevated button labeled “View Image” is added next to the text field.
  • The on_click event of the button is connected to the btn_click function defined earlier.
  • These elements are added to the GUI page (page).

4. Launching the Application:

ft.app(target=main)

  • This line launches the Flet application, with the main function as the target.

----------------------------------------------------------------------------------------

Here is the whole code all together:

import flet as ft

import instaloader

import glob

import os

def main(page):

def btn_click(e):

if not txt_name.value:

txt_name.error_txt = "Please enter a username"

page.update()

else:

username = txt_name.value

obj = instaloader.Instaloader()

obj.download_profile(username, profile_pic_only=True)

path = os.getcwd()

user_path = path + f"/{username}"

print(user_path)

file_path = glob.glob(f"{user_path}/*.jpg")

for i in file_path:

page.add(ft.Image(src=i, width=300, height=300))

page.update()

txt_name = ft.TextField(label="Username")

page.add(txt_name, ft.ElevatedButton("View Image", on_click=btn_click))

page.update()

ft.app(target=main)

----------------------------------------------------------------------------------------

With just a few lines of Python code, you’ve learned how to create a simple GUI application to download and display Instagram profile pictures. Whether you’re a beginner or an experienced Python developer, this project demonstrates the power and versatility of Python for various tasks. If you have any doubts regarding the code or have not understood any part of the code, feel free to write it in the comments. If you enjoyed this tutorial and want to explore more Python projects, feel free to follow me for future code snippets and tutorials. Happy coding!

Photo by Pete Pedroza on Unsplash

how tosocial mediatech news

About the Creator

Enjoyed the story? Support the Creator.

Subscribe for free to receive all their stories in your feed.

Subscribe For Free

Reader insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment
    Written by jeet mundada