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 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 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 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 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 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 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 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
#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)
def read_image(fname): return color2gray(file2image(fname))
def import_flag(): loaded = image.file2image('special_bases/flag.png') return image.color2gray(loaded)
# 90도 회전한 이미지가 책과 다르다;;; S1 = {z * 1j for z in S} # plot(S1, 4) # 54p # Task2.4.8 S1 = {z * 1j / 2 for z in S} # plot(S1, 4) # Task2.4.9 S1 = {(z * 1j) / 2 + 2 - 1j for z in S} # plot(S1, 4) # Task2.4.10 # 밝기가 아니라 색상이다. RGB : (183,183,183) # color2gray의 리턴값(밝은 정도) 중에 120보다 작은 값을 가진 픽셀만 추린다. data = file2image('img01.png') data = color2gray(data) 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) # 200은 스케일, 1은 점의 굵기 # > 이미지가 반대로 나온다...x, y가 뒤바뀐듯. # file2image 함수 결과값에서부터 반대로 나온다. # 리스트를 컴프리헨션 할때, 마지막값부터 읽어보자. reversed 이용 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) # 55p # Task2.4.11 복소수의 중심을 구한다. 그러고나서 원점으로 평행이동 def f(zlist): x_min = 0 x_max = 0
#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)
# 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 # points, and plot the value.
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)
#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()