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_right(image):

    rotated_image = []
    number_of_rows = len(image)
    number_of_columns = len(image[0])
    for column in range(number_of_columns):
        current_row = []
        for row in range(number_of_rows-1, -1, -1):
            current_row.append(image[row][column])
        rotated_image.append(current_row)

    return rotated_image


def rotate_left(image):

    rotated_image = []
    number_of_rows = len(image)
    number_of_columns = len(image[0])
    for column in range(number_of_columns-1, -1, -1):
        current_row = []
        for row in range(number_of_rows):
            current_row.append(image[row][column])
        rotated_image.append(current_row)

    return rotated_image


def inverted_pixel(pixel):
    return (255-pixel[0],
            255-pixel[1],
            255-pixel[2])


def invert(image):

    inverted_image = []
    number_of_rows = len(image)
    number_of_columns = len(image[0])
    for row in range(number_of_rows):
        current_row = []
        for column in range(number_of_columns):
            current_row.append(inverted_pixel(image[row][column]))
        inverted_image.append(current_row)

    return inverted_image


def lightened_pixel(pixel, factor):
    return (int(pixel[0] + factor*(255-pixel[0])),
            int(pixel[1] + factor*(255-pixel[1])),
            int(pixel[2] + factor*(255-pixel[2])))


def lighten(image, factor):

    lightened_image = []
    number_of_rows = len(image)
    number_of_columns = len(image[0])
    for row in range(number_of_rows):
        current_row = []
        for column in range(number_of_columns):
            current_row.append(lightened_pixel(image[row][column], factor))
        lightened_image.append(current_row)

    return lightened_image


def darkened_pixel(pixel, factor):
    return (int(pixel[0] - factor*(pixel[0]-0)),
            int(pixel[1] - factor*(pixel[1]-0)),
            int(pixel[2] - factor*(pixel[2]-0)))


def darken(image, factor):

    darkened_image = []
    number_of_rows = len(image)
    number_of_columns = len(image[0])
    for row in range(number_of_rows):
        current_row = []
        for column in range(number_of_columns):
            current_row.append(darkened_pixel(image[row][column], factor))
        darkened_image.append(current_row)

    return darkened_image


def create_histogram(image):

    red_values = {}
    green_values = {}
    blue_values = {}
    number_of_rows = len(image)
    number_of_columns = len(image[0])
    for row in range(number_of_rows):
        for column in range(number_of_columns):
            red = image[row][column][0]
            green = image[row][column][1]
            blue = image[row][column][2]
            if red in red_values:
                red_values[red] += 1
            else:
                red_values[red] = 1
            if green in green_values:
                green_values[green] += 1
            else:
                green_values[green] = 1
            if blue in blue_values:
                blue_values[blue] += 1
            else:
                blue_values[blue] = 1

    colors = {}
    colors['red'] = red_values
    colors['green'] = green_values
    colors['blue'] = blue_values

    return colors

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.089s

OK

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

Пламен обнови решението на 13.03.2016 23:54 (преди почти 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_right(image):

    rotated_image = []
    number_of_rows = len(image)
    number_of_columns = len(image[0])
    for column in range(number_of_columns):
        current_row = []
        for row in range(number_of_rows-1, -1, -1):
            current_row.append(image[row][column])
        rotated_image.append(current_row)

    return rotated_image


def rotate_left(image):

    rotated_image = []
    number_of_rows = len(image)
    number_of_columns = len(image[0])
    for column in range(number_of_columns-1, -1, -1):
        current_row = []
        for row in range(number_of_rows):
            current_row.append(image[row][column])
        rotated_image.append(current_row)

    return rotated_image


def inverted_pixel(pixel):
    return (255-pixel[0],
            255-pixel[1],
            255-pixel[2])


def invert(image):

    inverted_image = []
    number_of_rows = len(image)
    number_of_columns = len(image[0])
    for row in range(number_of_rows):
        current_row = []
        for column in range(number_of_columns):
            current_row.append(inverted_pixel(image[row][column]))
        inverted_image.append(current_row)

    return inverted_image


def lightened_pixel(pixel, factor):
    return (int(pixel[0] + factor*(255-pixel[0])),
            int(pixel[1] + factor*(255-pixel[1])),
            int(pixel[2] + factor*(255-pixel[2])))


def lighten(image, factor):

    lightened_image = []
    number_of_rows = len(image)
    number_of_columns = len(image[0])
    for row in range(number_of_rows):
        current_row = []
        for column in range(number_of_columns):
            current_row.append(lightened_pixel(image[row][column], factor))
        lightened_image.append(current_row)

    return lightened_image


def darkened_pixel(pixel, factor):
    return (int(pixel[0] - factor*(pixel[0]-0)),
            int(pixel[1] - factor*(pixel[1]-0)),
            int(pixel[2] - factor*(pixel[2]-0)))


def darken(image, factor):

    darkened_image = []
    number_of_rows = len(image)
    number_of_columns = len(image[0])
    for row in range(number_of_rows):
        current_row = []
        for column in range(number_of_columns):
            current_row.append(darkened_pixel(image[row][column], factor))
        darkened_image.append(current_row)

    return darkened_image


def create_histogram(image):

    red_values = {}
    green_values = {}
    blue_values = {}
    number_of_rows = len(image)
    number_of_columns = len(image[0])
    for row in range(number_of_rows):
        for column in range(number_of_columns):
            red = image[row][column][0]
            green = image[row][column][1]
            blue = image[row][column][2]
            if red in red_values:
                red_values[red] += 1
            else:
                red_values[red] = 1
            if green in green_values:
                green_values[green] += 1
            else:
                green_values[green] = 1
            if blue in blue_values:
                blue_values[blue] += 1
            else:
                blue_values[blue] = 1

    colors = {}
    colors['red'] = red_values
    colors['green'] = green_values
    colors['blue'] = blue_values

    return colors