Q1.
from PIL import Image, ImageFilter
import numpy as np
from [Link] import convolve2d
class ImageProcessor:
def __init__(self, image_path):
# Open imgage
[Link] = [Link](image_path)
def image_process(self, output_path, blur_radius=0):
# Convert image to grayscale and apply Gaussian blur
img = [Link]('L')
img = [Link]([Link](blur_radius))
img_array = [Link](img)
# Sobel kernels for edge detection
sobel_x = [Link]([[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1]])
sobel_y = [Link]([[-1, -2, -1],
[0, 0, 0],
[1, 2, 1]])
# Convolve image with Sobel kernels to compute gradients
gradx = convolve2d(img_array, sobel_x)
grady = convolve2d(img_array, sobel_y)
# Check if convolution was successful
if gradx is None or grady is None:
print("Error: Unable to process image.")
return
# Compute gradient magnitude
grad_mag = [Link](gradx**2 + grady**2)
max_gradient = [Link](grad_mag)
# Check if max gradient is zero
if max_gradient == 0:
print("Error: Unable to process image. Max gradient is zero.")
return
# Normalize gradient magnitude to range [0, 255]
grad_mag = (
255.0 / [Link](grad_mag)) * grad_mag
grad_mag_scaled = grad_mag * \
(255.0 / [Link](grad_mag))
# Convert gradient magnitude array to PIL Image
edge_image = [Link](
grad_mag_scaled.astype(np.uint8))
# Save the edge-detected image
edge_image.save(output_path)
# Example usage
img = ImageProcessor("[Link]")
img.image_process("[Link]", 1)
OUTPUT:
Q2.
import turtle
class Draw: # turtle class for drawing
def __init__(self, turtle, screen): # init turtle attributes
[Link] = turtle
[Link] = screen
[Link](0)
[Link]()
[Link](1.5)
[Link](0)
# function for rectangle
def rectangle(self, sidex, sidey, coords=None, angle=0):
a = [Link]
if coords is None: # if no position is given
coords = [Link]() # use current position
# move turtle to correct positoin for rectangle in center
[Link](angle)
[Link]()
[Link](coords)
[Link](sidex/2)
[Link](90)
[Link](sidey/2)
[Link](90)
[Link]()
# draw rectangle
[Link]("cyan" if sidex==sidey else "red")
a.begin_fill()
for i in range(2):
[Link](sidex)
[Link](90)
[Link](sidey)
[Link](90)
a.end_fill()
# function to draw circle
def circle(self, radius, coords=None):
t = [Link]
if coords is None: # if no position is given
coords = [Link]() # use current position
# draw from center and return to center
[Link]()
[Link](coords)
[Link](90)
[Link](radius)
[Link](90)
[Link]()
[Link]("green")
t.begin_fill()
[Link](radius)
t.end_fill()
[Link]()
[Link](coords)
# function for drawing lines
def triangle(self, side_len, coords, angle=0):
t = [Link]
[Link]()
[Link](coords)
[Link]()
[Link](angle)
[Link]("purple")
t.begin_fill()
for i in range(3):
[Link](side_len)
[Link](120)
t.end_fill()
# function for drawing the robot
def draw_robot(self):
[Link](200,200)
[Link](50,100,(-50,-150))
[Link](50,100,(50,-150))
[Link](60,60,(-50,-230))
[Link](60,60,(50,-230))
[Link](35,40,(0,120))
[Link](80,(0,220))
[Link](70,30,(0,180))
[Link](20,(-30,240))
[Link](20,(30,240))
[Link](60,(101,105),-90)
[Link](60,(-101,105),-150)
[Link](60,(-30,295))
[Link](60,(75,250),-90)
[Link](60,(-75,250),-150)
[Link](45,100,(152,15),30)
[Link](45,100,(-152,15),-30)
[Link](20,(188,-45))
[Link](20,(-188,-45))
[Link]()
t = [Link]()
screen = [Link]()
[Link](width=700,height=700)
d = Draw(t, screen) # class instance
d.draw_robot() # draw shape
[Link]()
Output:
Q3.
class Calsi:
def expression_split(self, expression):
expression = [Link](" ", "") # remove space
operators = {'+': 1, '-': 1, '*': 2, '/': 2, '^': 3}
output = []
list2 = []
i=0
while i < len(expression):
if expression[i].isdigit():
num = ''
while i < len(expression) and expression[i].isdigit():
num += expression[i]
i += 1
[Link](num)
elif expression[i] in operators:
while (list2 and list2[-1] in operators
and operators[list2[-1]] >= operators[expression[i]]):
[Link]([Link]())
[Link](expression[i])
i += 1
elif expression[i] == '(':
[Link](expression[i])
i += 1
elif expression[i] == ')':
while list2 and list2[-1] != '(':
[Link]([Link]())
if list2[-1] == '(':
[Link]() # Remove '('
i += 1
while list2:
[Link]([Link]())
return output
def calculate(self, expression):
list3 = []
for token in expression:
if [Link]():
[Link](int(token))
else:
num2 = [Link]()
num1 = [Link]()
if token == '+':
[Link](num1 + num2)
elif token == '-':
[Link](num1 - num2)
elif token == '*':
[Link](num1 * num2)
elif token == '/':
[Link](num1 / num2)
elif token == '^':
[Link](num1 ** num2)
return list3[0]
# Example usage:
expression = input("enter a expression: ")
d = Calsi()
k = d.expression_split(expression)
result = [Link](k)
print(f"Result of '{expression}' is: {result}")
Output: