Question
image.py import tkinter import os, os.path tk = tkinter _root = None class ImageView(tk.Canvas): def __init__(self, image, title = New Image, autoflush=False): master = tk.Toplevel(_root)
image.py
import tkinter import os, os.path tk = tkinter
_root = None
class ImageView(tk.Canvas): def __init__(self, image, title = "New Image", autoflush=False): master = tk.Toplevel(_root) master.protocol("WM_DELETE_WINDOW", self.close) tk.Canvas.__init__(self, master, width = image.getWidth(), height = image.getHeight()) self.master.title(title) self.pack() master.resizable(0,0) self.image = image self.height = image.getHeight() self.width = image.getWidth() self.autoflush = autoflush self.closed = False
def close(self): """Close the window""" self.closed = True self.master.destroy() self.image.canvas = None _root.quit()
def isClosed(self): return self.closed
def getHeight(self): """Return the height of the window""" return self.height
def getWidth(self): """Return the width of the window""" return self.width
class Image: def __init__(self, *args): self.canvas = None if len(args) == 1: name = args[0] if type(name) != str: raise Exception('Must be a file name') if name[-4:].upper() != '.GIF': raise Exception('File must be a GIF') if not os.path.exists(args[0]): raise Exception('File not in current directory') self.image = tk.PhotoImage(file = args[0], master = _root) self.filename = args[0] self.width = self.image.width() self.height = self.image.height() else: # arguments are width and height self.width, self.height = args self.image = tk.PhotoImage(master =_root, width = self.width, height = self.height) self.filename = "" def getWidth(self): """Returns the width of the image in pixels""" return self.width
def getHeight(self): """Returns the height of the image in pixels""" return self.height
def getPixel(self, x, y): """Returns a tuple (r,g,b) with the RGB color values for pixel (x,y) r,g,b are in range(256)
""" value = self.image.get(x, y) if type(value) == int: return (value, value, value) elif type(value) == tuple: return value else: return tuple(map(int, value.split()))
def setPixel(self, x, y, color): """Sets pixel (x,y) to the color given by RGB values r, g, and b. r,g,b should be in range(256)
""" (r, g, b) = color r = round(r) g = round(g) b = round(b) color = (r, g, b) self.image.put("{#%02x%02x%02x}" % color, (x, y))
def draw(self): """Creates and opens a window on an image. The user must close the window to return control to the caller.""" if not self.canvas: self.canvas = ImageView(self, self.filename) self.canvas.create_image(self.width // 2, self.height // 2, image = self.image) _root.mainloop()
def save(self, filename = ""): """Saves the image to filename. If no file name is provided, uses the image's file name if there is one; otherwise, simply returns. If the .gif extension is not present, it is added. """ if filename == "": return else: self.filename = filename path, name = os.path.split(filename) ext = name.split(".")[-1] if ext != "gif": filename += ".gif" self.filename = filename self.image.write(self.filename, format = "gif")
def clone(self): new = Image(self.width, self.height) new.image = self.image.copy() return new
def __str__(self): rep = "" if self.filename: rep += ("File name: " + self.filename + " ") rep += ("Width: " + str(self.width) + \ " Height: " + str(self.height)) return rep _root = tk.Tk() _root.withdraw()
sharpen.py
from images import Image
def sharpen(image, degree, threshold): """Builds and returns a sharpened image. Expects an image and two integers (the degree to which the image should be sharpened and the threshold used to detect edges) as arguments.""" pass
def main(): filename = input("Enter the image file name: ") image = Image(filename) newimage = sharpen(image, 20, 15) newimage.draw()
if __name__ == "__main__": main()
smokey.gif
(is only a gif here)
The edge-detection function described in this chapter returns a black-and-white image. Think of a similar way to transform color values so that the new image is still in its original colors but the outlines within it are merely sharpened. Then, define a function named sharpen that performs this operation. The function should expect an image and two integers as arguments. One integer should represent the degree to which the image should be sharpened. The other integer should represent the threshold used to detect edges. (Hint: A pixel can be darkened by making its RGB values smaller.) The edge-detection function described in this chapter returns a black-and-white image. Think of a similar way to transform color values so that the new image is still in its original colors but the outlines within it are merely sharpened. Then, define a function named sharpen that performs this operation. The function should expect an image and two integers as arguments. One integer should represent the degree to which the image should be sharpened. The other integer should represent the threshold used to detect edges. (Hint: A pixel can be darkened by making its RGB values smaller.)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started