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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
def rotate_left(image):
    new_image = []

    rows = len(image)
    cols = len(image[0])

    for i in range(cols - 1, -1, -1):
        row = []
        for j in range(0, rows):
            row.append(image[j][i])
        new_image.append(row)

    return new_image


def rotate_right(image):
    new_image = []

    rows = len(image)
    cols = len(image[0])

    for i in range(0, cols):
        row = []
        for j in range(rows - 1, -1, -1):
            row.append(image[j][i])
        new_image.append(row)

    return new_image


def invert(image):
    new_image = []

    rows = len(image)
    cols = len(image[0])

    for i in range(0, rows):
        row = []
        for j in range(0, cols):
            row.append((255 - image[i][j][0],
                        255 - image[i][j][1],
                        255 - image[i][j][2]))
        new_image.append(row)

    return new_image


def lighten(image, coef):
    new_image = []

    rows = len(image)
    cols = len(image[0])

    for i in range(0, rows):
        row = []
        for j in range(0, cols):
            row.append((image[i][j][0] + int(coef * (255 - image[i][j][0])),
                        image[i][j][1] + int(coef * (255 - image[i][j][1])),
                        image[i][j][2] + int(coef * (255 - image[i][j][2]))))
        new_image.append(row)

    return new_image


def darken(image, coef):
    new_image = []

    rows = len(image)
    cols = len(image[0])

    for i in range(0, rows):
        row = []
        for j in range(0, cols):
            row.append((image[i][j][0] - int(coef * image[i][j][0]),
                        image[i][j][1] - int(coef * image[i][j][1]),
                        image[i][j][2] - int(coef * image[i][j][2])))
        new_image.append(row)

    return new_image


def create_histogram(image):
    histogram = {
        "red": {},
        "green": {},
        "blue": {},
    }

    rows = len(image)
    cols = len(image[0])

    for i in range(0, rows):
        for j in range(0, cols):
            if image[i][j][0] in histogram["red"].keys():
                histogram["red"][image[i][j][0]] += 1
            else:
                histogram["red"][image[i][j][0]] = 1

            if image[i][j][1] in histogram["green"].keys():
                histogram["green"][image[i][j][1]] += 1
            else:
                histogram["green"][image[i][j][1]] = 1

            if image[i][j][2] in histogram["blue"].keys():
                histogram["blue"][image[i][j][2]] += 1
            else:
                histogram["blue"][image[i][j][2]] = 1

    return histogram

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

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

OK

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

Георги обнови решението на 13.03.2016 14:51 (преди почти 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
def rotate_left(image):
    new_image = []

    rows = len(image)
    cols = len(image[0])

    for i in range(cols - 1, -1, -1):
        row = []
        for j in range(0, rows):
            row.append(image[j][i])
        new_image.append(row)

    return new_image


def rotate_right(image):
    new_image = []

    rows = len(image)
    cols = len(image[0])

    for i in range(0, cols):
        row = []
        for j in range(rows - 1, -1, -1):
            row.append(image[j][i])
        new_image.append(row)

    return new_image


def invert(image):
    new_image = []

    rows = len(image)
    cols = len(image[0])

    for i in range(0, rows):
        row = []
        for j in range(0, cols):
            row.append((255 - image[i][j][0],
                        255 - image[i][j][1],
                        255 - image[i][j][2]))
        new_image.append(row)

    return new_image


def lighten(image, coef):
    new_image = []

    rows = len(image)
    cols = len(image[0])

    for i in range(0, rows):
        row = []
        for j in range(0, cols):
            row.append((image[i][j][0] + int(coef * (255 - image[i][j][0])),
                        image[i][j][1] + int(coef * (255 - image[i][j][1])),
                        image[i][j][2] + int(coef * (255 - image[i][j][2]))))
        new_image.append(row)

    return new_image


def darken(image, coef):
    new_image = []

    rows = len(image)
    cols = len(image[0])

    for i in range(0, rows):
        row = []
        for j in range(0, cols):
            row.append((image[i][j][0] - int(coef * image[i][j][0]),
                        image[i][j][1] - int(coef * image[i][j][1]),
                        image[i][j][2] - int(coef * image[i][j][2])))
        new_image.append(row)

    return new_image


def create_histogram(image):
    histogram = {
        "red": {},
        "green": {},
        "blue": {},
    }

    rows = len(image)
    cols = len(image[0])

    for i in range(0, rows):
        for j in range(0, cols):
            if image[i][j][0] in histogram["red"].keys():
                histogram["red"][image[i][j][0]] += 1
            else:
                histogram["red"][image[i][j][0]] = 1

            if image[i][j][1] in histogram["green"].keys():
                histogram["green"][image[i][j][1]] += 1
            else:
                histogram["green"][image[i][j][1]] = 1

            if image[i][j][2] in histogram["blue"].keys():
                histogram["blue"][image[i][j][2]] += 1
            else:
                histogram["blue"][image[i][j][2]] = 1

    return histogram