Advertisement
here2share

# tk_rgb_filter_demo.py

Sep 5th, 2023
1,208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.34 KB | None | 0 0
  1. # tk_rgb_filter_demo.py ZZZ Sorry, I can't seem to find a way for this to work properly
  2.  
  3. from tkinter import *
  4. from PIL import Image, ImageTk, ImageFilter, ImageDraw, ImageOps, ImageFont
  5. from math import sin, cos, pi
  6. import copy
  7.  
  8. def float_range(start, stop, step=1.0):
  9.     while start < stop:
  10.         yield start
  11.         start += step
  12.  
  13. ww = 512
  14. hh = 512
  15. cx, cy = ww//2, hh//2
  16.  
  17. root = Tk()
  18. root.title("# tk_rgb_filter_demo")
  19. root.geometry("%dx%d+0+0"%(ww,hh))
  20.  
  21. def rgb2hex(r,g,b):
  22.     return '#%02X%02X%02X'%(r,g,b)
  23.  
  24. rgb = range(0, 256, 50)
  25. colors = [rgb2hex(r, g, b) for r in rgb for g in rgb for b in rgb]
  26.  
  27. img = Image.new('RGB', (ww, hh), (0, 0, 0))
  28. draw = {}
  29. for k in (0, 1, 2, 3):
  30.     draw[k] = ImageDraw.Draw(img)
  31.    
  32. text_img = Image.new('RGBA', (ww, hh), (0, 0, 0, 0))
  33. text_draw = ImageDraw.Draw(text_img)
  34. my_font = ImageFont.truetype('arial.ttf', 24)
  35.  
  36. blur_radius = 0.02 * min(img.size)
  37.  
  38. canvas = Canvas(root, width=ww, height=hh, bg='white')
  39. canvas.pack(side=LEFT, fill=BOTH, expand=True)
  40.  
  41. # Define filter types
  42. filters = {
  43.     'Blur': {'filter': ImageFilter.BLUR, 'range': (0, 10, 1)},
  44.     'Contour': {'filter': ImageFilter.CONTOUR, 'range': (0, 255, 25)},
  45.     'Detail': {'filter': ImageFilter.DETAIL, 'range': (0, 5, 0.5)},
  46.     'Edge Enhance': {'filter': ImageFilter.EDGE_ENHANCE, 'range': (0, 255, 25)},
  47.     'Edge Enhance More': {'filter': ImageFilter.EDGE_ENHANCE_MORE, 'range': (0, 255, 25)},
  48.     'Emboss': {'filter': ImageFilter.EMBOSS, 'range': (0, 255, 25)},
  49.     'Find Edges': {'filter': ImageFilter.FIND_EDGES, 'range': (0, 255, 25)},
  50.     'Sharpen': {'filter': ImageFilter.SHARPEN, 'range': (0, 10, 1)},
  51.     'Smooth': {'filter': ImageFilter.SMOOTH, 'range': (0, 10, 1)},
  52.     'Smooth More': {'filter': ImageFilter.SMOOTH_MORE, 'range': (0, 10, 1)},
  53.     'Sobel X': {'filter': ImageFilter.Kernel((3,3), [-1,0,1,-2,0,2,-1,0,1]), 'range': (-255, 255, 50)},
  54.     'Sobel Y': {'filter': ImageFilter.Kernel((3,3), [-1,-2,-1,0,0,0,1,2,1]), 'range': (-255, 255, 50)},
  55.     'Laplacian': {'filter': ImageFilter.Kernel((3,3), [0,1,0,1,-4,1,0,1,0]), 'range': (0, 255, 25)},
  56.     'Canny': {'filter': ImageOps.autocontrast(img.filter(ImageFilter.FIND_EDGES), 0), 'range': (0, 255, 25)},
  57. }
  58.  
  59. grayscale_range = [rgb2hex(i, i, i) for i in range(0, 256, 15)]
  60. grayscale_range = grayscale_range + grayscale_range[::-1]
  61.  
  62. def display(img):
  63.     tkimg = ImageTk.PhotoImage(img)
  64.     canvas.create_image((cx, cy), image=tkimg)
  65.     canvas.update()
  66.  
  67. sz = 16
  68. c = 0
  69. xy = range(0, 512, sz)
  70. for y in xy:
  71.     c = (c + 3) % 11
  72.     for x in xy:
  73.         color = colors.pop(c)
  74.         colors.append(color)
  75.         draw[1].rectangle((x, y, x+sz, y+sz), fill=color, outline=color)
  76.         c = (c + 1) % 11
  77. draw['source'] = img.filter(ImageFilter.GaussianBlur(radius=blur_radius))
  78. source = {}
  79. target = {}
  80. for y in range(hh):
  81.     for x in range(ww):
  82.         source[x,y] = draw['source'].getpixel((x, y))
  83.        
  84. o255 = [i for i in range(256)]
  85. o255 = o255[1:-1] + o255[::-1]
  86. Lc = len(o255)
  87.    
  88. while 1:
  89.     for name, data in filters.items():
  90.         # Extract filter and range from data dictionary
  91.         filter_type = data['filter']
  92.         range_ = data['range']
  93.         # Apply the filter for each range value and display the result
  94.        
  95.         for value in float_range(range_[0], range_[1], range_[2]):
  96.             value = round(value, 2)
  97.             for y in xy:
  98.                 c = (c + 1) % 11
  99.                 for x in xy:
  100.                     color = colors.pop(c)
  101.                     colors.append(color)
  102.                     draw[0].rectangle((x, y, x+sz, y+sz), fill=color, outline=color)
  103.                     c = (c + 1) % 11
  104.             draw['target'] = img.filter(ImageFilter.GaussianBlur(radius=blur_radius))
  105.            
  106.             for i in range(0, 100):
  107.                 sss = f'{name} {value}'
  108.                 alpha = i / 100.0
  109.                 draw['image'] = Image.blend(draw['source'], draw['target'], alpha)
  110.  
  111.                 filtered_image = draw['image'].filter(filter_type)
  112.                
  113.                 mask = Image.new('RGBA', filtered_image.size, color=(0, 0, 0, 128))
  114.                 draw['image'].paste(filtered_image, mask=mask)
  115.                
  116.                 text_img = Image.new('RGBA', (ww, hh), (0, 0, 0, 0))
  117.                 text_draw = ImageDraw.Draw(text_img)
  118.                 text_width, text_height = text_draw.textsize(sss, my_font)
  119.                 x = (img.width - text_width) // 2
  120.                 y = hh // 2
  121.                 grayscale_color = grayscale_range.pop(0)
  122.                 grayscale_range.append(grayscale_color)
  123.                 text_draw.text((x, y), sss, font=my_font, fill=grayscale_color)
  124.                 draw['image'].paste(text_img, mask=text_img)
  125.                 display(draw['image'])
  126.             draw['source'] = copy.deepcopy(draw['target'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement