diff --git a/a.py b/a.py new file mode 100644 index 0000000..efb90f1 --- /dev/null +++ b/a.py @@ -0,0 +1,3 @@ + + +print "czesc" diff --git a/matrix.py b/matrix.py new file mode 100644 index 0000000..b12fcbf --- /dev/null +++ b/matrix.py @@ -0,0 +1,39 @@ + + +def reduce(matIn, n_size): + nx, ny = n_size + sx = len(matIn) + sy = len(matIn[0]) + + if sx < nx or sy < ny: + raise Exception("New size should be smaller then origin") + + nxs = sx / nx + nys = sy / nx + + mat = [[0. for i in range(nx)] for j in range(ny)] + + for i in range(nx): + for j in range(ny): + x = int(i*nxs) + y = int(j*nys) + ids = 1 + s = matIn[x][y] + for xi in range(-1, 2, 2): + for yj in range(-1, 2, 2): + ii = x+xi + jj = y + yj + if 0 <= ii < sx and 0 <= jj < sy: + s += matIn[x+xi][y+yj] + ids += 1 + + mat[i][j] = s/ids + + return mat + + +# m = [[float(i*j) for i in range(5)] for j in range(5)] +# +# print np.array(m) +# +# print np.array(reduce(m, (3, 3))) diff --git a/solution.py b/solution.py new file mode 100644 index 0000000..60a7626 --- /dev/null +++ b/solution.py @@ -0,0 +1,60 @@ +import matplotlib.pyplot as plt +import matplotlib.image as mpimg +import matplotlib.colors as colors +import numpy as np + +import matrix + +out = "output.txt" +infile ='input.png' + +img = mpimg.imread(infile) + +nsize = 20 + +nonreduced = np.copy(img) + +R = matrix.reduce( img[:, :, 0], (nsize, nsize)) +G = matrix.reduce( img[:, :, 1], (nsize, nsize)) +B = matrix.reduce( img[:, :, 2], (nsize, nsize)) + +# old fashion +# img = np.array( [ [ [R[i][j], G[i][j], B[i][j]] for j in range(nsize)] for i in range(nsize)] ) +# or +img = np.zeros((nsize, nsize, 3)) +img[:, :, 0] = R +img[:, :, 1] = G +img[:, :, 2] = B + + +hsvimg = colors.rgb_to_hsv(img) +hsvimg[:, :, 1] = 0 + +cliped = np.copy(hsvimg) + +# old fashion +# dark = np.array( [ [ [False] for j in range(nsize) ] for i in range(nsize)] ) +# light = np.array( [ [ [False] for j in range(nsize) ] for i in range(nsize)] ) +# or +dark = np.zeros((nsize, nsize), dtype=bool) +light = np.copy(dark) + +for i in range(nsize): + for j in range(nsize): + if hsvimg[i, j, 2] < 0.3: + cliped[i, j, 2] = 0 + dark[i, j] = True + elif hsvimg[i, j][2] < 0.6: + cliped[i, j][2] = .3 + light[i, j] = True + else: + cliped[i, j, 2] = 1 + + +with open(out, 'w') as f: + for d, l in zip(dark, light): + line = np.array([" "]*nsize) + line[d] = "||" + line[l] = "- " + f.write("".join(line)) + f.write("\n") \ No newline at end of file