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

DIRECTIONS = {
    'left': lambda x, y, length: [y, - (x - length + 1)],
    'right': lambda x, y, length: [-(y - length + 1), x]
}
LENGTHS = {
    'left': lambda image: len(image[0]),
    'right': lambda image: len(image)
}
RGB_VALUE = 255


def make_matrix(length_row, length_col):
    return [[() for y in range(length_col)] for x in range(length_row)]


def rotate_pixel(direction, x, y, length, image):
    coords = DIRECTIONS[direction](x, y, length)
    return image[coords[0]][coords[1]]


def rotate(image, direction):
    length_row = len(image)
    length_col = len(image[0])
    length = LENGTHS[direction](image)

    rotated = [[rotate_pixel(direction, x, y, length, image)
                for y in range(length_row)] for x in range(length_col)]
    return rotated


def rotate_left(image):
    return rotate(image, 'left')


def rotate_right(image):
    return rotate(image, 'right')


def invert_pixel(pixel):
    return (RGB_VALUE - pixel[0], RGB_VALUE - pixel[1], RGB_VALUE - pixel[2])


def lighten_pixel(pixel, coeff):
    return (int(pixel[0] + coeff * (RGB_VALUE - pixel[0])),
            int(pixel[1] + coeff * (RGB_VALUE - pixel[1])),
            int(pixel[2] + coeff * (RGB_VALUE - pixel[2])))


def darken_pixel(pixel, coeff):
    return (int(pixel[0] - coeff * (pixel[0] - 0)),
            int(pixel[1] - coeff * (pixel[1] - 0)),
            int(pixel[2] - coeff * (pixel[2] - 0)))


def change_pixels(image, func, *args):
    length_row = len(image)
    length_col = len(image[0])

    changed = [[func(image[x][y], *args) for y in range(0, length_col)]
               for x in range(0, length_row)]

    return changed


def invert(image):
    return change_pixels(image, invert_pixel)


def lighten(image, coeff):
    return change_pixels(image, lighten_pixel, coeff)


def darken(image, coeff):
    return change_pixels(image, darken_pixel, coeff)


def create_histogram(image):
    result = {
        'red': defaultdict(int),
        'green': defaultdict(int),
        'blue': defaultdict(int)
    }

    for line in image:
        for pixel in line:
            result['red'][pixel[0]] += 1
            result['green'][pixel[1]] += 1
            result['blue'][pixel[2]] += 1

    return result

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.085s

OK

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

Десислава обнови решението на 09.03.2016 00: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
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
DIRECTIONS = {
    'left': lambda x, y, length: [length - 1 - y, x],
    'right': lambda x, y, length: [y, length - 1 - x]

}
LENGTHS = {
    'left': lambda image: len(image[0]),
    'right': lambda image: len(image)
}
RGB_VALUE = 255


def make_matrix(length_row, length_col):
    return [[[] for y in range(length_col)] for x in range(length_row)]


def rotate(image, direction):
    length_row = len(image)
    length_col = len(image[0])
    length = LENGTHS[direction](image)
    rotated = make_matrix(length_col, length_row)

    for x in range(0, length_row):
        for y in range(0, length_col):
            coords = DIRECTIONS[direction](x, y, length)
            rotated[coords[0]][coords[1]] = image[x][y]

    return rotated


def rotate_left(image):
    return rotate(image, 'left')


def rotate_right(image):
    return rotate(image, 'right')


def invert_pixel(pixel):
    return (RGB_VALUE - pixel[0], RGB_VALUE - pixel[1], RGB_VALUE - pixel[2])


def lighten_pixel(pixel, coeff):
    return (int(pixel[0] + coeff * (RGB_VALUE - pixel[0])),
            int(pixel[1] + coeff * (RGB_VALUE - pixel[1])),
            int(pixel[2] + coeff * (RGB_VALUE - pixel[2])))


def darken_pixel(pixel, coeff):
    return (int(pixel[0] - coeff * (pixel[0] - 0)),
            int(pixel[1] - coeff * (pixel[1] - 0)),
            int(pixel[2] - coeff * (pixel[2] - 0)))


def change_pixels(image, func, *args):
    length_row = len(image)
    length_col = len(image[0])

    inverted = make_matrix(length_row, length_col)
    for x in range(0, length_row):
        for y in range(0, length_col):
            inverted[x][y] = func(image[x][y], *args)
    return inverted


def invert(image):
    return change_pixels(image, invert_pixel)


def lighten(image, coeff):
    return change_pixels(image, lighten_pixel, coeff)


def darken(image, coeff):
    return change_pixels(image, darken_pixel, coeff)


def make_statistic(statistic_color, number):
    if number not in statistic_color:
        statistic_color[number] = 1
    else:
        statistic_color[number] += 1


def create_histogram(image):
    result = {
        'red': {},
        'green': {},
        'blue': {}
    }
    for line in image:
        for pixel in line:
            make_statistic(result['red'], pixel[0])
            make_statistic(result['green'], pixel[1])
            make_statistic(result['blue'], pixel[2])
    return result

Десислава обнови решението на 09.03.2016 00:24 (преди почти 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
DIRECTIONS = {
    'left': lambda x, y, length: [length - 1 - y, x],
    'right': lambda x, y, length: [y, length - 1 - x]

}
LENGTHS = {
    'left': lambda image: len(image[0]),
    'right': lambda image: len(image)
}
RGB_VALUE = 255


def make_matrix(length_row, length_col):
    return [[[] for y in range(length_col)] for x in range(length_row)]


def rotate(image, direction):
    length_row = len(image)
    length_col = len(image[0])
    length = LENGTHS[direction](image)
    rotated = make_matrix(length_col, length_row)

    for x in range(0, length_row):
        for y in range(0, length_col):
            coords = DIRECTIONS[direction](x, y, length)
            rotated[coords[0]][coords[1]] = image[x][y]

    return rotated


def rotate_left(image):
    return rotate(image, 'left')


def rotate_right(image):
    return rotate(image, 'right')


def invert_pixel(pixel):
    return (RGB_VALUE - pixel[0], RGB_VALUE - pixel[1], RGB_VALUE - pixel[2])


def lighten_pixel(pixel, coeff):
    return (int(pixel[0] + coeff * (RGB_VALUE - pixel[0])),
            int(pixel[1] + coeff * (RGB_VALUE - pixel[1])),
            int(pixel[2] + coeff * (RGB_VALUE - pixel[2])))


def darken_pixel(pixel, coeff):
    return (int(pixel[0] - coeff * (pixel[0] - 0)),
            int(pixel[1] - coeff * (pixel[1] - 0)),
            int(pixel[2] - coeff * (pixel[2] - 0)))


def change_pixels(image, func, *args):
    length_row = len(image)
    length_col = len(image[0])

    changed = make_matrix(length_row, length_col)
    for x in range(0, length_row):
        for y in range(0, length_col):
            changed[x][y] = func(image[x][y], *args)
    return changed


def invert(image):
    return change_pixels(image, invert_pixel)


def lighten(image, coeff):
    return change_pixels(image, lighten_pixel, coeff)


def darken(image, coeff):
    return change_pixels(image, darken_pixel, coeff)


def make_statistic(statistic_color, number):
    if number not in statistic_color:
        statistic_color[number] = 1
    else:
        statistic_color[number] += 1


def create_histogram(image):
    result = {
        'red': {},
        'green': {},
        'blue': {}
    }
    for line in image:
        for pixel in line:
            make_statistic(result['red'], pixel[0])
            make_statistic(result['green'], pixel[1])
            make_statistic(result['blue'], pixel[2])
    return result

Десислава обнови решението на 09.03.2016 15:12 (преди почти 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
from collections import defaultdict

DIRECTIONS = {
    'left': lambda x, y, length: [y, - (x - length + 1)],
    'right': lambda x, y, length: [-(y - length + 1), x]
}
LENGTHS = {
    'left': lambda image: len(image[0]),
    'right': lambda image: len(image)
}
RGB_VALUE = 255


def make_matrix(length_row, length_col):
    return [[() for y in range(length_col)] for x in range(length_row)]


def rotate_pixel(direction, x, y, length, image):
    coords = DIRECTIONS[direction](x, y, length)
    return image[coords[0]][coords[1]]


def rotate(image, direction):
    length_row = len(image)
    length_col = len(image[0])
    length = LENGTHS[direction](image)

    rotated = [[rotate_pixel(direction, x, y, length, image)
                for y in range(length_row)] for x in range(length_col)]
    return rotated


def rotate_left(image):
    return rotate(image, 'left')


def rotate_right(image):
    return rotate(image, 'right')


def invert_pixel(pixel):
    return (RGB_VALUE - pixel[0], RGB_VALUE - pixel[1], RGB_VALUE - pixel[2])


def lighten_pixel(pixel, coeff):
    return (int(pixel[0] + coeff * (RGB_VALUE - pixel[0])),
            int(pixel[1] + coeff * (RGB_VALUE - pixel[1])),
            int(pixel[2] + coeff * (RGB_VALUE - pixel[2])))


def darken_pixel(pixel, coeff):
    return (int(pixel[0] - coeff * (pixel[0] - 0)),
            int(pixel[1] - coeff * (pixel[1] - 0)),
            int(pixel[2] - coeff * (pixel[2] - 0)))


def change_pixels(image, func, *args):
    length_row = len(image)
    length_col = len(image[0])

    changed = make_matrix(length_row, length_col)

    for x in range(0, length_row):
        changed[x] = [func(image[x][y], *args) for y in range(0, length_col)]
    return changed


def invert(image):
    return change_pixels(image, invert_pixel)


def lighten(image, coeff):
    return change_pixels(image, lighten_pixel, coeff)


def darken(image, coeff):
    return change_pixels(image, darken_pixel, coeff)


def create_histogram(image):
    result = {
        'red': defaultdict(int),
        'green': defaultdict(int),
        'blue': defaultdict(int)
    }

    for line in image:
        for pixel in line:
            result['red'][pixel[0]] += 1
            result['green'][pixel[1]] += 1
            result['blue'][pixel[2]] += 1

    return result

Десислава обнови решението на 09.03.2016 15:15 (преди почти 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
from collections import defaultdict

DIRECTIONS = {
    'left': lambda x, y, length: [y, - (x - length + 1)],
    'right': lambda x, y, length: [-(y - length + 1), x]
}
LENGTHS = {
    'left': lambda image: len(image[0]),
    'right': lambda image: len(image)
}
RGB_VALUE = 255


def make_matrix(length_row, length_col):
    return [[() for y in range(length_col)] for x in range(length_row)]


def rotate_pixel(direction, x, y, length, image):
    coords = DIRECTIONS[direction](x, y, length)
    return image[coords[0]][coords[1]]


def rotate(image, direction):
    length_row = len(image)
    length_col = len(image[0])
    length = LENGTHS[direction](image)

    rotated = [[rotate_pixel(direction, x, y, length, image)
                for y in range(length_row)] for x in range(length_col)]
    return rotated


def rotate_left(image):
    return rotate(image, 'left')


def rotate_right(image):
    return rotate(image, 'right')


def invert_pixel(pixel):
    return (RGB_VALUE - pixel[0], RGB_VALUE - pixel[1], RGB_VALUE - pixel[2])


def lighten_pixel(pixel, coeff):
    return (int(pixel[0] + coeff * (RGB_VALUE - pixel[0])),
            int(pixel[1] + coeff * (RGB_VALUE - pixel[1])),
            int(pixel[2] + coeff * (RGB_VALUE - pixel[2])))


def darken_pixel(pixel, coeff):
    return (int(pixel[0] - coeff * (pixel[0] - 0)),
            int(pixel[1] - coeff * (pixel[1] - 0)),
            int(pixel[2] - coeff * (pixel[2] - 0)))


def change_pixels(image, func, *args):
    length_row = len(image)
    length_col = len(image[0])

    changed = [[func(image[x][y], *args) for y in range(0, length_col)]
               for x in range(0, length_row)]

    return changed


def invert(image):
    return change_pixels(image, invert_pixel)


def lighten(image, coeff):
    return change_pixels(image, lighten_pixel, coeff)


def darken(image, coeff):
    return change_pixels(image, darken_pixel, coeff)


def create_histogram(image):
    result = {
        'red': defaultdict(int),
        'green': defaultdict(int),
        'blue': defaultdict(int)
    }

    for line in image:
        for pixel in line:
            result['red'][pixel[0]] += 1
            result['green'][pixel[1]] += 1
            result['blue'][pixel[2]] += 1

    return result