import tkinter from tkinter import * from tkinter.filedialog import asksaveasfilename from tkinter.filedialog import askopenfilename from PIL import Image import urllib.request, io import manip # define globals and initialize global canvas, image, tk_image, root, dialog image = None canvas = None tk_image = None def wrap_image_for_tkInter( img ): try: from PIL import ImageTk return ImageTk.PhotoImage(img) except: iname = "_photo-op-temporary.jpg" img.save(iname) return PhotoImage(file=iname) def url_get_image( link ) : # returns the image at link stream = urllib.request.urlopen( link ) data = stream.read() file = io.BytesIO( data ) image = Image.open(file) image = image.convert('RGB') return image def create_button( container, label, action ) : ''' return a new button stored in container whose contents is label, that when clicked performs action ''' button = tkinter.Button( container , width=7, text=label ) # button is to respond to a left click button.bind( '', action ) # get button to arrange itself button.pack( padx=2, pady=5, side=LEFT ) # hand back our new button return button def open_file_click( event ) : ''' open an image ''' global image, tk_image, canvas filename = askopenfilename() try : image = Image.open( filename ) except : return image = image.convert('RGB') tk_image = wrap_image_for_tkInter( image ) canvas.config( image=tk_image ) def open_url_click( event ) : ''' open an image ''' global image, tk_image, canvas, root, dialog input = Label( root, text="URL") input.pack() dialog = Entry(root) dialog.pack() b = Button(root, text="OK", command=dialog_click ) b.pack() def dialog_click( ) : global image, tk_image, canvas, root, dialog import urllib.request, io link = dialog.get() image = url_get_image( link ) tk_image = wrap_image_for_tkInter( image ) canvas.config( image=tk_image ) def grey_click( event ) : global image, tk_image, canvas image = manip.greyscale( image ) tk_image = wrap_image_for_tkInter( image ) canvas.config( image=tk_image ) def mirror_click( event ) : global image, tk_image, canvas image = manip.mirroring( image ) tk_image = wrap_image_for_tkInter( image ) canvas.config( image=tk_image ) def cw_click( event ) : global image, tk_image, canvas image = manip.rotate( image ) tk_image = wrap_image_for_tkInter( image ) canvas.config( image=tk_image ) def flip_click( event ) : global image, tk_image, canvas image = manip.flipping( image ) tk_image = wrap_image_for_tkInter( image ) canvas.config( image=tk_image ) def sepia_click( event ) : global image, tk_image, canvas image = manip.sepia_tone( image ) tk_image = wrap_image_for_tkInter( image ) canvas.config( image=tk_image ) def contrast_click( event ) : global image, tk_image, canvas image = manip.reduce_palette( image ) tk_image = wrap_image_for_tkInter( image ) canvas.config( image=tk_image ) def mono_click( event ) : global image, tk_image, canvas image = manip.mono( image ) tk_image = wrap_image_for_tkInter( image ) canvas.config( image=tk_image ) canvas.config( image=tk_image ) def zoom_in_click( event ) : global image, tk_image, canvas image = manip.zoom_in( image ) tk_image = wrap_image_for_tkInter( image ) canvas.config( image=tk_image ) canvas.config( image=tk_image ) def zoom_out_click( event ) : global image, tk_image, canvas image = manip.zoom_out( image ) tk_image = wrap_image_for_tkInter( image ) canvas.config( image=tk_image ) canvas.config( image=tk_image ) def pixelate_click( event ) : global image, tk_image, canvas import effect image = effect.blur( image ) tk_image = wrap_image_for_tkInter( image ) canvas.config( image=tk_image ) canvas.config( image=tk_image ) def saveas_click( event ) : ''' save manipulation ''' global image, tk_image, canvas filename = asksaveasfilename() image.save( filename ) def save_click( event ) : ''' save manipulation ''' global image, tk_image, canvas image.save( filename, 'jpg' ) def create_edit_menu( container ) : menubar = tkinter.Menubutton( container, text='Commands' ) menubar.grid() edit_menu = tkinter.Menu( menubar, tearoff=0 ) edit_menu.add_command( label='Flip' , command=flipping ) edit_menu.add_command( label='Mirror' , command=mirroring ) edit_menu.add_command( label='Rotate' , command=rotate ) edit_menu.add_command( label='Grey scale' , command=greyscale ) edit_menu.add_command( label='Sepia' , command=sepia_tone ) edit_menu.add_command( label='Contrast' , command=reduce_palette ) #edit_menu.add_command( label='Revert' , command=revert ) menubar.pack() return menubar def url_get( link ) : # returns the image at link stream = urllib.request.urlopen( link ) data = stream.read() file = io.BytesIO( data ) image = Image.open(file) image = image.convert('RGB') return image def GUI( name='Photo Op', window_width=1000, window_height=900, bg='#4682B4' ) : global image, tk_image, canvas, root # define root window for the application root = Tk() # title the window root.title( name ) # create the contain for the window content pane container = Frame( root, width=window_width, height=window_height, background=bg ) # configure the container container.pack_propagate( 0 ) container.pack() # start off with the mandrill image link = 'http://www.cs.virginia.edu/~cs1112/images/mandrill/full.jpg' image = url_get_image( link ) # convert image from PIL format to tkinter format tk_image = wrap_image_for_tkInter( image ) # create a label to be the canvas holding the image canvas = tkinter.Label(container, image=tk_image ) # create a sub-container to hold the buttons buttons = LabelFrame( container, text='Image Operations', labelanchor=N, background="#4682B4" ) # create buttons for selecting photo operation open_file_button = create_button( buttons, 'Open file', open_file_click ) saveas_button = create_button( buttons, 'Save As', saveas_click ) flip_button = create_button( buttons, 'Flip', flip_click ) mirror_button = create_button( buttons, 'Mirror', mirror_click ) cw_button = create_button( buttons, 'Rotate', cw_click ) sepia_button = create_button( buttons, 'Sepia', sepia_click ) grey_button = create_button( buttons, 'Grey', grey_click ) mono_button = create_button( buttons, 'Mono', mono_click ) contrast_button = create_button( buttons, 'Contrast', contrast_click ) zoom_in_button = create_button( buttons, 'Zoom in', zoom_in_click ) zoom_out_button = create_button( buttons, 'Zoom out', zoom_out_click ) pixelate_button = create_button( buttons, 'Pixelate', pixelate_click ) #revert_button = create_button( buttons, 'Revert', revert_click ) buttons.pack( pady=10 ) # arrange the widgets in the window canvas.pack() # start event handling root.mainloop() if ( __name__ == '__main__' ) : GUI()