Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_izip_RGB.py
- from Tkinter import *
- from random import *
- from math import *
- from itertools import izip, product
- from PIL import Image, ImageTk
- def rgb2hex(r,g,b):
- return '#%02X%02X%02X'%(r,g,b)
- root = Tk()
- root.title("izip RGB")
- frame = Frame(root)
- frame.pack()
- ww = 512
- hh = 512
- canvas = Canvas(frame, width=ww, height=hh, bg="gray")
- canvas.pack(side=TOP)
- BITS = 6
- SIZE = int(((2 ** BITS) ** 3) ** 0.5)
- STEP = 2 ** (8 - BITS)
- def color_func(color):
- r, g, b = color
- r, g, b = 0.30 * r, 0.59 * g, 0.11 * b
- return r + g + b
- # def index_func(index):
- # x = index % SIZE
- # y = index / SIZE
- # bx = bin(x | SIZE)[3:]
- # by = bin(y | SIZE)[3:]
- # bz = ''.join(b + a for a, b in izip(bx, by))
- # z = int(bz, 2)
- # return z
- def index_func(index):
- x, y = index % SIZE, index / SIZE
- x, y = x - SIZE / 2, y - SIZE / 2
- return x * x + y * y
- def create_data(indexes, colors):
- result = [None] * (SIZE * SIZE)
- for index, color in izip(indexes, colors):
- result[index] = color
- return result
- '''
- r, g, b = color
- result[index] = chr(r) + chr(g) + chr(b)
- return ''.join(result)
- '''
- indexes = sorted(xrange(SIZE * SIZE), key=index_func)
- colors = sorted(product(range(0, 256, STEP), repeat=3), key=color_func)
- rgb = create_data(indexes, colors)
- img = Image.new("RGB",(ww, hh))
- img.putdata(rgb)
- imgTk = ImageTk.PhotoImage(img)
- canvas.create_image(0, 0, anchor=NW, image=imgTk)
- canvas.update()
- '''
- for y in range(hh):
- for x in range(ww):
- root.mainloop()
- '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement