In [2]:
%matplotlib inline
In [3]:
import numpy as np; import matplotlib.pyplot as plt; import itertools
import matplotlib.image as mpimg
from colorsys import hls_to_rgb
plt.rcParams['figure.figsize'] = (10,10)
def colorize(z):
n,m = z.shape
c = np.zeros((n,m,3))
c[np.isinf(z)] = (1.0, 1.0, 1.0)
c[np.isnan(z)] = (0.5, 0.5, 0.5)
idx = ~(np.isinf(z) + np.isnan(z))
A = (np.angle(z[idx]) + np.pi) / (2*np.pi)
A = (A + 0.5) % 1.0
B = 1.0 - 1.0/(1.0+abs(z[idx])**0.3)
c[idx] = [hls_to_rgb(a, b, 0.8) for a,b in zip(A,B)]
return c
In [4]:
axis = np.arange(-2*np.pi,2*np.pi,0.01)
extent = (axis[0],axis[-1],axis[0],axis[-1])
xx,yy = np.meshgrid(axis,axis)
zz = xx+yy*1j
In [5]:
def mod(value,modulus):
# modified modulo
return (value-modulus/2)%modulus-modulus/2
def mult(value, modulus):
# how many times the modulo wrapped
return (abs((value-mod(value,modulus))/modulus)).astype(int)
def dogeify(zz, doge):
'''
turns a 2d array of complex numbers into an
array of rgb tuples whose vales correspond to
a repeated image of a shiba inu
'''
img = mpimg.imread(doge)
print "Dogeifying",zz.shape,"array with",img.shape[:2],"doge at",doge
zs_y,zs_x = zz.shape
c = np.zeros((zs_y,zs_x,3),dtype=np.uint8)
sized = (-np.pi, np.pi)
periodd = sized[-1]-sized[0]
is_y,is_x,_ = img.shape
#wrap space: W := [-period/2,period]**2
#map ℂ ⟶ W
frac_x = mod(zz.real,periodd)/periodd
frac_y = mod(zz.imag,periodd)/periodd
#determine the multiplicity of wrapping
mult_x = mult(zz.real,periodd)
mult_y = mult(zz.imag,periodd)
#calculating brightness modifier
# according to wrapping multiplicity
mult_z = np.sqrt(mult_x**2+mult_y**2)
fact = np.exp(-mult_z/5)
#image space: I = range(v_pixels) × range(h_pixels)
#map W ⟶ I
mapped_x = (is_y*(frac_y)).astype(int)+is_y/2
mapped_y = (is_x*(frac_x)).astype(int)+is_x/2
c = np.array(img[mapped_x,mapped_y]*np.dstack([fact,fact,fact]),
dtype=np.uint8)
return c
In [6]:
ff = (zz-(1+1j))**(-1)*(zz-1)*(zz+1)
dogeified = dogeify(ff,'doge.jpg') #doge.jpg is 512x512 image
plt.imshow(dogeified,extent=extent)
Out[6]:
In [322]:
plt.imshow(colorize(ff),extent=extent) #plot regular complex plot
Out[322]:
In [ ]: