QR Code Generation Using Python: 13.import Qrcode 15.img - Save ("Youtubeqr - JPG")
QR Code Generation Using Python: 13.import Qrcode 15.img - Save ("Youtubeqr - JPG")
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:
1. Contact details
2. Facebook ids, Instagram ids, Twitter ids, WhatsApp ids, and more.
3. Event Details
4. YouTube links
5. Product details
6. Link directly to download an app on the Apple App Store or Google Play.
7. They are also being used in doing digital transactions by simply scanning QR
codes.
8. Access WI-Fi by storing encryption details such as SSID, password, and
encryption type.
9. For QR code generation using python, we are going to use a python module
called QRcode.
10.Link: https://pypi.org/project/qrcode/
11.Install it using this command: pip install qrcode
12.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.
13.import qrcode
14.img = qrcode.make("https://www.youtube.com/")
15.img.save("youtubeQR.jpg")
16.On executing this code output is:
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.
For example:You can encode: India is a country with many religions. I love India.
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:
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.
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.
Program:
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()
Explanation
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 show Calendar 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.
Output:
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.
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.
#image inversion
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.
Python is a multipurpose language and one can do literally anything with it.
Python can also be used for game development. Let’s create a simple command
line Rock-Paper-Scissor game without using any external game libraries like
PyGame.
In this game, user gets the first chance to pick the option among Rock, paper and
scissor. After that computer select from remaining two choices (randomly), then
winner is decided as per the rules.
while True:
if choice == 1:
choice_name = 'Rock'
elif choice == 2:
choice_name = 'paper'
else:
choice_name = 'scissor’
comp_choice = random.randint(1, 3)
comp_choice = random.randint(1, 3)
if comp_choice == 1:
comp_choice_name = 'Rock'
elif comp_choice == 2:
comp_choice_name = 'paper'
else:
comp_choice_name = 'scissor'
# condition for winning
result = "paper"
result = "Rock"
else:
result = "scissor"
else:
print("Do you want to play again? (Y/N)")
ans = input()
break
Output :
winning Rules of the Rock paper and scissor game as follows:
rock vs paper->paper wins
rock vs scissors->rock wins
paper vs scissors->scissors wins
Enter choice
1. Rock
2. paper
3. scissor
User turn: 1
User choice is: Rock
Now its computer turn.......
In this game, there is a list of words present, out of which our interpreter will
choose 1 random word. The user first has to input their names and then, will be
asked to guess any alphabet. If the random word contains that alphabet, it will
be shown as the output (with correct placement) else the program will ask you
to guess another alphabet. User will be given 12 turns(can be changed
accordingly) to guess the complete word.
import random
# library that we use in order to choose
# on random words from a list of words
name = input("What is your name? ")
# Here the user is asked to enter the name first
print("Good Luck ! ", name)
words = ['rainbow', 'computer', 'science', 'programming',
'python', 'mathematics', 'player', 'condition',
'reverse', 'water', 'board', 'geeks']
# Function will choose one random
# word from this list of words
word = random.choice(words)
print("Guess the characters")
guesses = ''
# any number of turns can be used here
turns = 12
while turns > 0:
# counts the number of times a user fails
failed = 0
# all characters from the input
# word taking one at a time.
for char in word:
# comparing that character with
# the character in guesses
if char in guesses:
print(char)
else:
print("_")
# for every failure 1 will be
# incremented in failure
failed += 1
if failed == 0:
# user will win the game if failure is 0
# and 'You Win' will be given as output
print("You Win")
# this print the correct word
print("The word is: ", word)
break
# if user has input the wrong alphabet then
# it will ask user to enter another alphabet
guess = input("guess a character:")
# every input character will be stored in guesses
guesses += guess
# check input with the character in word
if guess not in word:
turns -= 1
# if the character doesn’t match the word
# then “Wrong” will be given as output
# this will print the number of
# turns left for the user
print("You have", + turns, 'more guesses')
if turns == 0:
print("You Loose")
Output:
What is your name? Gautam
Good Luck! Gautam
Guess the characters
_
_
_
_
_
guess a character:g
g
_
_
_
_
guess a character:e
g
e
e
_
_
guess a character:k
g
e
e
k
_
guess a character:s
g
e
e
k
s
You Win
The word is: geeks