Advertisement
here2share

# Tk_slow_style_transfer.py

Nov 20th, 2018
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.20 KB | None | 0 0
  1. # Tk_slow_style_transfer.py
  2.  
  3. # Note:
  4. # * For controlled Machine Learning only since extremely slow compared to a neural network
  5. # * The style_image should be a whole lot larger than the target_image
  6.  
  7. from Tkinter import *
  8. import tkFileDialog
  9. from cStringIO import StringIO
  10. from PIL import ImageTk, ImageDraw, ImageChops, Image
  11. import base64, zlib
  12. import re
  13.  
  14. wt = 1400
  15. ht = 690
  16. root = Tk()
  17. root.geometry("%dx%d+-10+0"%(wt,ht))
  18. canvas = Canvas(root, width=wt, height=ht)
  19. canvas.grid()
  20.  
  21. ### oFiletype =("png files","*.png"),("all files","*.*")
  22. oFiletype =("all files","*.*"),("png files","*.png"),("jpg files","*.jpg")
  23. def b64_zlib():
  24.     """Convert a file (specified by a path) into a data URI."""
  25.     path = tkFileDialog.askopenfilename(title = "Select file",filetypes=oFiletype)
  26.     try:
  27.         with open(path, 'rb') as fp:
  28.             data = fp.read()
  29.             compressed = zlib.compress(data)
  30.             b64 = base64.encodestring(compressed)
  31.         return b64
  32.     except:
  33.         print ">>> acceptable image not selected <<<"
  34. 0
  35. class Cv(): pass
  36. cv=Cv()
  37.  
  38. nn = (0,1),(1,0),(1,1),(0,2),(1,2),(2,0),(2,1),(2,2)
  39.  
  40. def gray(z):
  41.     return sum(z[:-1])/3
  42.  
  43. def blend(rgb):
  44.     zzz = [(z*3+t.g[x,y])/4 for z in rgb[:-1]]
  45.     return zzz+[rgb[-1]]
  46. 0
  47. def gradient(x,y):
  48.     def r(x,y):
  49.         return x%xx,y%yy
  50.     '''
  51.     xy = [[x+a,y+b] for a,b in nn]
  52.     x2,y2 = [abs(a-z) for z in xy]
  53.     subj[style_image].xy2p[r(x+x2,y+y2)]
  54.     '''
  55.    
  56.     xx,yy = ww,hh
  57.     xy = [t.g[min(a,ww-1),min(b,hh-1)] for a,b in ([x,y+1],[x+1,y])]
  58.     a = t.g[x,y]
  59.     d = 4.2
  60.     x2,y2 = [(abs(a-z)/d)*d for z in xy]
  61.     x2 += x
  62.     y2 += y
  63.     for a,b in nn:
  64.         v = list(xy2p[x,y])
  65.         x2,y2 = [(z+v.pop(0)) for z in [x-a,y-b]]
  66.     xx,yy = wS,hS
  67.     return r(int(x2),int(y2))
  68. 0
  69. style_image, target_image = 'style', 'target'
  70. go = 'init'
  71. while go:
  72.     if go is 'init':
  73.         subj = {target_image: 0, style_image: 0}
  74.         for file in subj:
  75.             ttt = Cv()
  76.             t = 0
  77.             while not t:
  78.                 t = b64_zlib()
  79.                 try:
  80.                     t = base64.decodestring(t)
  81.                     t = StringIO(zlib.decompress(t))
  82.                 except:
  83.                     print ">>> image invalid <<<"
  84.                     t = 0
  85.             img = Image.open(t)
  86.             w,h = img.size
  87.             if file is target_image:
  88.                 ww,hh = img.size
  89.             else:
  90.                 wS,hS = img.size
  91.             ttt.data = img.convert("RGBA")
  92.             pixels = ttt.data.load()
  93.             xy2num = {}
  94.             xy2p = {}
  95.             g = {}
  96.             rgb = []
  97.             c = 0
  98.             for y in range(h):
  99.                 for x in range(w):
  100.                     p = pixels[x,y]
  101.                     xy2p[x,y] = p
  102.                     xy2num[x,y] = c
  103.                     if file is target_image:
  104.                         g[x,y] = gray(p)
  105.                         rgb.append(p)
  106.                         c += 1
  107.             ttt.g = g
  108.             ttt.xy2p = xy2p
  109.             ttt.xy2num = xy2num
  110.             ttt.rgb = rgb
  111.             ttt.img = img
  112.             subj[file] = ttt
  113.         #
  114.         photo_A = ImageTk.PhotoImage(subj[style_image].img)
  115.         img_A = canvas.create_image(5, 10+ht/2, image=photo_A, anchor='w')
  116.         if 1:
  117.             t = subj[target_image]
  118.             rgb = t.rgb[:]
  119.             for c in range(len(rgb)):
  120.                 x = c%ww
  121.                 y = c/hh
  122.                 g = gradient(x,y)
  123.                 p = subj[style_image].xy2p[g]
  124.                 xy2p[x,y] = p
  125.                 p = tuple(blend(p))
  126.                 rgb[c] = p
  127.                
  128.             '''
  129.                 rgb = rgb.replace(str(x),str(y))
  130.             '''
  131.             img = Image.new('RGBA', (ww,hh))
  132.             img.putdata(rgb)
  133.             photo_B = ImageTk.PhotoImage(img) # subj[target_image].img
  134.             img_B = canvas.create_image(wt/2, 10+ht/2, image=photo_B, anchor='w')
  135.             root.update()
  136.     break
  137. #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement