Advertisement
Guest User

Untitled

a guest
May 29th, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. import Tkinter as tk
  2. from PIL import Image
  3. from PIL import ImageTk
  4. import numpy as np
  5. import tkFileDialog
  6.  
  7. class DIP(tk.Frame):
  8. def __init__(self, parent):
  9. tk.Frame.__init__(self, parent)
  10. self.parent = parent
  11. self.initUI()
  12.  
  13. def initUI(self):
  14. self.parent.title("DIP Algorithms- Simple Photo Editor")
  15. self.pack(fill = tk.BOTH, expand = 1)
  16.  
  17. menubar = tk.Menu(self.parent)
  18. self.parent.config(menu = menubar)
  19.  
  20. self.label1 = tk.Label(self, border = 25)
  21. self.label2 = tk.Label(self, border = 25)
  22. self.label1.grid(row = 1, column = 1)
  23. self.label2.grid(row = 1, column = 2)
  24.  
  25. #Open Image Menu
  26. fileMenu = tk.Menu(menubar)
  27. fileMenu.add_command(label = "Open", command = self.onOpen)
  28. menubar.add_cascade(label = "File", menu = fileMenu)
  29.  
  30. #menu for image ngative
  31. basicMenu = tk.Menu(menubar)
  32. basicMenu.add_command(label = "Negative", command = self.onNeg)
  33. menubar.add_cascade(label = "Basic", menu = basicMenu)
  34.  
  35. def onNeg(self):
  36. #Image Negative Menu callback
  37. I2 = 255-self.I;
  38. im = Image.fromarray(np.uint8(I2))
  39. photo2 = ImageTk.PhotoImage(im)
  40. self.label2.image = photo2 # keep a reference!
  41.  
  42. def setImage(self):
  43. self.img = Image.open(self.fn)
  44. self.I = np.asarray(self.img)
  45. l, h = self.img.size
  46. text = str(2*l+100)+"x"+str(h+50)+"+0+0"
  47. self.parent.geometry(text)
  48. photo = ImageTk.PhotoImage(self.img)
  49. self.label1.configure(image = photo)
  50. self.label1.image = photo # keep a reference!
  51.  
  52. def onOpen(self):
  53. #Open Callback
  54. ftypes = [('Image Files', '*.tif *.jpg *.png')]
  55. dlg = tkFileDialog.Open(self, filetypes = ftypes)
  56. filename = dlg.show()
  57. self.fn = filename
  58. #print self.fn #prints filename with path here
  59. self.setImage()
  60.  
  61. #def onError(self):
  62. #box.showerror("Error", "Could not open file")
  63.  
  64. def main():
  65.  
  66. root = tk.Tk()
  67. DIP(root)
  68. root.geometry("320x240")
  69. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement