def import_faces(path=''): "call import_faces() to obtain a list of images of faces" faces = {} for i in range(1, 21): name = path + ('faces/img%02d.png' % i) print('Reading '+name) faces[i-1] = image.file2image(name) return faces
def loadImageRotateAndPlot(imageFile, theta): data = image.color2gray(image.file2image(imageFile)) row = len(data) col = len(data[0]) S = rotate([ x+(y*1j) for y in range(len(data)) for x in range(len(data[y])) if data[row-y-1][x] < 120 ], theta) plotting.plot(S, max(row * 1.5, col * 1.5), 1)
def loadImageAndPlot(imageFile): data = image.color2gray(image.file2image(imageFile)) row = len(data) col = len(data[0]) S = [ x+(y*1j) for y in range(len(data)) for x in range(len(data[y])) if data[row-y-1][x] < 120 ] plotting.plot(S, max(row, col), 1)
def load_images(directoryname, num_faces=20): #loads the given number of image files from the classified files #returns a dict of face number to image files return { i: color2gray(file2image(os.path.join(directoryname, "img%02d.png" % i))) for i in range(num_faces) }
def import_unclassified(path=''): "call import_faces() to obtain a list of images of things including faces" unclassified = {} for i in range(1, 12): name = path + ('unclassified/img%02d.png' % i) print('Reading '+name) unclassified[i-1] = image.file2image(name) return unclassified
def T2410(filename): data = image.file2image(filename) pts = set() for x in range(166): for y in range(189): if data[y][x][0] < (0.5*255): pts.add(x-(y-189)*1j) return pts
def T2410(filename): data = image.file2image(filename) pts = set() for x in range(166): for y in range(189): if data[y][x][0] < (0.5 * 255): pts.add(x - (y - 189) * 1j) return pts
def load_images(path, n = 20): ''' Input: - path: path to directory containing img*.png - n: number of images to load Output: - dict mapping numbers 0 to (n-1) to a list of rows, each of which is a list of pixel brightnesses ''' return {i:color2gray(file2image(os.path.join(path,"img%02d.png" % i))) for i in range(n)}
def png2graymatrix(filename): """ Converts an image file to a Matrix object where each value is the grayscale value of a pixel. Uses functions from image.py by Philip N. Klein. """ image_data = file2image(filename) if (isgray(image_data) == False): image_data = color2gray(image_data) return Matrix(image_data)
def png2graymatrix(filename): #FIXME: a single line of code should go here image_data = image.file2image(filename) if not image.isgray(image_data): image_data = image.color2gray( image_data) #FIXME: make the image grayscale return Matrix(image_data) #FIXME
def png2graymatrix(filename): """ takes a png file and returns a Matrix object of the pixels INPUT: filename - the path and filename of the png file OUTPUT: a Matrix object with dimensions m x n, assuming the png file has width = n and height = m, """ #FIXME: a single line of code should go here image_data=image.file2image(filename) if not image.isgray(image_data): image_data = image.color2gray(image_data)#FIXME: make the image grayscale return Matrix(image_data) #FIXME
def loadImageRotateCenterAndPlot(imageFile, theta): data = image.color2gray(image.file2image(imageFile)) row = len(data) col = len(data[0]) yCenter = int(row / 2) xCenter = int(col / 2) toCenter = 0 - xCenter - yCenter * 1j S = rotate([ x+(y*1j)+toCenter for y in range(len(data)) for x in range(len(data[y])) if data[row-y-1][x] < 120 ], theta) plotting.plot(S, max(yCenter * 1.5, xCenter * 1.5), 1)
def loadImageAndPlotToCenterRotateAndScale(imageFile): data = image.color2gray(image.file2image(imageFile)) row = len(data) col = len(data[0]) yCenter = int(row / 2) xCenter = int(col / 2) toCenter = 0 - xCenter - yCenter * 1j S = rotateAndScaled([ x+(y*1j)+toCenter for y in range(len(data)) for x in range(len(data[y])) if data[row-y-1][x] < 120 ]) plotting.plot(S, max(yCenter, xCenter), 1)
def loadImageRotateCenterScaleAndPlot(imageFile, theta, scale): data = image.color2gray(image.file2image(imageFile)) row = len(data) col = len(data[0]) yCenter = int(row / 2) xCenter = int(col / 2) toCenter = 0 - xCenter - yCenter * 1j S = [ math.e**(theta*1j)*scale*(x+(y*1j)+toCenter) for y in range(len(data)) for x in range(len(data[y])) if data[row-y-1][x] < 120 ] plotting.plot(S, max(yCenter * 1.5, xCenter * 1.5), 1)
def task1410(filename): # Errata mentions to use color2gray to convert the 3-tuples that file2image gives into a single number data = color2gray(file2image(filename)) # Original Pseudo-code # for y = 0; y < len(data); y++ # for x = 0; x < len(data[y]); x++ # if(data[y][x] < 120) # pts.add(x + yj) # pts = {x + yj for {y,x} in data if data[y][x] < 120} # Fix this by subtracting the y we get from the height (length of data) pts = [x+(len(data)-y)*1j for y, datay in enumerate(data) for x, intensity in enumerate(datay) if data[y][x] < 120] plot(pts, 190) return pts # Not specified in task, but useful for later manipulation
def image2complex(img_name): import image NP = 0 CP = 0 data = image.color2gray(image.file2image("img01.png")) output = set() for i in reversed(data): for ii in i: NP = NP + 1 if ii < 120: output.add(complex( NP,CP)) NP = 0 CP = CP + 1 return output
def image2complex(img_name): """ IN: img_name - the name of the image to read OUT: set of complex numbers x+yi where the intensity of pixel (x,y) is greater than 120 """ import image NormalPart = 0 ComplexPart = 0 # data is a list of lists such that the intensity of pixel (x,y) is given by data[h-1-y][x] # where h = height of the image data = image.color2gray(image.file2image("img01.png")) output = set() for i in reversed(data): for ii in i: NormalPart += 1 if ii < 120: output.add(complex(NormalPart, ComplexPart)) NormalPart = 0 ComplexPart += 1 return output
def read_image(fname): return color2gray(file2image(fname))
x = [x.real for x in m + t] y = [y.imag for y in m + t] # Plotting the Vectors plt.xlabel("Real") plt.ylabel("Complex") plt.scatter(x, y) plt.xlim(-1, 1) plt.ylim(-1, 1) plt.show() ################################## NUMBER 2 ################################### from image import file2image from plotting import plot data = file2image("img01.png") Y = list(range(0, 189, 1)) X = list(range(0, 166, 1)) pts = [x + (189j + y * -1j) for x in X for y in Y if data[y][x][0] < 120] plot(pts, 300) # translation by 189j and multiplication by -1 was used to make the image # reflect the expected result # Y is the set of numbers from 0-188, as the image is 189 pixels tall # X is the set of numbers from 0-165, as the image is 166 pixels across ################################## NUMBER 3 ################################### data = file2image("img01.png")
def read_image(filepath): return file2image(filepath)
# comprehension to assign to a list pts the set of complex numbers x+yi such # that the image intensity of pixel (x,y) is less than 120, and plot the list # pts. ''' Keep these in mind: Something to do with this statement: [[x for x in row] for row in image] S={2+2j,3+2j,1.75+1j, 2+1j, 2.25+1j, 2.5+1j, 2.75+1j, 3+1j, 3.25+1j} plot(S,4) Use enumerate(List) to return a tuple of (index,List(item)) ''' import image data = image.file2image('img01.png') bw_data = image.color2gray(data) pts = { complex(col_ind, 189 - row_ind) for (row_ind, row) in enumerate(bw_data) for (col_ind, intensity) in enumerate(row) if intensity < 120 } plot(pts, 200, 1) # Task 1.4.11: Write a Python procedure f(z) that takes as an argument a # complex number z so that when f(z) applied to each of the complex numbers in # S, the set of resulting numbers is centered at the origin. Write a # comprehension in terms of S and f whose value is the set of translated
# ROTAING BY 90 DEGREES # a point located at (x,y) must be moved to (-y,x) the complex number located # at(x,y) is x + iy. Multiplying by i yields ix + i^2y, which is ix-y # which is the complex number represented by the point (-y, x) rot_90_deg = {1j*z for z in S} # plot(rot_180_deg|rot_90_deg, 4) # Task 1.4.9: Using a compr(ehension, create a new plot in which the points of S are rotated by # 90 degrees, scaled by 1/2, and then shifted down by one unit and to the right two units. Use # a comprehension in which the points of S are multiplied by one complex number and added to # another. T = { 1j*z * 0.5 + 2-1j for z in S} # plot(T|S,4) # Task 1.4.10: data = file2image('../Material/img01.png') # print(data) height = len(data) width =len(data[0]) pts=[x+(189j+y*-1j) for x in range(width) for y in range(height) if data[y][x][0]<120] print(len(pts)) # points = {x+(y*-1j) for x in range(len(data)) for y in range(len(data[0])) if data[x][y][0] < 120} # print(points)`` # points_rot = {-1j*x for x in points} # plot(points, 200, 1) # plot(set(points|points_rot), 200, 1) # Task 1.4.11: Write a Python procedure f(z) that takes as argument a complex number z so # that when f(z) is applied to each of the complex numbers in S, the set of resulting numbers # is centered at the origin. # def center_at_origin(S):
def file2mat(path, row_labels=('x', 'y', 'u')): """input: a filepath to an image in .png format output: the pair (points, matrix) of matrices representing the image.""" return image2mat(image.file2image(path), row_labels)
def plot_image(): data = file2image("img01.png") plot({ x + ((len(data) - y) * 1j) for x in range(len(data[0])) for y in range(len(data)) if sum(data[y][x]) / 3 < 120 }, 200)
for i in range(2, 8): b = i - 2 print("\n" + "=" * b + " + " + "=" * b) plus_chart({x for x in range(i)}) print("\n" + "=" * b + " * " + "=" * b) mult_chart({x for x in range(i)}) def myplot(numset): X = [x.real for x in numset] Y = [x.imag for x in numset] plt.scatter(X, Y, color='red') plt.show() face = img.file2image('img01.png') height = len(face) width = len(face[0]) complixels = [(width - x) + (height - y) * 1j for y, row in enumerate(face) for x, tup in enumerate(row) if tup[0] < (127.5)] rot90 = [num * 1j for num in complixels] center = [num - (width / 2) - ((height / 2) * 1j) for num in complixels] # myplot(complixels) # myplot(rot90) # myplot(center) plot(complixels) print(face[98][45])
b = i - 2 print("\n" + "="*b + " + " + "="*b) plus_chart({x for x in range(i)}) print("\n" + "="*b + " * " + "="*b) mult_chart({x for x in range(i)}) def myplot(numset): X = [x.real for x in numset] Y = [x.imag for x in numset] plt.scatter(X,Y, color='red') plt.show() face = img.file2image('img01.png') height = len(face) width = len(face[0]) complixels = [(width - x) + (height - y) * 1j for y, row in enumerate(face) for x, tup in enumerate(row) if tup[0] < (127.5)] rot90 = [num * 1j for num in complixels] center = [num - (width / 2) - ((height / 2) * 1j) for num in complixels] # myplot(complixels) # myplot(rot90) # myplot(center) plot(complixels) print(face[98][45]) # 1.7.1
Created on Sun May 12 12:24:03 2019 @author: naveenpc """ from plotting import plot as _plot import image S = { 2 + 2j, 3 + 2j, 1.75 + 1j, 2 + 1j, 2.25 + 1j, 2.5 + 1j, 2.75 + 1j, 3 + 1j, 3.25 + 1j } #_plot(S,4) #print(abs(3+4j)) #print((3+4j).conjugate()) #_plot(set(z+1+2j for z in S)|set(z*(0.5j) for z in S)|set((z*(0.5j))+2-1j for z in S),5) #_plot([z for z in S],5) img_data = image.file2image(r"C:\Users\naveenpc\img01.png") width = len(img_data[0]) height = len(img_data) intensity = 100 #_plot([]) pts = list( complex(w, height - h) for (h, row) in list(enumerate(img_data)) for (w, p) in list(enumerate(row)) if p[0] < intensity) #_plot(pts,200) #n=80 #w=pow(math.e,complex(0,((2*math.pi)/n))) #nthpow = list(pow(w,i) for i in range(n)) #_plot(nthpow,2) #z0=complex(sum([z.real for z in pts])/len(pts),sum([z.imag for z in pts])/len(pts)) #plot(list(z-z0 for z in pts),170) #_plot(list(z*pow(math.e,complex(0,math.pi/4)) for z in S),5)
#task 1.4.3 #plot({1+2j+z for z in S}, scale=5, hpath='htmls/task143.html', plot='plot - translation') #task 1.4.7 #plot({z/2 for z in S}, scale=5, hpath='htmls/task147.html', title='plot - scaled by 1/2') #task 1.4.8 #plot({z*0.5j for z in S}, scale=5, hpath='htmls/task148.html', title='plot - rotated by 90 degree and scaled by 1/2') #task 1.4.9 #plot({z*0.5j+2-1j for z in S}, scale=5, dot_size=2, hpath='htmls/task149.html', title='plot - rotate scale and shift') data = list(reversed(file2image('img01.png'))) #print(len(data)) data = color2gray(data) width = len(data[0]) height = len(data) print(width, height) pts =[complex(x,y) for y in range(len(data)) for x in range(len(data[y])) if data[y][x] < 120] #print(pts) #task 1.4.10 #plot(pts, scale=len(data), dot_size=2, hpath='htmls/task1410.html', title='image filter') #task 1.4.11
def sparsity2d(D_dict): return sum([sum([1 if abs(D_dict[kn][km]) != 0 else 0 for km in D_dict[kn]]) for kn in D_dict]) / len(D_dict) / len(list(D_dict.values())[0]) def listdict2dict(L_dict, i): return dict((k, L_dict[k][i]) for k in L_dict) def listdict2dictlist(listdict): return [listdict2dict(listdict, i) for i in range(len(list(listdict.values())[0]))] def backward2d(dictdict): D_list = dict((k, backward(dictdict[k])) for k in dictdict) L_dict = listdict2dictlist(D_list) L_list = [backward(v) for v in L_dict] return L_list def image_round(image): return [[min(255, abs(round(n))) for n in row] for row in image] from image import file2image, color2gray, image2display img = color2gray(file2image('Dali.png')) # image2display(img) wave_comp = forward2d(img) wave_comp = suppress2d(wave_comp, 100) print('sparsity is %f' % sparsity2d(wave_comp)) dump_img = image_round(backward2d(wave_comp)) image2display(dump_img)
def import_flag(): loaded = image.file2image('special_bases/flag.png') return image.color2gray(loaded)
def file2mat(path, row_labels = ('x', 'y', 'u')): """input: a filepath to an image in .png format output: the pair (points, matrix) of matrices representing the image.""" return image2mat(image.file2image(path), row_labels)
def f_2(z): data = file2image("img01.png") data_set = { x + ((len(data) - y) * 1j) for x in range(len(data[0])) for y in range(len(data)) if sum(data[y][x]) / 3 < 120 } return { (s - z) for s in data_set }
def file2cmlx(fname): img = file2image(fname) return [ complex(col_i, len(img) - row_i) for row_i, row in enumerate(img) \ for col_i, (intensity, _, _) in enumerate(row) \ if intensity < 120]
#2-3 #def solve1(a,b,c): return (c-b)/a #print solve1(10+5j, 5, 20) #2-4 #plot(S, 4) #P.48 2.4.2 Task 2.4.3 #plot({1+2j+z for z in S}.union(S), 4) # add original set use union function #P.50 2.4.3 Task 2.4.7 #half #plot({(1+2j+z)/2 for z in S}.union(S), 4) # add original set use union function #180 degree #plot({-(1+2j+z) for z in S}.union(S), 4) # add original set use union function #P.52 2.4.5 Task 2.4.8 #90 degree #plot({-1/2j*(1+2j+z) for z in S}.union(S), 4) # add original set use union function #P.52 2.4.5 Task 2.4.10 data = file2image('img01.png') data = color2gray(data) # if occur error, make comment in "image.py" at line 98 #pts = [[x + y * 1j for x, pixel in enumerate(row) if pixel < 120] for y, row in enumerate(data)] #pts1 = sum(pts, []) #plot(pts1, 200, 1) pts = [[x + y * 1j for x, pixel in enumerate(row) if pixel < 120] for y, row in enumerate(reversed(data))] pts1 = sum(pts, []) plot(pts1, 200, 1)
# imported method that coverts image file into a 2D list, each element # contains the intesity of pixel(x,y) # ============================================================================= # convert 2D list into a set of complex points. def listList2set(L): points = [] for y in range(len(L)): for x in range(len(L[0])): if L[y][x][0] < 120: points.append(complex(x, len(L) - y)) return points image = image.file2image('img01.png') plotimg = listList2set(image) # scaling the image to .20 of its size" setImg = transform(plotimg, 0.20, 0 + 0j) plotting.plot(setImg) # ============================================================================= # Task 1.4.19 - rotate the above image # ============================================================================= def rotateRad(L, rd): # use euler's method to change the angle's radius (translate the points) return [ complex( z.real * math.cos(rd * math.pi) - z.imag * math.sin(rd * math.pi),
def image(): import image im=image.file2image("dance.png") return im
#Task 1.4.3 def t3(): plot({1 + 2j + z for z in S}, 4) #Task 1.4.8 def t8(): plot(scale(S, (1 / 2) * 1j)) #Task 1.4.9 def t9(): plot(translate(scale(S, (1 / 2) * 1j), 2 - 1j)) data = color2gray(file2image("./img01.png")) pts = {(x - (1j * (y - len(data)))) for y in range(len(data)) for x in range(len(data[0])) if data[y][x] < 120} #Task 1.4.10 def t10(): plot(pts, len(data)) #No idea what this task is even asking. #Task 1.4.11 #def t11()