Advertisement
here2share

# Tk_cycledelic.py

Nov 16th, 2018
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.13 KB | None | 0 0
  1. # Tk_cycledelic.py
  2.  
  3. from Tkinter import *
  4. import tkFileDialog
  5. from cStringIO import StringIO
  6. from PIL import ImageTk, ImageDraw, ImageChops, Image
  7. import base64, zlib
  8. import re
  9.  
  10. wt = 180
  11. ht = 180
  12. root = Tk()
  13. root.geometry("%dx%d+0+0"%(wt,ht))
  14. canvas = Canvas(root, width=wt, height=ht)
  15. canvas.grid()
  16.  
  17. oFiletype =("png files","*.png"),("all files","*.*")
  18. def b64_zlib():
  19.     """Convert a file (specified by a path) into a data URI."""
  20.     path = tkFileDialog.askopenfilename(title = "Select file",filetypes=oFiletype)
  21.     try:
  22.         with open(path, 'rb') as fp:
  23.             data = fp.read()
  24.             compressed = zlib.compress(data)
  25.             b64 = base64.encodestring(compressed)
  26.         return b64
  27.     except:
  28.         print ">>> acceptable image not selected <<<"
  29.  
  30. b64='''\
  31. eJzt20Fu2zAQheGr9ABdNAXa+ygh4b2XPn1txWkUkRzOm3lDE47+rWX4wyMke+OXv79+vvz5/WM5
  32. Ojr6Fr3Wa7/hVG8krk1s4CKIoq1mvIg4tlHF2wI1OpZQidsQtbprl6G6FQjo/BuivNe3N9TnABp0
  33. twYJYd2dZwBahGbdmAldPAMQvJOdvOgzdvNigQReJJDCiwOSeFE3CY0XsyCRF/EcpPL4QJjH9nWA
  34. ZB57QDqPC4R5ET4BGMBjAiPmY55wDI/mC5qPBozikb6Hw+YjDRjHo/gC56MAI3kEX+h8hGf00/kQ
  35. HuGAJ/fBvGjfaazPO2A078l9MC/ed/pmvsuXZvJdmk3ga+N2xLE+pW5DHO9T4f4LYd/J6dPzVuFY
  36. H4RbG+pLuC+P86Vk8WVUaPUlqw8DGu/f9J7JBwltvuTzAUCTL1l9OaNAiy/5fWqgwZcYPi1wgYGJ
  37. 49MBF9iXWD4VEPYlnk8DRH0peYA5o8AFBHJ9XeAC+va8yXwFDwQWvh4Q81V4kK/k9YB7nwwc7it4
  38. oq/Gg4A1nwgsfRLQ66vyJF+FJ/jqPLdPANZ8bWDDpwY2eAIQ8rV4aiDsq/LCfE0e6GsA2zy3rwFs
  39. 8BpAwacCCrxwnwIo8erAJq8KFHkRPoFXA8q+LlDmVXwiD/d1gB1eBSj7CmCPx/Z1eAWw6xOBXd7e
  40. 1+XhPgHY5+G+BeW1gQreDqjgfRWqfA2gipdxHu6rA2GfkrcVKn0VoE639el5uK8AannZwvsUqn3J
  41. yMs23gdQ70s2XrbpPoCA71MI6O4+A+9dCPmSgZfNuhWI+VYhxsu+f0ihvgTqrnl4yxnTna9hurOL
  42. t9w+ENJBwtvFfp9SeN6k1nF8feK5SIXj+SRiiesYt9fwfDVkk9ZCFi+TffQO3+F7INDJm953mdwX
  43. PKCbd/imBk7uI/Cm9wUCKbxA4OQ+Em96XxCQxgsCTu4j8iKAVN70PjqQzGMD6bzpfdRf+gE85oIh
  44. PJ4wikfyhfE4wkgewRfK8wujeT5gvM4FHMIzC0fpjMCBPINwrA4VjtchwsfotMLH6frGR9vuTY27
  45. N7Pt6OhJ+wdXRhbz'''
  46.  
  47. # b64 = b64_zlib()
  48.  
  49. rgb='''
  50. a (0,0,0,0)
  51. z (255,255,255,255)
  52. r (255,0,0,255)
  53. g (0,255,0,255)
  54. b (0,0,255,255)
  55. c (0,255,255,255)
  56. d (255,0,255,255)
  57. e (255,255,0,255)
  58. f (0,0,0,255)
  59. '''.splitlines()
  60.  
  61. abcX={}
  62. rgbX={}
  63. for z in rgb:
  64.     if z:
  65.         k,v = z.split()
  66.         abcX[k]=eval(v)
  67.         rgbX[eval(v)]=k
  68. #
  69. class Cv(): pass
  70. cv=Cv()
  71. #
  72. rainbow=[]
  73. def z(r,g,b):
  74.     rainbow.append((r,g,b))
  75. r,g,b=255,0,0
  76. for g in range(256):
  77.     z(r,g,b)
  78. for r in range(254, -1, -1):
  79.     z(r,g,b)
  80. for b in range(256):
  81.     z(r,g,b)
  82. for g in range(254, -1, -1):
  83.     z(r,g,b)
  84. for r in range(256):
  85.     z(r,g,b)
  86. for b in range(254, -1, -1):
  87.     z(r,g,b)
  88. rainbow*=2
  89.  
  90. def IO(k):
  91.     if k > 128: return 255
  92.     return 0
  93. def rgba(i,j):
  94.     return [IO(k) for k in pixels[i, j][:-1]]
  95.  
  96. ttt = 0
  97. if b64:
  98.     b64 = base64.decodestring(b64)
  99.     try:
  100.         z = StringIO(zlib.decompress(b64))
  101.         img = Image.open(z)
  102.         wt,ht = img.size
  103.     except:
  104.         ttt,rgb = zlib.decompress(b64).split()
  105.         wt,ht = [int(z) for z in ttt.split(',')]
  106. if ttt:
  107.     for t in abcX:
  108.         rgb = rgb.replace(t,str(abcX[t])+',')
  109.     rgb = '['+rgb[:-1]+']'
  110.     img = Image.new('RGBA', (wt,ht))
  111.     img.putdata(eval(rgb))
  112. #
  113.  
  114. image = img.convert("RGBA")
  115.  
  116. if not ttt:
  117.     s=''
  118.     pixels = image.load()
  119.     ttt = []
  120.     for i in range(ht):
  121.         for j in range(wt):
  122.             a = pixels[j,i][-1]
  123.             r, g, b = rgba(j,i)
  124.             pixels[j,i] = (r, g, b, a)
  125.             z=(r, g, b, a)
  126.             s+=rgbX[z]
  127.             ttt.append(z)
  128.     rgb=str(ttt)
  129.     t = set(ttt)
  130.     compressed = zlib.compress('%s,%s %s'%(wt,ht,s[::-1]))
  131.     b64 = base64.encodestring(compressed)
  132.     print "b64_zlib='''\\\n"+b64+"'''"
  133. #
  134. class Cv(): pass
  135. cv=Cv()
  136. #
  137. t = re.findall('([0-9 ,]{3,})',rgb)
  138. t = list(set(t))
  139.  
  140. while 1:
  141.     t2=[]
  142.     for k in t:
  143.         r,g,b,a = eval(k)
  144.         if (r,g,b) in rainbow:
  145.             r,g,b = rainbow[rainbow.index((r,g,b))+32]
  146.             t2.append(str((r,g,b,a)))
  147.             rgb = rgb.replace(k,t2[-1])
  148.     t=t2[:]
  149.     img = Image.new('RGBA', (wt,ht))
  150.     img.putdata(eval(rgb))
  151.     photo = ImageTk.PhotoImage(img)
  152.     imgx = canvas.create_image(10, 90, image=photo, anchor='w')
  153.     root.update()
  154. #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement