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
110
111
112
113
114
115
116
117
118
119
def rotate_left(image):
    new_image = []

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

    for column in range(columns - 1, -1, -1):
        new_row = []
        for row in range(rows):
            new_row.append(image[row][column])
        new_image.append(new_row)

    return new_image


def rotate_right(image):
    new_image = []

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

    for column in range(columns):
        new_row = []
        for row in range(rows - 1, -1, -1):
            new_row.append(image[row][column])
        new_image.append(new_row)

    return new_image


def invert(image):
    new_image = []

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

    for row in range(rows):
        new_row = []
        for column in range(columns):
            red = image[row][column][0]
            green = image[row][column][1]
            blue = image[row][column][2]
            new_row.append((255 - red, 255 - green, 255 - blue))
        new_image.append(new_row)

    return new_image


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

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

    for row in range(rows):
        new_row = []
        for column in range(columns):
            red = image[row][column][0]
            green = image[row][column][1]
            blue = image[row][column][2]

            new_red = int(red + coef * (255 - red))
            new_green = int(green + coef * (255 - green))
            new_blue = int(blue + coef * (255 - blue))
            new_row.append((new_red, new_green, new_blue))
        new_image.append(new_row)

    return new_image


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

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

    for row in range(rows):
        new_row = []
        for column in range(columns):
            red = image[row][column][0]
            green = image[row][column][1]
            blue = image[row][column][2]

            new_red = int(red - coef * red)
            new_green = int(green - coef * green)
            new_blue = int(blue - coef * blue)
            new_row.append((new_red, new_green, new_blue))
        new_image.append(new_row)

    return new_image


def create_histogram(image):
    rows = len(image)
    columns = len(image[0])

    red = {}
    green = {}
    blue = {}

    for row in range(rows):
        for column in range(columns):
            if image[row][column][0] in red:
                red[image[row][column][0]] += 1
            else:
                red[image[row][column][0]] = 1

            if image[row][column][1] in green:
                green[image[row][column][1]] += 1
            else:
                green[image[row][column][1]] = 1

            if image[row][column][2] in blue:
                blue[image[row][column][2]] += 1
            else:
                blue[image[row][column][2]] = 1

    histogram = {'red': red, 'green': green, 'blue': blue}
    return histogram

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.083s

OK

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

Данислав обнови решението на 13.03.2016 14:33 (преди почти 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
110
111
112
113
114
115
116
117
118
119
def rotate_left(image):
    new_image = []

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

    for column in range(columns - 1, -1, -1):
        new_row = []
        for row in range(rows):
            new_row.append(image[row][column])
        new_image.append(new_row)

    return new_image


def rotate_right(image):
    new_image = []

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

    for column in range(columns):
        new_row = []
        for row in range(rows - 1, -1, -1):
            new_row.append(image[row][column])
        new_image.append(new_row)

    return new_image


def invert(image):
    new_image = []

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

    for row in range(rows):
        new_row = []
        for column in range(columns):
            red = image[row][column][0]
            green = image[row][column][1]
            blue = image[row][column][2]
            new_row.append((255 - red, 255 - green, 255 - blue))
        new_image.append(new_row)

    return new_image


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

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

    for row in range(rows):
        new_row = []
        for column in range(columns):
            red = image[row][column][0]
            green = image[row][column][1]
            blue = image[row][column][2]

            new_red = int(red + coef * (255 - red))
            new_green = int(green + coef * (255 - green))
            new_blue = int(blue + coef * (255 - blue))
            new_row.append((new_red, new_green, new_blue))
        new_image.append(new_row)

    return new_image


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

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

    for row in range(rows):
        new_row = []
        for column in range(columns):
            red = image[row][column][0]
            green = image[row][column][1]
            blue = image[row][column][2]

            new_red = int(red - coef * red)
            new_green = int(green - coef * green)
            new_blue = int(blue - coef * blue)
            new_row.append((new_red, new_green, new_blue))
        new_image.append(new_row)

    return new_image


def create_histogram(image):
    rows = len(image)
    columns = len(image[0])

    red = {}
    green = {}
    blue = {}

    for row in range(rows):
        for column in range(columns):
            if image[row][column][0] in red:
                red[image[row][column][0]] += 1
            else:
                red[image[row][column][0]] = 1

            if image[row][column][1] in green:
                green[image[row][column][1]] += 1
            else:
                green[image[row][column][1]] = 1

            if image[row][column][2] in blue:
                blue[image[row][column][2]] += 1
            else:
                blue[image[row][column][2]] = 1

    histogram = {'red': red, 'green': green, 'blue': blue}
    return histogram