Advertisement
here2share

# Tk_izip_RGB.py

Sep 21st, 2021
1,071
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.51 KB | None | 0 0
  1. # Tk_izip_RGB.py
  2.  
  3. from Tkinter import *
  4. from random import *
  5. from math import *
  6. from itertools import izip, product
  7. from PIL import Image, ImageTk
  8.  
  9. def rgb2hex(r,g,b):
  10.     return '#%02X%02X%02X'%(r,g,b)
  11.  
  12. root = Tk()
  13.  
  14. root.title("izip RGB")
  15. frame = Frame(root)
  16. frame.pack()
  17.  
  18. ww = 512
  19. hh = 512
  20.  
  21. canvas = Canvas(frame, width=ww, height=hh, bg="gray")
  22. canvas.pack(side=TOP)
  23.  
  24. BITS = 6
  25. SIZE = int(((2 ** BITS) ** 3) ** 0.5)
  26. STEP = 2 ** (8 - BITS)
  27.  
  28. def color_func(color):
  29.     r, g, b = color
  30.     r, g, b = 0.30 * r, 0.59 * g, 0.11 * b
  31.     return r + g + b
  32.  
  33. # def index_func(index):
  34. #    x = index % SIZE
  35. #    y = index / SIZE
  36. #    bx = bin(x | SIZE)[3:]
  37. #    by = bin(y | SIZE)[3:]
  38. #    bz = ''.join(b + a for a, b in izip(bx, by))
  39. #    z = int(bz, 2)
  40. #    return z
  41.  
  42. def index_func(index):
  43.     x, y = index % SIZE, index / SIZE
  44.     x, y = x - SIZE / 2, y - SIZE / 2
  45.     return x * x + y * y
  46.  
  47. def create_data(indexes, colors):
  48.     result = [None] * (SIZE * SIZE)
  49.     for index, color in izip(indexes, colors):
  50.         result[index] = color
  51.     return result
  52.     '''
  53.         r, g, b = color
  54.         result[index] = chr(r) + chr(g) + chr(b)
  55.     return ''.join(result)
  56.     '''
  57.  
  58.  
  59. indexes = sorted(xrange(SIZE * SIZE), key=index_func)
  60. colors = sorted(product(range(0, 256, STEP), repeat=3), key=color_func)
  61. rgb = create_data(indexes, colors)
  62.  
  63. img = Image.new("RGB",(ww, hh))
  64.  
  65. img.putdata(rgb)
  66. imgTk = ImageTk.PhotoImage(img)
  67. canvas.create_image(0, 0, anchor=NW, image=imgTk)
  68. canvas.update()
  69.    
  70.  
  71. '''
  72. for y in range(hh):
  73.     for x in range(ww):
  74.  
  75. root.mainloop()
  76. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement