Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download
Project: Peter's Files
Views: 3893
Visibility: Unlisted (only visible to those who know the link)
Image: ubuntu1804
Kernel: Python 3 (Ubuntu Linux)

Iterated Function System Fractal Generator -- In Progress

Back to the Generator

from IFSFGL import *
A = np.array([[0,0,0,0,0,0,0,0,0], [0,0,1,1,1,1,1,0,0], [0,0,1,1,1,1,1,0,0], [0,0,1,1,1,1,1,0,0], [0,0,0,0,0,0,0,0,0]]) B = np.array([[0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [1,1,1,1,1,1,1,1,1], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0]])
## image Analysis def isSimilar(array1, array2): compare = array1+array2 numberOfOnes = sum(sum(compare - 2*(compare//2))) return numberOfOnes
def isSimilar(img1, img2, tolerance): array1 = (255-cv2.imread(img1, cv2.IMREAD_GRAYSCALE))/255 array2 = (255-cv2.imread(img2, cv2.IMREAD_GRAYSCALE))/255 compare = array1+array2 numberOfOnes = sum(sum(compare - 2*(compare//2))) return numberOfOnes <= tolerance
isSimilar(f'Saved Fractals/For Zooms/point2_0.png',f'Saved Fractals/For Zooms/point2_2.png', 5)
False
if frame > 0: for i in range(frame): if isSimilar(f'Saved Fractals/For Zooms/{name}_{frame}.png', f'Saved Fractals/For Zooms/{name}_{i}.png', 5): print(f'---{round(1/scale, 2)}___{round(1/(1 - ((zoom-1)/zoom)*sqrt(i/frames)),2)}---') ## image Analysis def isSimilar(img1, img2, tolerance): array1 = np.ceil((255-cv2.imread(img1, cv2.IMREAD_GRAYSCALE))/255) array2 = np.ceil((255-cv2.imread(img2, cv2.IMREAD_GRAYSCALE))/255) compare = array1+array2 numberOfOnes = sum(sum(compare - 2*(compare//2))) return numberOfOnes <= tolerance
## for GIF making def zoom_in_complex(n, transformations, weights, name, size, steps = 10, endBounds = np.array([0,0,0,0], dtype=np.float64)): start = time.time() print('Generating Zoomed images...') startBounds = find_bounds(transformations, weights) ratio = (startBounds[1]-startBounds[0])/(startBounds[1]-startBounds[0]) points = generate_points_zoom(n, transformations, weights, startBounds) save_points((points.T[0],points.T[1]), size, 'blue','For Zooms/',f'{name}_0') for step in range(1, steps+1): frame = startBounds + np.sqrt(step/steps)*(endBounds-startBounds) # startBounds*((steps-step)/steps)+endBounds*(step/steps) frame[2] = frame[3] - ((frame[1]-frame[0])/ratio) counter = 0 for row in points: if (frame[0]<=row[0]) and (frame[1]>=row[0]) and (frame[2]<=row[1]) and (frame[3]>=row[1]): points[counter] = row counter += 1 points[counter:] = generate_points_zoom(n-counter, transformations, weights, frame, points[counter-1]) save_points((points.T[0],points.T[1]), size, 'blue', 'For Zooms/', f'{name}_{step}','V') print(f' Saved all {steps} images to Saved Fractals/For Zooms.') print(f'Completed in {time.time()-start} seconds.') def gif_maker(name,steps,mode='del'): start = time.time() print('Making GIF...', end='') images = [imageio.imread(f'Saved Fractals/For Zooms/{name}_0.png')]*7 for step in np.arange(steps+1): images.append(imageio.imread(f'Saved Fractals/For Zooms/{name}_{step}.png')) if mode == 'del': os.remove(f'Saved Fractals/For Zooms/{name}_{step}.png') imageio.mimsave(f'Saved Fractals/{name}.gif', images) print(f' finished in {time.time()-start} seconds.') ## Image Analysis def makeKindOfConvexRows(array): for row in array: index = 0 while index < len(row): if row[index] == 1: for i in np.arange(index+1,len(row)): if row[i] == 1: row[index:i] = 1 index = i index = index + 1 return array def makeKindOfConvex(array): return makeKindOfConvexRows(makeKindOfConvexRows(makeKindOfConvexRows(makeKindOfConvexRows(array).T).T).T).T def array_plot(array,size): plt.rcParams['figure.figsize'] = [size,size] img = plt.pcolor(array, alpha=0.8, cmap="tab20", linewidths=0) plt.axis('off') plt.show() def pixel_data(image): # takes a png file img = cv2.imread(image, cv2.IMREAD_GRAYSCALE) new_img = np.ceil((255-img)/255) return (int(sum(sum(new_img))),int(sum(sum(makeKindOfConvex(new_img)))), img.shape) # returns a tuple: (the integer number of non-white pixels of the png, (xlength,ylength)) # image = 'Saved Fractals/proper fern_400000_10.png' # img = cv2.imread(image, cv2.IMREAD_GRAYSCALE) # temp_img = np.ceil((255-img)/255) # for i in np.arange(len(temp_img)): # to flip it back? # img[len(img)-1-i] = temp_img[i] # array_plot(img,10) # array_plot(makeKindOfConvex(img),10) # @njit def force_points(pixelsPerUnit, Transformations, epsilon, Bounds=np.array([0,0,0,0], dtype=np.float64)): numberOfXPoints = round(pixelsPerUnit*(Bounds[1]-Bounds[0])) numberOfYPoints = round(pixelsPerUnit*(Bounds[3]-Bounds[2])) grid = np.array([[0]*int(numberOfXPoints*numberOfYPoints)], dtype=np.float64) finalPoints = np.array([[0,0,0]]*int((numberOfXPoints+1)*(numberOfYPoints+1)), dtype=np.float64) row = 0 for i in np.arange(len(grid)): for j in np.arange(len(grid[0])): P = np.array([Bounds[0]+(j/pixelsPerUnit), Bounds[3]-(i/pixelsPerUnit), 1]) for T in Transformations: Q = T @ P if np.sqrt((P[0]-Q[0])**2+(P[1]-Q[1])**2) < epsilon: finalPoints[row] = P row = row + 1 finalPoints = finalPoints[:row-1].T return (finalPoints[0],finalPoints[1])
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-1-79a8cdf05a82> in <module>() 1 ## for GIF making 2 ----> 3 def zoom_in_complex(n, transformations, weights, name, size, steps = 10, endBounds = np.array([0,0,0,0], dtype=np.float64)): 4 start = time.time() 5 print('Generating Zoomed images...') NameError: name 'np' is not defined
def find_best_data(array1, array2): options = [] R = [] slopes = [] for start in range(len(array1) - 2): for end in range(start + 2, len(array2)): options += [(start, end)] data = linregress(array1[start:end],array2[start:end]) R += [data[2]] slopes += [data[0]] return (R,slopes)