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
from collections import Counter


def rotate_left(image):
    return [[row[col] for row in image]
            for col in range((len(image[0]) - 1), -1, -1)]


def rotate_right(image):
    return [[row[col] for row in image[::-1]] for col in range(len(image[0]))]


def invert(image):
    width = range(len(image[0]))
    return [[tuple((255 - row[col][color]) for color in range(3))
             for col in width] for row in image]


def lighten(image, n):
    width = range(len(image[0]))
    return [[tuple((row[col][color] + int(n * (255 - row[col][color])))
            for color in range(3)) for col in width] for row in image]


def darken(image, n):
    width = range(len(image[0]))
    return [[tuple((row[col][color] - int(n * (row[col][color] - 0)))
            for color in range(3)) for col in width] for row in image]


def create_histogram(image):
    red_values = get_color_values(image, 0)
    green_values = get_color_values(image, 1)
    blue_values = get_color_values(image, 2)

    return {'red': red_values, 'green': green_values, 'blue': blue_values}


def get_color_values(image, color_index):
    width = range(len(image[0]))
    return Counter(row[col][color_index] for row in image for col in width)

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.086s

OK

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

Тина обнови решението на 11.03.2016 16:43 (преди почти 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
def rotate_left(image):
    return [[row[col] for row in image]
            for col in range((len(image[1]) - 1), -1, -1)]


def rotate_right(image):
    return [[row[col] for row in image[::-1]] for col in range(len(image[1]))]


def invert(image):
    width = range(len(image[1]))
    return [[tuple((255 - row[col][color_index]) for color_index in range(3))
            for col in width] for row in image]


def lighten(image, n):
    width = range(len(image[1]))
    return [[tuple((row[col][color] + int(n * (255 - row[col][color])))
            for color in range(3)) for col in width] for row in image]


def darken(image, n):
    width = range(len(image[1]))
    return [[tuple((row[col][color] - int(n * (row[col][color] - 0)))
            for color in range(3)) for col in width] for row in image]


def create_histogram(image):
    red_values = get_values(image, 0)
    green_values = get_values(image, 1)
    blue_values = get_values(image, 2)

    return {'red': red_values, 'green': green_values, 'blue': blue_values}


def get_values(image, color_index):
    color_values = {x: 0 for x in range(0, 256)}
    for key in color_values:
        for pixels in image:
            for pixel in pixels:
                if pixel[color_index] == key:
                    color_values[key] += 1

    return {p: color_values[p] for p in color_values if color_values[p] != 0}

Тина обнови решението на 11.03.2016 23:58 (преди почти 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
def rotate_left(image):
    return [[row[col] for row in image]
            for col in range((len(image[1]) - 1), -1, -1)]


def rotate_right(image):
    return [[row[col] for row in image[::-1]] for col in range(len(image[1]))]


def invert(image):
    width = range(len(image[1]))
    return [[tuple((255 - row[col][color]) for color in range(3))
             for col in width] for row in image]


def lighten(image, n):
    width = range(len(image[1]))
    return [[tuple((row[col][color] + int(n * (255 - row[col][color])))
                   for color in range(3)) for col in width] for row in image]


def darken(image, n):
    width = range(len(image[1]))
    return [[tuple((row[col][color] - int(n * (row[col][color] - 0)))
                   for color in range(3)) for col in width] for row in image]


def create_histogram(image):
    red_values = get_values(image, 0)
    green_values = get_values(image, 1)
    blue_values = get_values(image, 2)

    return {'red': red_values, 'green': green_values, 'blue': blue_values}


def get_values(image, color_index):
    color_values = {x: 0 for x in range(0, 256)}
    for pixels in image:
        for pixel in pixels:
            color_values[pixel[color_index]] += 1

    return {p: color_values[p] for p in color_values if color_values[p] != 0}

Тина обнови решението на 14.03.2016 12:10 (преди почти 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
from collections import Counter


def rotate_left(image):
    return [[row[col] for row in image]
            for col in range((len(image[0]) - 1), -1, -1)]


def rotate_right(image):
    return [[row[col] for row in image[::-1]] for col in range(len(image[0]))]


def invert(image):
    width = range(len(image[0]))
    return [[tuple((255 - row[col][color]) for color in range(3))
             for col in width] for row in image]


def lighten(image, n):
    width = range(len(image[0]))
    return [[tuple((row[col][color] + int(n * (255 - row[col][color])))
            for color in range(3)) for col in width] for row in image]


def darken(image, n):
    width = range(len(image[0]))
    return [[tuple((row[col][color] - int(n * (row[col][color] - 0)))
            for color in range(3)) for col in width] for row in image]


def create_histogram(image):
    red_values = get_color_values(image, 0)
    green_values = get_color_values(image, 1)
    blue_values = get_color_values(image, 2)

    return {'red': red_values, 'green': green_values, 'blue': blue_values}


def get_color_values(image, color_index):
    width = range(len(image[0]))
    return Counter(row[col][color_index] for row in image for col in width)