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
def create_histogram(image):
    rgb_dict = {
        'red': [],
        'green': [],
        'blue': [],
    }

    for row in image:
        ([
            (
                rgb_dict['red'].append(x),
                rgb_dict['green'].append(y),
                rgb_dict['blue'].append(z)
            ) for x, y, z in row
        ])

    for color_name in rgb_dict:
        rgb_dict[color_name] = dict(
            (i, rgb_dict[color_name].count(i)) for i in rgb_dict[color_name]
        )

    return(rgb_dict)


def transponse_image(image):
    return ([list(x) for x in zip(*image)])


def lighten_value(value, factor):
    return int(value + factor * (255 - value))


def darken_value(value, factor):
    return int(value - factor * (value - 0))


def rotate_left(image):
    image = transponse_image(image)

    return image[::-1]


def rotate_right(image):
    image = transponse_image(image)

    image_rotated = []

    for row in image:
        image_rotated.append(row[::-1])

    return image_rotated


def invert(image):
    inverted_image = []

    for row in image:
        inverted_row = []

        for pixel in row:
            inverted_row.append(
                (tuple(x - y for x, y in zip((255, 255, 255), pixel)))
            )

        inverted_image.append(inverted_row)

    return inverted_image


def lighten(image, factor):
    lighten_image = []
    for row in image:
        lighten_image.append([
            (
                lighten_value(x, factor),
                lighten_value(y, factor),
                lighten_value(z, factor)
            ) for x, y, z in row
        ])

    return lighten_image


def darken(image, factor):
    darken_image = []

    for row in image:
        darken_image.append([
            (
                darken_value(x, factor),
                darken_value(y, factor),
                darken_value(z, factor)
            ) for x, y, z in row
        ])

    return darken_image

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

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

OK

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

Явор обнови решението на 14.03.2016 13:01 (преди почти 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
def transponse_image(image):
    return ([list(x) for x in zip(*image)])


def lighten_value(value, factor):
    return int(value + factor * (255 - value))


def darken_value(value, factor):
    return int(value - factor * (value - 0))


def rotate_left(image):
    image = transponse_image(image)

    return image[::-1]


def rotate_right(image):
    image = transponse_image(image)

    image_rotated = []

    for row in image:
        image_rotated.append(row[::-1])

    return image_rotated


def invert(image):
    inverted_image = []

    for row in image:
        inverted_row = []

        for pixel in row:
            inverted_row.append(
                (tuple(x - y for x, y in zip((255, 255, 255), pixel)))
            )

        inverted_image.append(inverted_row)

    return inverted_image


def lighten(image, factor):
    lighten_image = []
    for row in image:
        lighten_image.append([
            (
                lighten_value(x, factor),
                lighten_value(y, factor),
                lighten_value(z, factor)
            ) for x, y, z in row
        ])

    return lighten_image


def darken(image, factor):
    darken_image = []

    for row in image:
        darken_image.append([
            (
                darken_value(x, factor),
                darken_value(y, factor),
                darken_value(z, factor)
            ) for x, y, z in row
        ])

    return darken_image

Явор обнови решението на 14.03.2016 16:00 (преди почти 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
def create_histogram(image):
    rgb_dict = {
        'red': [],
        'green': [],
        'blue': [],
    }

    for row in image:
        ([
            (
                rgb_dict['red'].append(x),
                rgb_dict['green'].append(y),
                rgb_dict['blue'].append(z)
            ) for x, y, z in row
        ])

    for color_name in rgb_dict:
        rgb_dict[color_name] = dict(
            (i, rgb_dict[color_name].count(i)) for i in rgb_dict[color_name]
        )

    return(rgb_dict)


def transponse_image(image):
    return ([list(x) for x in zip(*image)])


def lighten_value(value, factor):
    return int(value + factor * (255 - value))


def darken_value(value, factor):
    return int(value - factor * (value - 0))


def rotate_left(image):
    image = transponse_image(image)

    return image[::-1]


def rotate_right(image):
    image = transponse_image(image)

    image_rotated = []

    for row in image:
        image_rotated.append(row[::-1])

    return image_rotated


def invert(image):
    inverted_image = []

    for row in image:
        inverted_row = []

        for pixel in row:
            inverted_row.append(
                (tuple(x - y for x, y in zip((255, 255, 255), pixel)))
            )

        inverted_image.append(inverted_row)

    return inverted_image


def lighten(image, factor):
    lighten_image = []
    for row in image:
        lighten_image.append([
            (
                lighten_value(x, factor),
                lighten_value(y, factor),
                lighten_value(z, factor)
            ) for x, y, z in row
        ])

    return lighten_image


def darken(image, factor):
    darken_image = []

    for row in image:
        darken_image.append([
            (
                darken_value(x, factor),
                darken_value(y, factor),
                darken_value(z, factor)
            ) for x, y, z in row
        ])

    return darken_image