You are on page 1of 15

MANAV RACHNA INTERNATIONAL

SCHOOL NOIDA

Session: 2022-23
COMPUTER SCIENCE (083) PROJECT

[BASIC PAINT APPLICATION MADE


USING TKINTER]

Student Name: Misha Goel


Grade: 11th
Roll No: 07
Manav Rachna International School, Noida
Session: 2022 – 23

Certificate

This is to certify that the content of this project entitled ________________ by


__________ is the bone fide work of (students) submitted to Manav Rachna
International School, Noida for consideration in partial fulfillment of the
requirement of Central Board of Secondary Education, Delhi.

The original research work was carried out by him/her under the supervision of
Mr/Ms Rakhi Garg in the academic year 2022-23. On the basis of the declaration
made by him, I recommend this project report for evaluation.

Signature of the Examiner


Manav Rachna International School, Noida
Session: 2022 – 23

ACKNOWLEDGEMENT

Firstly, we would like to thank my teacher (Rakhi Garg) because she always supported and guided me
while doing this project. She very well cleared all the doubts we had regarding project. Also, I would
like to especially thank my parents and friends who helped me a lot.

The journey of making this project file has been beautiful, as well as knowledgeable for me and I have
learned a lot from it.

Once again, thanks to everyone who was involved with this project from beginning to end.

Name of the Student : Misha Goel

Roll No: 07

Manav Rachna International School, Noida


Session: 2022 – 23

Index

S. No Topic Page No. T. Signature

1 Certificate

2 Acknowledgement

3 Introduction

4 System Requirements

5 Salient Features

6 Code

7 Output

8 Scope of Improvement

9 Bibliography
Introduction

A very rough draft/prototype of a paint


application with basic buttons. The buttons
have essential functions like erase, clear,
brush-size scale, shape selection, etc.
which are required in a simple paint
application. It also has a feature to toggle
between two tabs although there are no
widgets in 2nd tab as of now.
System Requirements

Main Processor: x86 64-bit CPU (Intel / AMD architecture)

Hard Disk: 1-3 GB free disk space

RAM: 4 GB

Operating System: - Windows* 7 or higher


-Mac OS X 10.11 or higher, 64-bit
-Linux: RHEL 6/7, 64-bit (almost all
libraries, also works in Ubuntu)

Language: Python* version 3.11.1


Salient Features

- Allows to draw with any color of choice


- Can choose size in the range of 1 to 20
- Gives buttons with functions to erase
specific parts and clear entire canvas
- Allows you to draw circle, triangle and
rectangle
Code

import PIL
from PIL import *
import tkinter as tk
from tkinter import colorchooser,filedialog,ttk
import cv2

root = tk.Tk()
notebook = ttk.Notebook(root)
root.geometry("800x600")
width, height = 800, 600
x_offset = (root.winfo_screenwidth() - width) / 2
y_offset = (root.winfo_screenheight() - height) / 2
root.geometry("%dx%d+%d+%d" % (width, height, x_offset, y_offset))

notebook.grid(row=0, column=00, sticky='nsew')

# Create two frames for the tabs


frame1 = ttk.Frame(notebook)
frame2 = ttk.Frame(notebook)

frame_buttons = tk.Frame(root)
frame_buttons.grid(row=1, column=0)

notebook.add(frame1, text="Tab 1")


notebook.add(frame2, text="Tab 2")

# Create a canvas widget on frame1


canvas = tk.Canvas(frame1, bg='white', width=700, height=500)
canvas.grid(row=0, column=1, columnspan=10)

# Create brush and eraser variables


brush_color = "black"
eraser_color = "white"
brush_size = 5

# Create an eraser button


eraser_button = tk.Button(frame1, text='Eraser', command=lambda: set_eraser())

# Create a brush size scale


brush_size_scale = tk.Scale(frame1, from_=1, to=20, orient="horizontal",
command=lambda x: set_brush_size(x))
brush_size_scale.set(brush_size)
def set_brush_color():
global brush_color
brush_color = colorchooser.askcolor()[1]

brush_color_button = tk.Button(frame1, text='Brush Color', command=lambda:


set_brush_color())
clear_button = tk.Button(frame1, text='Clear Canvas', command=lambda: clear_canvas())

def clear_canvas():
canvas.delete("all")

def set_eraser():
global brush_color
brush_color = eraser_color

def set_brush_size(size):
global brush_size
brush_size = int(size)

def paint(event):
x1, y1 = (event.x - brush_size), (event.y - brush_size)
x2, y2 = (event.x + brush_size), (event.y + brush_size)
canvas.create_oval(x1, y1, x2, y2, fill=brush_color, outline=brush_color)

eraser_button.grid(row=2, column=2)
brush_size_scale.grid(row=2, column=3)
brush_color_button.grid(row=2, column=1)
clear_button.grid(row=2, column=5)

shape_var = tk.StringVar(root)
shape_var.set("Select a Shape")
shape_dropdown = tk.OptionMenu(frame1, shape_var, "Oval",'Rectangle','Triangle')
shape_dropdown.grid(row=2, column=4)

def draw_shape(event):
shape = shape_var.get()
if shape == "Rectangle":
x1, y1 = (event.x - 40), (event.y - 40)
x2, y2 = (event.x + 40), (event.y + 40)
canvas.create_rectangle(x1, y1, x2, y2, outline=brush_color)
elif shape == "Oval":
x1, y1 = (event.x - 40), (event.y - 40)
x2, y2 = (event.x + 40), (event.y + 40)
canvas.create_oval(x1, y1, x2, y2, outline=brush_color)
elif shape == "Triangle":
x1, y1 = (event.x - 40), (event.y - 40)
x2, y2 = (event.x + 40), (event.y - 40)
x3, y3 = (event.x), (event.y + 40)
canvas.create_polygon(x1, y1, x2, y2, x3, y3,outline=brush_color)

canvas.bind("<Button-1>", draw_shape)

def record_canvas():
# Get the current state of the canvas as an image
# Get the location of the canvas
x = root.winfo_rootx() + canvas.winfo_x()
y = root.winfo_rooty() + canvas.winfo_y()
width = canvas.winfo_width()
height = canvas.winfo_height()

# Ask user to select a location to save the video


filepath = filedialog.asksaveasfilename(defaultextension=".avi", filetypes=[("AVI",
"*.avi"), ("All Files", "*.*")])
if not filepath:
return

# Define the codec and create a video writer


fourcc = cv2.VideoWriter_fourcc(*"XVID")
out = cv2.VideoWriter(filepath, fourcc, 20.0, (width, height))

# Get a handle to the screen


screen = cv2.GetWindowHandle("Canvas")

# Start recording
while True:
img = cv2.GetWindowImage(screen)
out.write(img)
if cv2.waitKey(1) & 0xFF == ord("q"):
break

# Release the video writer and close the window


out.release()
cv2.destroyAllWindows()

record_button = tk.Button(frame1, text='Record', command=lambda: record_canvas())


record_button.grid(row=2, column=7)
canvas.bind("<B1-Motion>", paint)

root.mainloop()
Output
Scope of Improvement

- More appealing GUI


- Fix bugs related to recording and shapes
- Add option to save as pdf/csv/Png and
show preview on another tab
- Use socket module to convert program
into a game like Pictionary
Bibliography
[Thareja, R., Python Programming: Using Problem Solving Approach, Oxford
University Press, 2019]

You might also like