Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_rgb_filter_demo.py ZZZ Sorry, I can't seem to find a way for this to work properly
- from tkinter import *
- from PIL import Image, ImageTk, ImageFilter, ImageDraw, ImageOps, ImageFont
- from math import sin, cos, pi
- import copy
- def float_range(start, stop, step=1.0):
- while start < stop:
- yield start
- start += step
- ww = 512
- hh = 512
- cx, cy = ww//2, hh//2
- root = Tk()
- root.title("# tk_rgb_filter_demo")
- root.geometry("%dx%d+0+0"%(ww,hh))
- def rgb2hex(r,g,b):
- return '#%02X%02X%02X'%(r,g,b)
- rgb = range(0, 256, 50)
- colors = [rgb2hex(r, g, b) for r in rgb for g in rgb for b in rgb]
- img = Image.new('RGB', (ww, hh), (0, 0, 0))
- draw = {}
- for k in (0, 1, 2, 3):
- draw[k] = ImageDraw.Draw(img)
- text_img = Image.new('RGBA', (ww, hh), (0, 0, 0, 0))
- text_draw = ImageDraw.Draw(text_img)
- my_font = ImageFont.truetype('arial.ttf', 24)
- blur_radius = 0.02 * min(img.size)
- canvas = Canvas(root, width=ww, height=hh, bg='white')
- canvas.pack(side=LEFT, fill=BOTH, expand=True)
- # Define filter types
- filters = {
- 'Blur': {'filter': ImageFilter.BLUR, 'range': (0, 10, 1)},
- 'Contour': {'filter': ImageFilter.CONTOUR, 'range': (0, 255, 25)},
- 'Detail': {'filter': ImageFilter.DETAIL, 'range': (0, 5, 0.5)},
- 'Edge Enhance': {'filter': ImageFilter.EDGE_ENHANCE, 'range': (0, 255, 25)},
- 'Edge Enhance More': {'filter': ImageFilter.EDGE_ENHANCE_MORE, 'range': (0, 255, 25)},
- 'Emboss': {'filter': ImageFilter.EMBOSS, 'range': (0, 255, 25)},
- 'Find Edges': {'filter': ImageFilter.FIND_EDGES, 'range': (0, 255, 25)},
- 'Sharpen': {'filter': ImageFilter.SHARPEN, 'range': (0, 10, 1)},
- 'Smooth': {'filter': ImageFilter.SMOOTH, 'range': (0, 10, 1)},
- 'Smooth More': {'filter': ImageFilter.SMOOTH_MORE, 'range': (0, 10, 1)},
- 'Sobel X': {'filter': ImageFilter.Kernel((3,3), [-1,0,1,-2,0,2,-1,0,1]), 'range': (-255, 255, 50)},
- 'Sobel Y': {'filter': ImageFilter.Kernel((3,3), [-1,-2,-1,0,0,0,1,2,1]), 'range': (-255, 255, 50)},
- 'Laplacian': {'filter': ImageFilter.Kernel((3,3), [0,1,0,1,-4,1,0,1,0]), 'range': (0, 255, 25)},
- 'Canny': {'filter': ImageOps.autocontrast(img.filter(ImageFilter.FIND_EDGES), 0), 'range': (0, 255, 25)},
- }
- grayscale_range = [rgb2hex(i, i, i) for i in range(0, 256, 15)]
- grayscale_range = grayscale_range + grayscale_range[::-1]
- def display(img):
- tkimg = ImageTk.PhotoImage(img)
- canvas.create_image((cx, cy), image=tkimg)
- canvas.update()
- sz = 16
- c = 0
- xy = range(0, 512, sz)
- for y in xy:
- c = (c + 3) % 11
- for x in xy:
- color = colors.pop(c)
- colors.append(color)
- draw[1].rectangle((x, y, x+sz, y+sz), fill=color, outline=color)
- c = (c + 1) % 11
- draw['source'] = img.filter(ImageFilter.GaussianBlur(radius=blur_radius))
- source = {}
- target = {}
- for y in range(hh):
- for x in range(ww):
- source[x,y] = draw['source'].getpixel((x, y))
- o255 = [i for i in range(256)]
- o255 = o255[1:-1] + o255[::-1]
- Lc = len(o255)
- while 1:
- for name, data in filters.items():
- # Extract filter and range from data dictionary
- filter_type = data['filter']
- range_ = data['range']
- # Apply the filter for each range value and display the result
- for value in float_range(range_[0], range_[1], range_[2]):
- value = round(value, 2)
- for y in xy:
- c = (c + 1) % 11
- for x in xy:
- color = colors.pop(c)
- colors.append(color)
- draw[0].rectangle((x, y, x+sz, y+sz), fill=color, outline=color)
- c = (c + 1) % 11
- draw['target'] = img.filter(ImageFilter.GaussianBlur(radius=blur_radius))
- for i in range(0, 100):
- sss = f'{name} {value}'
- alpha = i / 100.0
- draw['image'] = Image.blend(draw['source'], draw['target'], alpha)
- filtered_image = draw['image'].filter(filter_type)
- mask = Image.new('RGBA', filtered_image.size, color=(0, 0, 0, 128))
- draw['image'].paste(filtered_image, mask=mask)
- text_img = Image.new('RGBA', (ww, hh), (0, 0, 0, 0))
- text_draw = ImageDraw.Draw(text_img)
- text_width, text_height = text_draw.textsize(sss, my_font)
- x = (img.width - text_width) // 2
- y = hh // 2
- grayscale_color = grayscale_range.pop(0)
- grayscale_range.append(grayscale_color)
- text_draw.text((x, y), sss, font=my_font, fill=grayscale_color)
- draw['image'].paste(text_img, mask=text_img)
- display(draw['image'])
- draw['source'] = copy.deepcopy(draw['target'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement