0% found this document useful (0 votes)
17 views

python code pdf

The document contains three separate code snippets: a Breadth-First Search (BFS) algorithm for traversing a graph, a Vacuum Cleaner Agent simulation that cleans an environment, and a wxPython application for removing backgrounds from images. Each code snippet includes the necessary functions and classes to perform their respective tasks. The BFS code explores a graph starting from node 'A', the vacuum cleaner code simulates an agent's actions based on the dirt status, and the photo editing code provides a GUI for loading and saving images while removing backgrounds.

Uploaded by

Batool 2002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

python code pdf

The document contains three separate code snippets: a Breadth-First Search (BFS) algorithm for traversing a graph, a Vacuum Cleaner Agent simulation that cleans an environment, and a wxPython application for removing backgrounds from images. Each code snippet includes the necessary functions and classes to perform their respective tasks. The BFS code explores a graph starting from node 'A', the vacuum cleaner code simulates an agent's actions based on the dirt status, and the photo editing code provides a GUI for loading and saving images while removing backgrounds.

Uploaded by

Batool 2002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

#breath for search algoritum code

graph = {
'A': ['B', 'C','D'],
'B': ['F', 'E'],
'C': ['G'],
'D': ['H','I'],
'E': ['J'],
'F': ['K'],
'G': [],
'H': ['L'],
'I': ['M'],
'J': ['N'],
'K': [],
'L': [],
'M': [],
'N': []
}

visited = []
queue = []

def bfs(visited, graph, node):


visited.append(node)
queue.append(node)

while queue:
s = queue.pop(0)
print(s, end=" ")

for neighbour in graph[s]:


if neighbour not in visited:
visited.append(neighbour)
queue.append(neighbour)

# Driver Code
bfs(visited, graph, 'A')
print('\n')
print(visited)

#vacume cleanar code


import random
class VacuumCleanerAgent:
def __init__(self, environment):
self.environment = environment
self.location =
random.choice(list(environment.keys()))

def sense(self):
return self.environment[self.location]

def act(self, action):


if action == "left":
self.location = "A"
elif action == "right":
self.location = "B"
elif action == "clean":
self.environment[self.location] = 0

def simulate_environment():
return {"A": random.randint(0, 1), "B":
random.randint(0, 1)}
def main():
environment = simulate_environment()
agent = VacuumCleanerAgent(environment)

for _ in range(10): # Simulate 10 steps


print(f"Current State: {environment}")
print(f"Agent Location: {agent.location}")
dirt_status = agent.sense()

if dirt_status == 1:
print("Agent cleaning...")
agent.act("clean")
else:
action = random.choice(["left", "right"])
print(f"Agent moving {action}...")
agent.act(action)
print("---------")

if __name__ == "__main__":
main()

#photo editing code


import io
import wx
import wx.lib.filebrowsebutton as filebrowse
import rembg
class RemoveBackgroundApp(wx.Frame):
def __init__(self, parent, title):
super(RemoveBackgroundApp,
self).__init__(parent, title=title, size=(500, 400))

# Create a panel with a gradient background


self.panel = GradientPanel(self)

self.image = wx.StaticBitmap(self.panel,
wx.ID_ANY, wx.NullBitmap, size=(400, 300))

self.load_button = wx.Button(self.panel,
label="Load Image")
self.load_button.Bind(wx.EVT_BUTTON,
self.load_image)

self.remove_bg_button = wx.Button(self.panel,
label="Remove Background")
self.remove_bg_button.Bind(wx.EVT_BUTTON,
self.remove_background)

self.file_picker =
filebrowse.FileBrowseButton(self.panel, labelText="Image
File", fileMask="*.png;*.jpg;*.jpeg;*.bmp;*.gif")

self.save_button = wx.Button(self.panel,
label="Save Image")
self.save_button.Bind(wx.EVT_BUTTON,
self.save_image)

# Create a box sizer to center and align the


buttons at the bottom
self.button_sizer = wx.BoxSizer(wx.HORIZONTAL)
self.button_sizer.Add(self.load_button, 0,
wx.ALL, 5)
self.button_sizer.Add(self.remove_bg_button, 0,
wx.ALL, 5)
self.button_sizer.Add(self.file_picker, 1,
wx.ALL | wx.EXPAND, 5)
self.button_sizer.Add(self.save_button, 0,
wx.ALL, 5)

# Create a main sizer for the panel


self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.image, 0, wx.ALL |
wx.ALIGN_CENTER, 5)
self.sizer.Add(self.button_sizer, 0, wx.ALL |
wx.ALIGN_CENTER, 5)

self.panel.SetSizer(self.sizer)
self.Layout()

self.modified_image = None

def load_image(self, event):


dialog = wx.FileDialog(self, "Choose an image
file", "", "", "Image files
(*.png;*.jpg;*.jpeg;*.bmp;*.gif)|*.png;*.jpg;*.jpeg;*.bm
p;*.gif", wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)

if dialog.ShowModal() == wx.ID_OK:
filepath = dialog.GetPath()
dialog.Destroy()
self.file_picker.SetValue(filepath)
self.display_image(filepath)

def display_image(self, filepath):


image = wx.Image(filepath, wx.BITMAP_TYPE_ANY)
image = image.Scale(400, 300,
wx.IMAGE_QUALITY_HIGH)
bitmap = wx.Bitmap(image)

self.image.SetBitmap(bitmap)
self.modified_image = None # Reset modified
image when loading a new one

def remove_background(self, event):


filepath = self.file_picker.GetValue()

if filepath:
with open(filepath, "rb") as input_file:
input_data = input_file.read()

output_data = rembg.remove(input_data)

output_image =
wx.Image(io.BytesIO(output_data), wx.BITMAP_TYPE_ANY)
output_image = output_image.Scale(400, 300,
wx.IMAGE_QUALITY_HIGH)
self.modified_image =
output_image.ConvertToBitmap()

self.image.SetBitmap(self.modified_image)

def save_image(self, event):


if self.modified_image:
dialog = wx.FileDialog(self, "Save the
modified image", "", "", "PNG files (*.png)|*.png",
wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)

if dialog.ShowModal() == wx.ID_OK:
filepath = dialog.GetPath()
dialog.Destroy()

self.modified_image.SaveFile(filepath,
wx.BITMAP_TYPE_PNG)

class GradientPanel(wx.Panel):
def __init__(self, parent):
super(GradientPanel, self).__init__(parent)

self.Bind(wx.EVT_PAINT, self.on_paint)

def on_paint(self, event):


dc = wx.PaintDC(self)
dc.GradientFillLinear((0, 0, self.GetSize()[0],
self.GetSize()[1]), "#E6E6FA", "#FFFFFF", wx.NORTH)

def main():
app = wx.App(False)
frame = RemoveBackgroundApp(None, "Remove Background
App")
frame.Show()
app.MainLoop()

if __name__ == "__main__":
main()

You might also like