timeit

Програмиране с Python

Курс във Факултета по Математика и Информатика към СУ

Решение на Работа с картинки от Ангел Новоселски

Обратно към всички решения

Към профила на Ангел Новоселски

Резултати

  • 10 точки от тестове
  • 0 бонус точки
  • 10 точки общо
  • 12 успешни тест(а)
  • 0 неуспешни тест(а)

Код

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def rotate_right(image):
    rotated = list(zip(*image[::-1]))
    return rotated


def rotate_left(image):
    rotated = list(zip(*image))[::-1]
    return rotated


def darken(image, coeficient):
    dark = []
    for ps in image:
        dark.append(list(map(list, ps)))
    for ps in dark:
        for p in ps:
            for i in range(0, len(p)):
                p[i] = (p[i] - int(coeficient*(p[i])))
    darkTuple = []
    for ps in dark:
        darkTuple.append(list(map(tuple, ps)))
    return darkTuple


def lighten(image, coeficient):
    light = []
    for ps in image:
        light.append(list(map(list, ps)))
    for ps in light:
        for p in ps:
            for i in range(0, len(p)):
                p[i] = (
                    p[i] + int(coeficient*(255-p[i])))
    lightTuple = []
    for ps in light:
        lightTuple.append(list(map(tuple, ps)))
    return lightTuple


def invert(image):
    inv = []
    for ps in image:
        inv.append(list(map(list, ps)))
    for ps in inv:
        for p in ps:
            for i in range(0, len(p)):
                p[i] = (255-p[i])
    invTuple = []
    for ps in inv:
        invTuple.append(list(map(tuple, ps)))
    return invTuple


def create_histogram(image):
    histogram = {}
    histogram['red'] = {}
    histogram['green'] = {}
    histogram['blue'] = {}
    for ps in image:
        for p in ps:
            for i in range(0, len(p)):
                if(i == 0):
                    if(p[i] in histogram['red']):
                        histogram['red'][p[i]] = 1 + histogram['red'][p[i]]
                    else:
                        histogram['red'][p[i]] = 1
                if(i == 1):
                    if(p[i] in histogram['green']):
                        histogram['green'][p[i]] = 1 + histogram['green'][p[i]]
                    else:
                        histogram['green'][p[i]] = 1
                if(i == 2):
                    if(p[i] in histogram['blue']):
                        histogram['blue'][p[i]] = 1 + histogram['blue'][p[i]]
                    else:
                        histogram['blue'][p[i]] = 1
    return histogram

Лог от изпълнението

............
----------------------------------------------------------------------
Ran 12 tests in 0.084s

OK

История (1 версия и 0 коментара)

Ангел обнови решението на 14.03.2016 16:24 (преди почти 2 години)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def rotate_right(image):
    rotated = list(zip(*image[::-1]))
    return rotated


def rotate_left(image):
    rotated = list(zip(*image))[::-1]
    return rotated


def darken(image, coeficient):
    dark = []
    for ps in image:
        dark.append(list(map(list, ps)))
    for ps in dark:
        for p in ps:
            for i in range(0, len(p)):
                p[i] = (p[i] - int(coeficient*(p[i])))
    darkTuple = []
    for ps in dark:
        darkTuple.append(list(map(tuple, ps)))
    return darkTuple


def lighten(image, coeficient):
    light = []
    for ps in image:
        light.append(list(map(list, ps)))
    for ps in light:
        for p in ps:
            for i in range(0, len(p)):
                p[i] = (
                    p[i] + int(coeficient*(255-p[i])))
    lightTuple = []
    for ps in light:
        lightTuple.append(list(map(tuple, ps)))
    return lightTuple


def invert(image):
    inv = []
    for ps in image:
        inv.append(list(map(list, ps)))
    for ps in inv:
        for p in ps:
            for i in range(0, len(p)):
                p[i] = (255-p[i])
    invTuple = []
    for ps in inv:
        invTuple.append(list(map(tuple, ps)))
    return invTuple


def create_histogram(image):
    histogram = {}
    histogram['red'] = {}
    histogram['green'] = {}
    histogram['blue'] = {}
    for ps in image:
        for p in ps:
            for i in range(0, len(p)):
                if(i == 0):
                    if(p[i] in histogram['red']):
                        histogram['red'][p[i]] = 1 + histogram['red'][p[i]]
                    else:
                        histogram['red'][p[i]] = 1
                if(i == 1):
                    if(p[i] in histogram['green']):
                        histogram['green'][p[i]] = 1 + histogram['green'][p[i]]
                    else:
                        histogram['green'][p[i]] = 1
                if(i == 2):
                    if(p[i] in histogram['blue']):
                        histogram['blue'][p[i]] = 1 + histogram['blue'][p[i]]
                    else:
                        histogram['blue'][p[i]] = 1
    return histogram