0% found this document useful (0 votes)
62 views17 pages

QR Code Generation Using Python: 13.import Qrcode 15.img - Save ("Youtubeqr - JPG")

This Python code converts an input image to a pencil sketch effect. It reads the image, converts it to grayscale, inverts it,

Uploaded by

Nidhi Arora
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
62 views17 pages

QR Code Generation Using Python: 13.import Qrcode 15.img - Save ("Youtubeqr - JPG")

This Python code converts an input image to a pencil sketch effect. It reads the image, converts it to grayscale, inverts it,

Uploaded by

Nidhi Arora
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 17

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:

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 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.

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.

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.

Program:

#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()
Explanation

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.

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.

#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.

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)
Output:

Python program to implement Rock Paper Scissor game

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.
 

Rock vs paper-> paper wins


Rock vs scissor-> Rock wins
paper vs scissor-> scissor wins.
In this game, randint() inbuilt function is used for generating random integer
value within the given range.
Below is the implementation:

# import random module

# Print multiline instruction

# performstring concatenation of string

print("Winning Rules of the Rock paper scissor game as follows: \n"

                                +"Rock vs paper->paper wins \n"

                                + "Rock vs scissor->Rock wins \n"

                                +"paper vs scissor->scissor wins \n")

  

while True:

    print("Enter choice \n 1. Rock \n 2. paper \n 3. scissor \n")

    # take the input from user

    choice = int(input("User turn: "))

    # OR is the short-circuit operator

    # if any one of the condition is true

    # then it return True value


      

    # looping until user enter invalid input

    while choice > 3 or choice < 1:

        choice = int(input("enter valid input: "))

    # initialize value of choice_name variable

    # corresponding to the choice value

    if choice == 1:

        choice_name = 'Rock'

    elif choice == 2:

        choice_name = 'paper'

    else:

        choice_name = 'scissor’

    # print user choice 

    print("user choice is: " + choice_name)

    print("\nNow its computer turn.......")

  # Computer chooses randomly any number 

    # among 1 , 2 and 3. Using randint method # of random module

    comp_choice = random.randint(1, 3)
 

    # looping until comp_choice value 

    # is equal to the choice value

    while comp_choice == choice:

        comp_choice = random.randint(1, 3)

  

    # initialize value of comp_choice_name 

    # variable corresponding to the choice value

    if comp_choice == 1:

        comp_choice_name = 'Rock'

    elif comp_choice == 2:

        comp_choice_name = 'paper'

    else:

        comp_choice_name = 'scissor'

          

    print("Computer choice is: " + comp_choice_name)

    print(choice_name + " V/s " + comp_choice_name)

  
    # condition for winning

    if((choice == 1 and comp_choice == 2) or

      (choice == 2 and comp_choice ==1 )):

        print("paper wins => ", end = "")

        result = "paper"

    elif((choice == 1 and comp_choice == 3) or

        (choice == 3 and comp_choice == 1)):

        print("Rock wins =>", end = "")

        result = "Rock"

    else:

        print("scissor wins =>", end = "")

        result = "scissor"

    # Printing either user or computer wins

    if result == choice_name:

        print("<== User wins ==>")

    else:

        print("<== Computer wins ==>")

          
    print("Do you want to play again? (Y/N)")

    ans = input()

    # if user input n or N then condition is True

    if ans == 'n' or ans == 'N':

        break

# after coming out of the while loop

# we print thanks for playing

print("\nThanks for playing")

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.......

computer choice is: paper


Rock V/s paper
paper wins =>computer wins
do you want to play again?
N

Python program for word guessing game

Python is a powerful multi-purpose programming language used by multiple


giant companies. It has simple and easy to use syntax making it perfect language
for someone trying to learn computer programming for first time. It is a high-
level programming language, and its core design philosophy is all about code
readability and a syntax which allows programmers to express concepts in a few
lines of code.
In this article, we will use random module to make a word guessing game. This
game is for beginners learning to code in python and to give them a little brief
about using strings, loops and conditional(If, else) statements.
 
random module : 
Sometimes we want the computer to pick a random number in a given range,
pick a random element from a list, pick a random card from a deck, flip a coin,
etc. The random module provides access to functions that support these types
of operations. One such operation is random.choice() method (returns a random
item from a list, tuple, or string.) that we are going to use in order to select one
random word from a list of words that we’ve created.

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.

Below is the Python implementation: 


 
 Python3

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

You might also like