def cartesian2logpolar(img, center=None, final_radius=None, initial_radius = None, phase_width = 3000): """ See also: https://en.wikipedia.org/wiki/Log-polar_coordinates """ if center is None: center=(img.shape[0]/2,img.shape[1]/2) if final_radius is None: final_radius=max(img.shape[0],img.shape[1])/2 if initial_radius is None: initial_radius = 0 phase_width=img.shape[0]/2 theta , R = np.meshgrid(np.linspace(0, 2*np.pi, phase_width), np.arange(initial_radius, final_radius)) Xcart = np.exp(R) * np.cos(theta) + center[0] Ycart = np.exp(R) * np.sin(theta) + center[1] Xcart = Xcart.astype(int) Ycart = Ycart.astype(int) if img.ndim ==3: polar_img = img[Ycart,Xcart,:] polar_img = np.reshape(polar_img,(final_radius-initial_radius,phase_width,img.shape[-1])) else: polar_img = img[Ycart,Xcart] polar_img = np.reshape(polar_img,(final_radius-initial_radius,phase_width)) return polar_img
def marble(size=(256,256),xPeriod=3.0,yPeriod=10.0,turbPower=0.8,turbSize=None,seed=None): """ Generate a marble texture akin to blender and others :param xPeriod: defines repetition of marble lines in x direction :param yPeriod: defines repetition of marble lines in y direction :param turbPower: add twists and swirls (when turbPower==0 it becomes a normal sine pattern) :param turbSize: initial size of the turbulence default is max(w,h)/10 NOTE: xPeriod and yPeriod together define the angle of the lines xPeriod and yPeriod both 0 then it becomes a normal clouds or turbulence pattern Optimized version of: https://lodev.org/cgtutor/randomnoise.html """ w,h=size turb=turbulence(size,turbSize,seed=seed)*turbPower # determine where the pixels will cycle cycleValue=np.ndarray((w,h)) for y in range(h): for x in range(w): cycleValue[x,y]=(x*xPeriod/w)+(y*yPeriod/h) # add in the turbulence and then last of all, make it a sinewave img=np.abs(np.sin((cycleValue+turb)*math.pi)) return img
def cartesian2polar(img, center=None, final_radius=None, initial_radius = None, phase_width = 3000): """ Comes from: https://stackoverflow.com/questions/9924135/fast-cartesian-to-polar-to-cartesian-in-python """ img=numpyArray(img) if center is None: center=(img.shape[0]/2,img.shape[1]/2) if final_radius is None: final_radius=max(img.shape[0],img.shape[1])/2 if initial_radius is None: initial_radius = 0 phase_width=img.shape[0]/2 theta , R = np.meshgrid(np.linspace(0, 2*np.pi, phase_width), np.arange(initial_radius, final_radius)) Xcart = R * np.cos(theta) + center[0] Ycart = R * np.sin(theta) + center[1] Xcart = Xcart.astype(int) Ycart = Ycart.astype(int) if img.ndim ==3: polar_img = img[Ycart,Xcart,:] polar_img = np.reshape(polar_img,(final_radius-initial_radius,phase_width,img.shape[-1])) else: polar_img = img[Ycart,Xcart] polar_img = np.reshape(polar_img,(final_radius-initial_radius,phase_width)) return polar_img
def woodRings(size=(256,256),xyPeriod=12.0,turbPower=0.15,turbSize=32.0,seed=None): """ Draw wood rings :param xyPeriod: number of rings :param turbPower: makes twists :param turbSize: # initial size of the turbulence Optimized version of: https://lodev.org/cgtutor/randomnoise.html """ turb=turbulence(size,turbSize,seed=seed)*turbPower dc=normalize(deltaC(size,magnitude=True)) img = np.abs(np.sin(xyPeriod * (dc+turb) * math.pi)) return normalize(img)
def clock2(size=(256,256),waveform="sine",frequency=(10,10),noise=0.2,noiseBasis="perlin", noiseOctaves=4,noiseSoften=0.0,direction=0,invert=False,seed=None): """ a rotary clockface gradient """ noiseSize=max(size)/8 # TODO: pass this value in somehow turb=turbulence(size,noiseSize,seed=seed)*noise #points=np.ndarray((int(size[0]),int(size[1]))) angle=0.75-direction/360.0 dc=deltaC(size) img=np.arctan2(dc[:,:,0],dc[:,:,1])/math.pi img=np.abs(np.sin((((normalize(img)+angle)%1.0)+turb)*math.pi)) img=normalize(img) if invert: img=1.0-img return img
def waveImage(size=(256,256),repeats=2,angle=0,wave='sine',radial=False): """ create an image based on a sine, saw, or triangle wave function """ ret=np.zeros(size) if radial: raise NotImplementedError() # TODO: Implement radial wave images else: twopi=2*pi thetas=np.arange(size[0])*float(repeats)/size[0]*twopi if wave=='sine': ret[:,:]=0.5+0.5*np.sin(thetas) elif wave=='saw': n=np.round(thetas/twopi) thetas-=n*twopi ret[:,:]=np.where(thetas<0,thetas+twopi,thetas)/twopi elif wave=='triangle': ret[:,:]=1.0-2.0*np.abs(np.floor((thetas*(1.0/twopi))+0.5)-(thetas*(1.0/twopi))) return ret
stripes = 37 # number of stripes per wave N = 512 # image size in pixels ite = 30 # iterations phases = np.arange(0, 2 * pi, 2 * pi / ite) image = np.empty((N, N)) d = np.arange(-N / 2, N / 2, dtype=np.float64) xv, yv = np.meshgrid(d, d) theta = np.arctan2(yv, xv) r = np.log(np.sqrt(xv * xv + yv * yv)) r[np.isinf(r) == True] = 0 tcos = theta * np.cos(np.arange(0, pi, pi / k))[:, np.newaxis, np.newaxis] rsin = r * np.sin(np.arange(0, pi, pi / k))[:, np.newaxis, np.newaxis] inner = (tcos - rsin) * stripes cinner = np.cos(inner) sinner = np.sin(inner) i = 0 for phase in phases: image[:] = np.sum(cinner * np.cos(phase) - sinner * np.sin(phase), axis=0) + k plt.imshow(image.copy2numpy(), cmap="RdGy") fig.savefig("quasi-{:03d}.png".format(i), bbox_inches='tight', pad_inches=0) i += 1
def sin(x): return np.abs(np.sin(x*math.pi))