Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_slow_style_transfer.py
- # Note:
- # * For controlled Machine Learning only since extremely slow compared to a neural network
- # * The style_image should be a whole lot larger than the target_image
- from Tkinter import *
- import tkFileDialog
- from cStringIO import StringIO
- from PIL import ImageTk, ImageDraw, ImageChops, Image
- import base64, zlib
- import re
- wt = 1400
- ht = 690
- root = Tk()
- root.geometry("%dx%d+-10+0"%(wt,ht))
- canvas = Canvas(root, width=wt, height=ht)
- canvas.grid()
- ### oFiletype =("png files","*.png"),("all files","*.*")
- oFiletype =("all files","*.*"),("png files","*.png"),("jpg files","*.jpg")
- 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 <<<"
- 0
- class Cv(): pass
- cv=Cv()
- nn = (0,1),(1,0),(1,1),(0,2),(1,2),(2,0),(2,1),(2,2)
- def gray(z):
- return sum(z[:-1])/3
- def blend(rgb):
- zzz = [(z*3+t.g[x,y])/4 for z in rgb[:-1]]
- return zzz+[rgb[-1]]
- 0
- def gradient(x,y):
- def r(x,y):
- return x%xx,y%yy
- '''
- xy = [[x+a,y+b] for a,b in nn]
- x2,y2 = [abs(a-z) for z in xy]
- subj[style_image].xy2p[r(x+x2,y+y2)]
- '''
- xx,yy = ww,hh
- xy = [t.g[min(a,ww-1),min(b,hh-1)] for a,b in ([x,y+1],[x+1,y])]
- a = t.g[x,y]
- d = 4.2
- x2,y2 = [(abs(a-z)/d)*d for z in xy]
- x2 += x
- y2 += y
- for a,b in nn:
- v = list(xy2p[x,y])
- x2,y2 = [(z+v.pop(0)) for z in [x-a,y-b]]
- xx,yy = wS,hS
- return r(int(x2),int(y2))
- 0
- style_image, target_image = 'style', 'target'
- go = 'init'
- while go:
- if go is 'init':
- subj = {target_image: 0, style_image: 0}
- for file in subj:
- ttt = Cv()
- t = 0
- while not t:
- t = b64_zlib()
- try:
- t = base64.decodestring(t)
- t = StringIO(zlib.decompress(t))
- except:
- print ">>> image invalid <<<"
- t = 0
- img = Image.open(t)
- w,h = img.size
- if file is target_image:
- ww,hh = img.size
- else:
- wS,hS = img.size
- ttt.data = img.convert("RGBA")
- pixels = ttt.data.load()
- xy2num = {}
- xy2p = {}
- g = {}
- rgb = []
- c = 0
- for y in range(h):
- for x in range(w):
- p = pixels[x,y]
- xy2p[x,y] = p
- xy2num[x,y] = c
- if file is target_image:
- g[x,y] = gray(p)
- rgb.append(p)
- c += 1
- ttt.g = g
- ttt.xy2p = xy2p
- ttt.xy2num = xy2num
- ttt.rgb = rgb
- ttt.img = img
- subj[file] = ttt
- #
- photo_A = ImageTk.PhotoImage(subj[style_image].img)
- img_A = canvas.create_image(5, 10+ht/2, image=photo_A, anchor='w')
- if 1:
- t = subj[target_image]
- rgb = t.rgb[:]
- for c in range(len(rgb)):
- x = c%ww
- y = c/hh
- g = gradient(x,y)
- p = subj[style_image].xy2p[g]
- xy2p[x,y] = p
- p = tuple(blend(p))
- rgb[c] = p
- '''
- rgb = rgb.replace(str(x),str(y))
- '''
- img = Image.new('RGBA', (ww,hh))
- img.putdata(rgb)
- photo_B = ImageTk.PhotoImage(img) # subj[target_image].img
- img_B = canvas.create_image(wt/2, 10+ht/2, image=photo_B, anchor='w')
- root.update()
- break
- #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement