Week 10
Week 10
import numpy as np
class ImageProcessor:
# Open imgage
self.img = Image.open(image_path)
img = self.img.convert('L')
img = img.filter(ImageFilter.GaussianBlur(blur_radius))
img_array = np.array(img)
[-2, 0, 2],
[-1, 0, 1]])
[0, 0, 0],
[1, 2, 1]])
return
max_gradient = np.max(grad_mag)
if max_gradient == 0:
return
grad_mag = (
grad_mag_scaled = grad_mag * \
(255.0 / np.max(grad_mag))
edge_image = Image.fromarray(
grad_mag_scaled.astype(np.uint8))
edge_image.save(output_path)
# Example usage
img = ImageProcessor("small.jpg")
img.image_process("s.jpg", 1)
OUTPUT:
Q2.
import turtle
self.screen = screen
self.turtle.speed(0)
self.turtle.hideturtle()
self.turtle.pensize(1.5)
self.screen.tracer(0)
a = self.turtle
a.setheading(angle)
a.up()
a.goto(coords)
a.bk(sidex/2)
a.right(90)
a.bk(sidey/2)
a.left(90)
a.pendown()
# draw rectangle
a.begin_fill()
for i in range(2):
a.forward(sidex)
a.right(90)
a.forward(sidey)
a.right(90)
a.end_fill()
t = self.turtle
t.up()
t.goto(coords)
t.right(90)
t.fd(radius)
t.left(90)
t.down()
t.fillcolor("green")
t.begin_fill()
t.circle(radius)
t.end_fill()
t.up()
t.goto(coords)
t = self.turtle
t.up()
t.goto(coords)
t.down()
t.seth(angle)
t.fillcolor("purple")
t.begin_fill()
for i in range(3):
t.fd(side_len)
t.left(120)
t.end_fill()
# function for drawing the robot
def draw_robot(self):
self.rectangle(200,200)
self.rectangle(50,100,(-50,-150))
self.rectangle(50,100,(50,-150))
self.rectangle(60,60,(-50,-230))
self.rectangle(60,60,(50,-230))
self.rectangle(35,40,(0,120))
self.circle(80,(0,220))
self.rectangle(70,30,(0,180))
self.circle(20,(-30,240))
self.circle(20,(30,240))
self.triangle(60,(101,105),-90)
self.triangle(60,(-101,105),-150)
self.triangle(60,(-30,295))
self.triangle(60,(75,250),-90)
self.triangle(60,(-75,250),-150)
self.rectangle(45,100,(152,15),30)
self.rectangle(45,100,(-152,15),-30)
self.circle(20,(188,-45))
self.circle(20,(-188,-45))
self.screen.update()
t = turtle.Turtle()
screen = turtle.Screen()
screen.setup(width=700,height=700)
screen.exitonclick()
Output:
Q3.
class Calsi:
output = []
list2 = []
i=0
if expression[i].isdigit():
num = ''
num += expression[i]
i += 1
output.append(num)
output.append(list2.pop())
list2.append(expression[i])
i += 1
list2.append(expression[i])
i += 1
output.append(list2.pop())
if list2[-1] == '(':
i += 1
while list2:
output.append(list2.pop())
return output
def calculate(self, expression):
list3 = []
if token.isdigit():
list3.append(int(token))
else:
num2 = list3.pop()
num1 = list3.pop()
if token == '+':
list3.append(num1 + num2)
list3.append(num1 - num2)
list3.append(num1 * num2)
list3.append(num1 / num2)
list3.append(num1 ** num2)
return list3[0]
# Example usage:
d = Calsi()
k = d.expression_split(expression)
result = d.calculate(k)
Output: