DEV Community

Cover image for Python Mini Projects
Shittu Olumide
Shittu Olumide

Posted on

Python Mini Projects

Learning programming can be challenging, especially for beginners diving into the vast world of coding. The syntax, logic, and problem-solving aspects can sometimes feel overwhelming, leading many aspiring developers to question their abilities.

However, a proven method for overcoming these hurdles and accelerating your learning curve is through practical application and hands-on projects. With its simplicity and versatility, Python offers an excellent platform for embarking on these mini projects that reinforce your understanding and provide a tangible sense of accomplishment.

In this article, we will explore some mini Python projects that you can follow up on and build in this article. These projects can help solidify your understanding of fundamental concepts, enhance your problem-solving skills, and foster a deep sense of creativity and innovation. Whether you're a beginner aiming to build a solid foundation or an intermediate learner seeking to expand your Python proficiency, embracing mini projects can be a transformative step in your programming journey.

File encryption and decryption

In terms of file encryption, encrypting a message so that only the intended audience can view it. We encrypt data to safeguard data security because we don't want anyone to access it.

It would be difficult to encrypt (and decode) files by hand, but we can do so using Python's "cryptography" module.

Code example:

def encrypt(message, key):
    encrypted_message = ""
    for char in message:
        if char.isalpha():  # Check if the character is a letter
            ascii_offset = ord('A') if char.isupper() else ord('a')
            shifted = (ord(char) - ascii_offset + key) % 26 + ascii_offset
            encrypted_message += chr(shifted)
        else:
            encrypted_message += char  # Keep non-alphabetic characters as is
    return encrypted_message

def decrypt(encrypted_message, key):
    decrypted_message = ""
    for char in encrypted_message:
        if char.isalpha():  # Check if the character is a letter
            ascii_offset = ord('A') if char.isupper() else ord('a')
            shifted = (ord(char) - ascii_offset - key) % 26 + ascii_offset
            decrypted_message += chr(shifted)
        else:
            decrypted_message += char  # Keep non-alphabetic characters as is
    return decrypted_message

# Example usage
message = "Hello, World!"
key = 3

# Encryption
encrypted = encrypt(message, key)
print("Encrypted message:", encrypted)

# Decryption
decrypted = decrypt(encrypted, key)
print("Decrypted message:", decrypted)
Enter fullscreen mode Exit fullscreen mode

Explanation of the code:

  • We define two functions: encrypt and decrypt, which take a message and a key as input parameters.

  • The encrypt function iterates over each character in the message using a for loop.

  • Inside the loop, we check if the character is alphabetic using the isalpha() method.

  • We determine the ASCII offset based on the case of the letter (A or a).

  • The character is then shifted by the key value using the formula shifted = (ord(char) - ascii_offset + key) % 26 + ascii_offset. This formula wraps the shifted value around within the range of alphabetic characters (26 characters in total).

  • The shifted character is converted back to its ASCII representation using chr() and added to the encrypted_message string.

  • Non-alphabetic characters are simply added to the encrypted_message without modification.

  • The encrypt function returns the encrypted message.

  • The decrypt function follows a similar logic to the encrypt function, but the shift is done in the opposite direction (subtracting the key value).

  • Example usage demonstrates how to encrypt and decrypt a message.

  • The encrypted message is printed.

  • The decrypted message is printed.

Photo background remover

To remove the background from your photos, various internet tools and software are accessible. However, I personally find it difficult to trust these programs since I'm not sure where my photos are kept when they are used with third-party software. Building your own background removal tool would thus be a fantastic idea.

!pip install rembg
from google.colab import files
uploaded = files.upload()

## https://pixabay.com/photos/animal-mammal-fallow-deer-stag-984573/
from rembg import remove 
from PIL import Image

input_img = 'deer.jpg'
output_img = 'deer_rmbg.png'

inp = Image.open(input_img)
output = remove(inp)

output.save(output_img)
files.download(output_img) 
Enter fullscreen mode Exit fullscreen mode

If you run this code, you will get this output:

Image description

Make your own animations

To turn your picture into animations, you can use the algorithm called AnimeGANv2, a deep learning algorithm that transforms real-world scenes into anime-style images. The technical nitty-gritty is beyond the scope of this blog, but if you’d like to learn more, feel free to check out this GitHub.

To turn our pictures into works of art, we will call the pre-trained models from the PyToch hub.

rom PIL import Image
import torch
import IPython
from IPython.display import display
# upload images
from google.colab import files
uploaded = files.upload()

# https://github.com/bryandlee/animegan2-pytorch
# load models and face2paint utility function
model_facev2 = torch.hub.load("bryandlee/animegan2-pytorch:main", "generator", pretrained="face_paint_512_v2")
face2paint = torch.hub.load("bryandlee/animegan2-pytorch:main", "face2paint", size=512)

for INPUT_IMG in ['KatLi.JPG']:  
  img = Image.open(INPUT_IMG).convert("RGB")
  out_facev2 = face2paint(model_facev2, img)

  # display images
  display(img)
  display(out_facev2)
Enter fullscreen mode Exit fullscreen mode

In this code, we first upload some images, then we load the models using the torch.hub.load() and call the face2paint function.

Viola! we have the animation, and the most fun part is that you can add more photos of your family and friends to create animations in batches to share with them!

Image description

YouTube video downloader

Sometimes you watch a video on YouTube, Which can be a tutorial, a comedy, or even a documentary, and you want to have it downloaded on your computer. Here is where the Python module {pytube} comes in handy.

from pytube import YouTube

def DownloadYT(url):
    youtubeObject = YouTube(url)
    youtubeObject = youtubeObject.streams.get_highest_resolution()
    try:
      youtubeObject.download()
      print("Title:", youtubeObject.title)
      print("=== Download is completed successfully ===\n")
    except:
        print("An error has occurred") 

urls = ['https://www.youtube.com/watch?v=ZnrjyEuBJcg&t=14s', 
        'https://www.youtube.com/watch?v=e_B0vt-eWPw&t=158s']

for url in urls:
  DownloadYT(url)
Enter fullscreen mode Exit fullscreen mode

As you can see, to download the video in its best quality, all we need is to specify the get_highest_resolution() function and call download().

So with these four mini projects, you should clearly understand the structure and how to build some projects with Python.

Top comments (0)