Skip to content

Instantly share code, notes, and snippets.

@Uiuran
Last active December 15, 2015 12:39
Show Gist options
  • Save Uiuran/5261554 to your computer and use it in GitHub Desktop.
Save Uiuran/5261554 to your computer and use it in GitHub Desktop.
Generative texture research
import Image
import numpy as np
import scipy
# To test the codes put a number between [1,2] in the variable do_code
do_code = 2;
if do_code == 1:
import kuramotoGrid as kg
K = 20.0
x = np.random.uniform(0.0,2*np.pi,(80,80));
x.reshape(-1);
# Frequencia dos osciladores, aqui vao as frequencias encontradas na analise da imagem
w = np.random.uniform(0.0,1.0,(80,80));
t = scipy.arange(0,200,0.01);
solv = kg.odeint(kg.kuramotoGrid,x,t,(K,w))
for i in range(20000):
img = Image.new('L',(80,80),255)
last = 255*np.sin(solv[i])
img.putdata(last)
img.save("randomFreq"+str(i)+".jpg")
####### Indo e voltado de imagem para array 2D de array 2D para imagem
#img = Image.Image.load('penalva/r.0.jpg')
#img = img.convert('L')
#data = np.array(list(img.getdata()))
#data = data.reshape((80,80))
#data = data.reshape(-1) # this function is flattering the matrix to a 1D object
#data = data.tolist()
#img = Image.new('L',(80,80),255)
#img.putdata(data)
# Log-energia de fourier de pedacos 10 x 10 de uma figura
#########################################################
elif do_code == 2:
from scipy import fftpack
img = Image.open('penalva/r.0.jpg');
img = img.convert('L');
s = [img.size[0], img.size[1]];
pttlen = 80;
arr = np.array(list(img.getdata()));
arr = arr.reshape((800,800));
# energia = dict( [[(i,j), np.log(fftpack.fft2(arr[i*s[0]/pttlen:(i+1)*s[0]/pttlen,j*s[1]/pttlen:(j+1)*s[1]/pttlen]).real**2 + fftpack.fft(arr[i*s[0]/pttlen:(i+1)*s[0]/pttlen,j*s[1]/pttlen:(j+1)*s[1]/pttlen]).imag**2)] for i in range(pttlen) for j in range(pttlen)]);
meanpix = dict( [[(i,j), np.mean(arr[i*s[0]/pttlen:(i+1)*s[0]/pttlen,j*s[1]/pttlen:(j+1)*s[1]/pttlen])] for i in range(pttlen) for j in range(pttlen)]);
normpix = float(np.max(meanpix.values()))
import kuramotoGrid as kg
K = 0.05
x = np.random.uniform(0.0,2*np.pi,(pttlen,pttlen));
x = x.reshape(-1);
# Frequencia dos osciladores, aqui vao as frequencias encontradas na analise da imagem
w = np.zeros((pttlen,pttlen))
for v in meanpix.keys():
data = meanpix[v];
# norm = float(max(energia[v].reshape(-1)))
# for i in range(s[0]/pttlen):
# for j in range(s[1]/pttlen):
# if data[i,j]/norm > 0.7:
# w[v[0],v[1]] = w[v[0],v[1]] + (i/float(s[0]/pttlen))**2 + (j/float(s[1]/pttlen))**2;
# w[v[0],v[1]] = 2*np.pi*np.sqrt(w[v[0],v[1]]);
w[v[0],v[1]] = meanpix[v]/normpix
t = scipy.arange(0,120,0.01);
solv = kg.odeint(kg.kuramotoGrid,x,t,(K,w))
freq = np.array([kg.kuramotoGrid(v,0,K,w) for v in solv])
freq = np.mean(freq, axis = 0);
freq = 255*freq/np.max(freq)
# freq = freq.reshape((pttlen,pttlen));
# print freq
freq = freq.tolist()
# for i in range(10000):
img = Image.new('L',(80,80),255)
# freq = 255*np.sin(solv[i])
img.putdata(freq)
img.save("PixK_0.05_t120_fig0.jpg")
# This rebuilds the original imagem in the frequency domain of window length s[0]/pttlen x s[1]/pttlen
elif do_code == 3:
fourierimage = Image.new('L',(s[0],s[1]),255);
for v in energia.keys():
b0 = Image.new('L',(s[0]/pttlen,s[1]/pttlen),255);
data = energia[v];
data = data.reshape(-1);
data = data.tolist();
b0.putdata(data);
fourierimage.paste(b0,(v[0]*(s[0]/pttlen),v[1]*(s[1]/pttlen)))
fourierimage.show();
else:
print 'Nothing to do, call your friends and go to the beach to drink coconut water and feel what nature and life is really about !!!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment