Beginners when start programming often get bored if they do not get a chance to play with some interesting code. So, in this article, I have explained three python projects with Code. Beginner programmers can try to implement these projects and get their hands dirty in the Python language.
Let’s start with the first one.
1. QR Code Generation using Python
QR code stands for Quick Response Code. QR codes may look simple but they are capable of storing lots of data. Irrespective of how much data they contain when scanned QR code allows the user to access information instantly. That is why they are called Quick Response Code.
These are being used in many scenarios these days. It first appeared in Japan in 1994. QR codes can be used to store(encode) lots of data and that too of various types. For example, they can be used to encode:
- Contact details
- Facebook ids, Instagram ids, Twitter ids, WhatsApp ids, and more.
- Event Details
- Youtube links
- Product details
- Link directly to download an app on the Apple App Store or Google Play.
- They are also being used in doing digital transactions by simply scanning QR codes.
- Access Wi-Fi by storing encryption details such as SSID, password, and encryption type.
This list goes on….!
Image Source: https://www.assetinfinity.com/
We just saw some advantages of QR codes. Now we will learn here how we can generate QR codes in Python.
For QR code generation using python, we are going to use a python module called QRcode.
Install it using this command: pip install qrcode
We will generate a QR Code for encoding the youtube link and we will also explore more. QR code generation is simple. Just pass the text, link, or any content to ‘make’ function of QRcode module.
import qrcode img = qrcode.make("https://www.youtube.com/") img.save("youtubeQR.jpg")
On executing this code output is:
You can scan it and verify.
You can see it’s just 3 lines of code to generate this QR Code. One more thing to mention is that it’s not necessary that you always have to give a link to qrcode.make() function. You can provide simple text also.
You can encode: India is a country with many religions. I love India.
Let’s try it out:
import qrcode img = qrcode.make("India is a country with many religions. I love India.") img.save("youtubeQR.jpg")
Output QR Code for this text is:
Scan it from your mobile and you will get the content.
So this is the one part, which involves generating a QR Code and scanning it. But what if we want to read this QR Code i.e., now we want to know what was encoded in the QR Code without scanning it. For this, we will use OpenCV. OpenCV is a library of programming functions focused on real-time computer vision tasks.
Install opencv: pip install opencv-python
Code to decode a QR code back to know the original string.
import cv2 d = cv2.QRCodeDetector() val, _, _ = d.detectAndDecode(cv2.imread("myQRCode.jpg")) print("Decoded text is: ", val)
India is a country with many religions. I love India.
This QRcode module in python provides many other functionalities. Go and try those yourself by reading documentation. It will be fun and amazing for you.
2. GUI application for Calendar with Python using Tkinter
In Python, we can make GUIs using Tkinter. If you are very imaginative and creative you can do amazing stuff with Tkinter. Here, we will make a Python Calendar GUI application using Tkinter. In this app, the user has to enter the year for which he wants to view the calendar, and then the calendar will appear.
Install Tkinter first using this command: pip install tk
We would also need a Calendar package, but we don’t have to install it. It is a default package that automatically comes with python.
#import calendar module import calendar #import tkinter module from tkinter import *
#This function displays calendar for a given year def showCalender(): gui = Tk() gui.config(background='grey') gui.title("Calender for the year") gui.geometry("550x600") year = int(year_field.get()) gui_content= calendar.calendar(year) calYear = Label(gui, text= gui_content, font= "Consolas 10 bold") calYear.grid(row=5, column=1,padx=20) gui.mainloop()
ShowCalender function is displaying the calendar. You enter a year in the search box and when enter is pressed, how the calendar should be displayed is managed here. You set the background color which is grey here and can be changed in code as per your need. You also set the dimension of the calendar which is 550×600 here. Then you ask for input year in integer form. Once the user enters the year calendar content is fetched from the calendar module of python by passing the year as an argument.
#Driver code if __name__=='__main__': new = Tk() new.config(background='grey') new.title("Calender") new.geometry("250x140") cal = Label(new, text="Calender",bg='grey',font=("times", 28, "bold")) #Label for enter year year = Label(new, text="Enter year", bg='dark grey') #text box for year input year_field=Entry(new) button = Button(new, text='Show Calender',fg='Black',bg='Blue',command=showCalender)
#adjusting widgets in position cal.grid(row=1, column=1) year.grid(row=2, column=1) year_field.grid(row=3, column=1) button.grid(row=4, column=1) Exit.grid(row=6, column=1) new.mainloop()
In the driver code, firstly we are giving background color to the left part of the screen(as shown in the image below). Since it is a small window to give input year so we set its dimension as 250×140. In the button line below year_field, we call showCalendar function that we made above. This function shows us the full calendar for an input year.
Now, we need to adjust the widgets in the calendar also, for that we define the location in the grid for everything. You can play by changing grid row and column parameters to explore more.
3. Convert Image to a Pencil Sketch using Python
This is going to be interesting. We will be writing the code step by step with the explanation.
We will use the OpenCV library for this project. Install it using pip install opencv-python command.
Step 1: Find an image that you want to convert to a pencil sketch.
We are going to use a dog image. You can choose whatever you want.
Step 2: Read the image in RBG format and then convert it to a grayscale image. Now, the image is turned into a classic black and white photo.
import cv2 #reading image
image = cv2.imread("dog.jpg")
#converting BGR image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
Step 3: Invert the grayscale image also called the negative image, this will be our inverted grayscale image. Inversion is basically used to enhance details.
inverted_image = 255 - gray_image
Step 4: Finally create the pencil sketch by mixing the grayscale image with the inverted blurry image. This is done by dividing the grayscale image by the inverted blurry image.
blurred = cv2.GaussianBlur(inverted_image, (21, 21), 0)
inverted_blurred = 255 - blurred pencil_sketch = cv2.divide(gray_image, inverted_blurred, scale=256.0)
We now got our pencil_sketch. So, display it using OpenCV.
cv2.imshow("Original Image", image) cv2.imshow("Pencil Sketch of Dog", pencil_sketch) cv2.waitKey(0)
See, how beautiful it is. So, just try this with other images also and play with python. These are just 3 projects we discussed. You can try more projects which will make you interested in programming. So, here is the list of a few projects.
List of some more python projects to try out:
1. Weight Converter with GUI using Tkinter
2. Send custom emails with Python
3. Unique password generator GUI
4. Text to speech with python
5. Scraping data from Twitter
6. Rock paper scissors Python game
7. Alarm Clock with GUI
8. Youtube Video downloader
9. Python Website blocker
10. Python Snake Game
Python is very easy to learn language because of its easy syntax. Here we discussed only basic python projects. I hope you find this article helpful. Let’s connect on Linkedin.
Thanks for reading if you reached here :).
The media shown in this article are not owned by Analytics Vidhya and are used at the Author’s discretion.