Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_cycledelic.py
- from Tkinter import *
- import tkFileDialog
- from cStringIO import StringIO
- from PIL import ImageTk, ImageDraw, ImageChops, Image
- import base64, zlib
- import re
- wt = 180
- ht = 180
- root = Tk()
- root.geometry("%dx%d+0+0"%(wt,ht))
- canvas = Canvas(root, width=wt, height=ht)
- canvas.grid()
- oFiletype =("png files","*.png"),("all files","*.*")
- def b64_zlib():
- """Convert a file (specified by a path) into a data URI."""
- path = tkFileDialog.askopenfilename(title = "Select file",filetypes=oFiletype)
- try:
- with open(path, 'rb') as fp:
- data = fp.read()
- compressed = zlib.compress(data)
- b64 = base64.encodestring(compressed)
- return b64
- except:
- print ">>> acceptable image not selected <<<"
- b64='''\
- eJzt20Fu2zAQheGr9ABdNAXa+ygh4b2XPn1txWkUkRzOm3lDE47+rWX4wyMke+OXv79+vvz5/WM5
- Ojr6Fr3Wa7/hVG8krk1s4CKIoq1mvIg4tlHF2wI1OpZQidsQtbprl6G6FQjo/BuivNe3N9TnABp0
- twYJYd2dZwBahGbdmAldPAMQvJOdvOgzdvNigQReJJDCiwOSeFE3CY0XsyCRF/EcpPL4QJjH9nWA
- ZB57QDqPC4R5ET4BGMBjAiPmY55wDI/mC5qPBozikb6Hw+YjDRjHo/gC56MAI3kEX+h8hGf00/kQ
- HuGAJ/fBvGjfaazPO2A078l9MC/ed/pmvsuXZvJdmk3ga+N2xLE+pW5DHO9T4f4LYd/J6dPzVuFY
- H4RbG+pLuC+P86Vk8WVUaPUlqw8DGu/f9J7JBwltvuTzAUCTL1l9OaNAiy/5fWqgwZcYPi1wgYGJ
- 49MBF9iXWD4VEPYlnk8DRH0peYA5o8AFBHJ9XeAC+va8yXwFDwQWvh4Q81V4kK/k9YB7nwwc7it4
- oq/Gg4A1nwgsfRLQ66vyJF+FJ/jqPLdPANZ8bWDDpwY2eAIQ8rV4aiDsq/LCfE0e6GsA2zy3rwFs
- 8BpAwacCCrxwnwIo8erAJq8KFHkRPoFXA8q+LlDmVXwiD/d1gB1eBSj7CmCPx/Z1eAWw6xOBXd7e
- 1+XhPgHY5+G+BeW1gQreDqjgfRWqfA2gipdxHu6rA2GfkrcVKn0VoE639el5uK8AannZwvsUqn3J
- yMs23gdQ70s2XrbpPoCA71MI6O4+A+9dCPmSgZfNuhWI+VYhxsu+f0ihvgTqrnl4yxnTna9hurOL
- t9w+ENJBwtvFfp9SeN6k1nF8feK5SIXj+SRiiesYt9fwfDVkk9ZCFi+TffQO3+F7INDJm953mdwX
- PKCbd/imBk7uI/Cm9wUCKbxA4OQ+Em96XxCQxgsCTu4j8iKAVN70PjqQzGMD6bzpfdRf+gE85oIh
- PJ4wikfyhfE4wkgewRfK8wujeT5gvM4FHMIzC0fpjMCBPINwrA4VjtchwsfotMLH6frGR9vuTY27
- N7Pt6OhJ+wdXRhbz'''
- # b64 = b64_zlib()
- rgb='''
- a (0,0,0,0)
- z (255,255,255,255)
- r (255,0,0,255)
- g (0,255,0,255)
- b (0,0,255,255)
- c (0,255,255,255)
- d (255,0,255,255)
- e (255,255,0,255)
- f (0,0,0,255)
- '''.splitlines()
- abcX={}
- rgbX={}
- for z in rgb:
- if z:
- k,v = z.split()
- abcX[k]=eval(v)
- rgbX[eval(v)]=k
- #
- class Cv(): pass
- cv=Cv()
- #
- rainbow=[]
- def z(r,g,b):
- rainbow.append((r,g,b))
- r,g,b=255,0,0
- for g in range(256):
- z(r,g,b)
- for r in range(254, -1, -1):
- z(r,g,b)
- for b in range(256):
- z(r,g,b)
- for g in range(254, -1, -1):
- z(r,g,b)
- for r in range(256):
- z(r,g,b)
- for b in range(254, -1, -1):
- z(r,g,b)
- rainbow*=2
- def IO(k):
- if k > 128: return 255
- return 0
- def rgba(i,j):
- return [IO(k) for k in pixels[i, j][:-1]]
- ttt = 0
- if b64:
- b64 = base64.decodestring(b64)
- try:
- z = StringIO(zlib.decompress(b64))
- img = Image.open(z)
- wt,ht = img.size
- except:
- ttt,rgb = zlib.decompress(b64).split()
- wt,ht = [int(z) for z in ttt.split(',')]
- if ttt:
- for t in abcX:
- rgb = rgb.replace(t,str(abcX[t])+',')
- rgb = '['+rgb[:-1]+']'
- img = Image.new('RGBA', (wt,ht))
- img.putdata(eval(rgb))
- #
- image = img.convert("RGBA")
- if not ttt:
- s=''
- pixels = image.load()
- ttt = []
- for i in range(ht):
- for j in range(wt):
- a = pixels[j,i][-1]
- r, g, b = rgba(j,i)
- pixels[j,i] = (r, g, b, a)
- z=(r, g, b, a)
- s+=rgbX[z]
- ttt.append(z)
- rgb=str(ttt)
- t = set(ttt)
- compressed = zlib.compress('%s,%s %s'%(wt,ht,s[::-1]))
- b64 = base64.encodestring(compressed)
- print "b64_zlib='''\\\n"+b64+"'''"
- #
- class Cv(): pass
- cv=Cv()
- #
- t = re.findall('([0-9 ,]{3,})',rgb)
- t = list(set(t))
- while 1:
- t2=[]
- for k in t:
- r,g,b,a = eval(k)
- if (r,g,b) in rainbow:
- r,g,b = rainbow[rainbow.index((r,g,b))+32]
- t2.append(str((r,g,b,a)))
- rgb = rgb.replace(k,t2[-1])
- t=t2[:]
- img = Image.new('RGBA', (wt,ht))
- img.putdata(eval(rgb))
- photo = ImageTk.PhotoImage(img)
- imgx = canvas.create_image(10, 90, image=photo, anchor='w')
- root.update()
- #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement