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


COLORS = ['red', 'green', 'blue']


def get_number_rows_cols(image):
    return len(image), len(image[0])


def create_new_image_matrix(rows, cols):
    new_image = [
        [() for x in range(cols)] for y in range(rows)
    ]
    return new_image


def invert_pixel(pixel):
    return [255 - x for x in pixel]


def light_pixel(pixel, number):
    return [int(x + (255 - x) * number) for x in pixel]


def dark_pixel(pixel, number):
    return [int(x - (x - 0) * number) for x in pixel]


def generate_histogram_numbers(pixel, histogram):
    for i in range(len(COLORS)):
        histogram[COLORS[i]][pixel[i]] += 1
    return histogram


def rotate_left(image):
    rows, cols = get_number_rows_cols(image)
    rotated_image = create_new_image_matrix(cols, rows)
    for i in range(cols):
        for j in range(rows):
            rotated_image[i][j] = image[j][- (i - cols + 1)]
    return rotated_image


def rotate_right(image):
    rows, cols = get_number_rows_cols(image)
    rotated_image = create_new_image_matrix(cols, rows)
    for i in range(cols):
        for j in range(rows):
            rotated_image[i][j] = image[- (j - rows + 1)][i]
    return rotated_image


def invert(image):
    rows, cols = get_number_rows_cols(image)
    inverted_image = create_new_image_matrix(rows, cols)
    for i in range(rows):
        for j in range(cols):
            inverted_image[i][j] = tuple(invert_pixel(image[i][j]))
    return inverted_image


def lighten(image, number):
    rows, cols = get_number_rows_cols(image)
    lighten_image = create_new_image_matrix(rows, cols)
    for i in range(rows):
        for j in range(cols):
            lighten_image[i][j] = tuple(light_pixel(image[i][j], number))
    return lighten_image


def darken(image, number):
    rows, cols = get_number_rows_cols(image)
    darken_image = create_new_image_matrix(rows, cols)
    for i in range(rows):
        for j in range(cols):
            darken_image[i][j] = tuple(dark_pixel(image[i][j], number))
    return darken_image


def create_histogram(image):
    rows, cols = get_number_rows_cols(image)
    histogram = {
        key: defaultdict(int) for key in COLORS
    }
    for i in range(rows):
        for j in range(cols):
            pixel = image[i][j]
            generate_histogram_numbers(pixel, histogram)
    return histogram

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.092s

OK

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

Петър обнови решението на 13.03.2016 18:04 (преди почти 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
COLORS = ['red', 'green', 'blue']


def get_number_rows_cols(image):
    return len(image), len(image[0])


def create_new_image_matrix(rows, cols):
    new_image = [
        [() for x in range(cols)] for y in range(rows)
    ]
    return new_image


def invert_pixel(pixel):
    return [255 - x for x in pixel]


def light_pixel(pixel, number):
    return [int(x + (255 - x) * number) for x in pixel]


def dark_pixel(pixel, number):
    return [int(x - (x - 0) * number) for x in pixel]


def get_histogram_with_new_number(color, number, histogram):
    if number not in histogram[color]:
        histogram[color][number] = 1
    else:
        histogram[color][number] += 1
    return histogram


def generate_histogram_numbers(pixel, histogram):
    for i in range(len(COLORS)):
        get_histogram_with_new_number(COLORS[i], pixel[i], histogram)
    return histogram


def rotate_left(image):
    rows, cols = get_number_rows_cols(image)
    rotated_image = create_new_image_matrix(cols, rows)
    for i in range(cols):
        for j in range(rows):
            rotated_image[i][j] = image[j][- (i - cols + 1)]
    return rotated_image


def rotate_right(image):
    rows, cols = get_number_rows_cols(image)
    rotated_image = create_new_image_matrix(cols, rows)
    for i in range(cols):
        for j in range(rows):
            rotated_image[i][j] = image[- (j - rows + 1)][i]
    return rotated_image


def invert(image):
    rows, cols = get_number_rows_cols(image)
    inverted_image = create_new_image_matrix(rows, cols)
    for i in range(rows):
        for j in range(cols):
            inverted_image[i][j] = tuple(invert_pixel(image[i][j]))
    return inverted_image


def lighten(image, number):
    rows, cols = get_number_rows_cols(image)
    lighten_image = create_new_image_matrix(rows, cols)
    for i in range(rows):
        for j in range(cols):
            lighten_image[i][j] = tuple(light_pixel(image[i][j], number))
    return lighten_image


def darken(image, number):
    rows, cols = get_number_rows_cols(image)
    darken_image = create_new_image_matrix(rows, cols)
    for i in range(rows):
        for j in range(cols):
            darken_image[i][j] = tuple(dark_pixel(image[i][j], number))
    return darken_image


def create_histogram(image):
    rows, cols = get_number_rows_cols(image)
    histogram = {
        'red': {},
        'green': {},
        'blue': {},
    }
    for i in range(rows):
        for j in range(cols):
            pixel = image[i][j]
            generate_histogram_numbers(pixel, histogram)
    return histogram

Петър обнови решението на 13.03.2016 18:06 (преди почти 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
COLORS = ['red', 'green', 'blue']


def get_number_rows_cols(image):
    return len(image), len(image[0])


def create_new_image_matrix(rows, cols):
    new_image = [
        [() for x in range(cols)] for y in range(rows)
    ]
    return new_image


def invert_pixel(pixel):
    return [255 - x for x in pixel]


def light_pixel(pixel, number):
    return [int(x + (255 - x) * number) for x in pixel]


def dark_pixel(pixel, number):
    return [int(x - (x - 0) * number) for x in pixel]


def get_histogram_with_new_number(color, number, histogram):
    if number not in histogram[color]:
        histogram[color][number] = 1
    else:
        histogram[color][number] += 1
    return histogram


def generate_histogram_numbers(pixel, histogram):
    for i in range(len(COLORS)):
        get_histogram_with_new_number(COLORS[i], pixel[i], histogram)
    return histogram


def rotate_left(image):
    rows, cols = get_number_rows_cols(image)
    rotated_image = create_new_image_matrix(cols, rows)
    for i in range(cols):
        for j in range(rows):
            rotated_image[i][j] = image[j][- (i - cols + 1)]
    return rotated_image


def rotate_right(image):
    rows, cols = get_number_rows_cols(image)
    rotated_image = create_new_image_matrix(cols, rows)
    for i in range(cols):
        for j in range(rows):
            rotated_image[i][j] = image[- (j - rows + 1)][i]
    return rotated_image


def invert(image):
    rows, cols = get_number_rows_cols(image)
    inverted_image = create_new_image_matrix(rows, cols)
    for i in range(rows):
        for j in range(cols):
            inverted_image[i][j] = tuple(invert_pixel(image[i][j]))
    return inverted_image


def lighten(image, number):
    rows, cols = get_number_rows_cols(image)
    lighten_image = create_new_image_matrix(rows, cols)
    for i in range(rows):
        for j in range(cols):
            lighten_image[i][j] = tuple(light_pixel(image[i][j], number))
    return lighten_image


def darken(image, number):
    rows, cols = get_number_rows_cols(image)
    darken_image = create_new_image_matrix(rows, cols)
    for i in range(rows):
        for j in range(cols):
            darken_image[i][j] = tuple(dark_pixel(image[i][j], number))
    return darken_image


def create_histogram(image):
    rows, cols = get_number_rows_cols(image)
    histogram = {
        key: {} for key in COLORS
    }
    for i in range(rows):
        for j in range(cols):
            pixel = image[i][j]
            generate_histogram_numbers(pixel, histogram)
    return histogram

Петър обнови решението на 13.03.2016 18:27 (преди почти 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
from collections import defaultdict


COLORS = ['red', 'green', 'blue']


def get_number_rows_cols(image):
    return len(image), len(image[0])


def create_new_image_matrix(rows, cols):
    new_image = [
        [() for x in range(cols)] for y in range(rows)
    ]
    return new_image


def invert_pixel(pixel):
    return [255 - x for x in pixel]


def light_pixel(pixel, number):
    return [int(x + (255 - x) * number) for x in pixel]


def dark_pixel(pixel, number):
    return [int(x - (x - 0) * number) for x in pixel]


def generate_histogram_numbers(pixel, histogram):
    for i in range(len(COLORS)):
        histogram[COLORS[i]][pixel[i]] += 1
    return histogram


def rotate_left(image):
    rows, cols = get_number_rows_cols(image)
    rotated_image = create_new_image_matrix(cols, rows)
    for i in range(cols):
        for j in range(rows):
            rotated_image[i][j] = image[j][- (i - cols + 1)]
    return rotated_image


def rotate_right(image):
    rows, cols = get_number_rows_cols(image)
    rotated_image = create_new_image_matrix(cols, rows)
    for i in range(cols):
        for j in range(rows):
            rotated_image[i][j] = image[- (j - rows + 1)][i]
    return rotated_image


def invert(image):
    rows, cols = get_number_rows_cols(image)
    inverted_image = create_new_image_matrix(rows, cols)
    for i in range(rows):
        for j in range(cols):
            inverted_image[i][j] = tuple(invert_pixel(image[i][j]))
    return inverted_image


def lighten(image, number):
    rows, cols = get_number_rows_cols(image)
    lighten_image = create_new_image_matrix(rows, cols)
    for i in range(rows):
        for j in range(cols):
            lighten_image[i][j] = tuple(light_pixel(image[i][j], number))
    return lighten_image


def darken(image, number):
    rows, cols = get_number_rows_cols(image)
    darken_image = create_new_image_matrix(rows, cols)
    for i in range(rows):
        for j in range(cols):
            darken_image[i][j] = tuple(dark_pixel(image[i][j], number))
    return darken_image


def create_histogram(image):
    rows, cols = get_number_rows_cols(image)
    histogram = {
        key: defaultdict(int) for key in COLORS
    }
    for i in range(rows):
        for j in range(cols):
            pixel = image[i][j]
            generate_histogram_numbers(pixel, histogram)
    return histogram