ClearCode

Image editor

Apr 1st, 2022
2,293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. import PySimpleGUI as sg
  2. from PIL import Image, ImageFilter, ImageOps
  3. from io import BytesIO
  4.  
  5. def update_image(original,blur,contrast,emboss,contour,flipx,flipy):
  6.     global image
  7.     image = original.filter(ImageFilter.GaussianBlur(blur))
  8.     image = image.filter(ImageFilter.UnsharpMask(contrast))
  9.  
  10.     if emboss:
  11.         image = image.filter(ImageFilter.EMBOSS())
  12.     if contour:
  13.         image = image.filter(ImageFilter.CONTOUR())
  14.  
  15.     if flipx:
  16.         image = ImageOps.mirror(image)
  17.     if flipy:
  18.         image = ImageOps.flip(image)
  19.  
  20.     bio = BytesIO()
  21.     image.save(bio, format = 'PNG')
  22.  
  23.     window['-IMAGE-'].update(data = bio.getvalue())
  24.  
  25. image_path = sg.popup_get_file('Open',no_window = True)
  26.  
  27. control_col = sg.Column([
  28.     [sg.Frame('Blur',layout = [[sg.Slider(range = (0,10), orientation = 'h', key = '-BLUR-')]])],
  29.     [sg.Frame('Contrast',layout = [[sg.Slider(range = (0,10), orientation = 'h', key = '-CONTRAST-')]])],
  30.     [sg.Checkbox('Emboss', key = '-EMBOSS-'), sg.Checkbox('Contour', key = '-CONTOUR-')],
  31.     [sg.Checkbox('Flip x', key = '-FLIPX-'), sg.Checkbox('Flip y', key = '-FLIPY-')],
  32.     [sg.Button('Save image', key = '-SAVE-')],])
  33. image_col = sg.Column([[sg.Image(image_path, key = '-IMAGE-')]])
  34. layout = [[control_col,image_col]]
  35.  
  36. original = Image.open(image_path)
  37. window = sg.Window('Image Editor', layout)
  38.  
  39. while True:
  40.     event, values = window.read(timeout = 50)
  41.     if event == sg.WIN_CLOSED:
  42.         break
  43.  
  44.     update_image(
  45.         original,
  46.         values['-BLUR-'],
  47.         values['-CONTRAST-'],
  48.         values['-EMBOSS-'],
  49.         values['-CONTOUR-'],
  50.         values['-FLIPX-'],
  51.         values['-FLIPY-'])
  52.  
  53.     if event == '-SAVE-':
  54.         save_path = sg.popup_get_file('Save',save_as = True, no_window = True) + '.png'
  55.         image.save(save_path,'PNG')
  56.        
  57. window.close()
Advertisement
Add Comment
Please, Sign In to add comment