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
def rotate_left(image):
    number_of_rows = len(image)
    number_of_columns = len(image[0])
    result = [[] for i in range(number_of_columns)]

    for row in range(number_of_rows):
        for column in range(number_of_columns):
            result[number_of_columns - 1 - column].append(image[row][column])

    return result


def rotate_right(image):
    number_of_rows = len(image)
    number_of_columns = len(image[0])
    result = [[] for i in range(number_of_columns)]

    for row in range(number_of_rows - 1, -1, -1):
        for column in range(number_of_columns):
            result[column].append(image[row][column])

    return result


def map_pixels(image, op):
    result = []
    current_row = []

    for row in image:
        for pixel in row:
            new_pixel = tuple(map(op, pixel))
            current_row.append(new_pixel)
        result.append(current_row)
        current_row = []

    return result


def invert(image):
    return map_pixels(image, lambda x: 255 - x)


def lighten(image, p):
    return map_pixels(image, lambda x: x + int(p * (255 - x)))


def darken(image, p):
    return map_pixels(image, lambda x: x - int(p * x))


def create_histogram(image):
    result = {'red': {}, 'green': {}, 'blue': {}}
    for row in image:
        for pixel in row:
            r, g, b = pixel
            if r in result['red']:
                result['red'][r] += 1
            else:
                result['red'][r] = 1

            if g in result['green']:
                result['green'][g] += 1
            else:
                result['green'][g] = 1

            if b in result['blue']:
                result['blue'][b] += 1
            else:
                result['blue'][b] = 1

    return result

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.084s

OK

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

Веселин обнови решението на 14.03.2016 00: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
def rotate_left(image):
    number_of_rows = len(image)
    number_of_columns = len(image[0])
    result = [[] for i in range(number_of_columns)]

    for row in range(number_of_rows):
        for column in range(number_of_columns):
            result[number_of_columns - 1 - column].append(image[row][column])

    return result


def rotate_right(image):
    number_of_rows = len(image)
    number_of_columns = len(image[0])
    result = [[] for i in range(number_of_columns)]

    for row in range(number_of_rows - 1, -1, -1):
        for column in range(number_of_columns):
            result[column].append(image[row][column])

    return result


def invert(image):
    result = []
    current_row = []

    for row in image:
        for pixel in row:
            r, g, b = pixel
            current_row.append((255 - r, 255 - g, 255 - b))
        result.append(current_row)
        current_row = []

    return result


def lighten(image, p):
    result = []
    current_row = []

    for row in image:
        for pixel in row:
            lightened = map(lambda x: x + int(p * (255 - x)), pixel)
            current_row.append(tuple(lightened))
        result.append(current_row)
        current_row = []

    return result


def darken(image, p):
    result = []
    current_row = []

    for row in image:
        for pixel in row:
            lightened = map(lambda x: x - int(p * x), pixel)
            current_row.append(tuple(lightened))
        result.append(current_row)
        current_row = []

    return result


def create_histogram(image):
    result = {'red': {}, 'green': {}, 'blue': {}}
    for row in image:
        for pixel in row:
            r, g, b = pixel
            if r in result['red']:
                result['red'][r] += 1
            else:
                result['red'][r] = 1

            if g in result['green']:
                result['green'][g] += 1
            else:
                result['green'][g] = 1

            if b in result['blue']:
                result['blue'][b] += 1
            else:
                result['blue'][b] = 1

    return result

Веселин обнови решението на 14.03.2016 00: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
def rotate_left(image):
    number_of_rows = len(image)
    number_of_columns = len(image[0])
    result = [[] for i in range(number_of_columns)]

    for row in range(number_of_rows):
        for column in range(number_of_columns):
            result[number_of_columns - 1 - column].append(image[row][column])

    return result


def rotate_right(image):
    number_of_rows = len(image)
    number_of_columns = len(image[0])
    result = [[] for i in range(number_of_columns)]

    for row in range(number_of_rows - 1, -1, -1):
        for column in range(number_of_columns):
            result[column].append(image[row][column])

    return result


def invert(image):
    result = []
    current_row = []

    for row in image:
        for pixel in row:
            r, g, b = pixel
            current_row.append((255 - r, 255 - g, 255 - b))
        result.append(current_row)
        current_row = []

    return result


def lighten(image, p):
    result = []
    current_row = []

    for row in image:
        for pixel in row:
            lightened = map(lambda x: x + int(p * (255 - x)), pixel)
            current_row.append(tuple(lightened))
        result.append(current_row)
        current_row = []

    return result


def darken(image, p):
    result = []
    current_row = []

    for row in image:
        for pixel in row:
            darkened = map(lambda x: x - int(p * x), pixel)
            current_row.append(tuple(darkened))
        result.append(current_row)
        current_row = []

    return result


def create_histogram(image):
    result = {'red': {}, 'green': {}, 'blue': {}}
    for row in image:
        for pixel in row:
            r, g, b = pixel
            if r in result['red']:
                result['red'][r] += 1
            else:
                result['red'][r] = 1

            if g in result['green']:
                result['green'][g] += 1
            else:
                result['green'][g] = 1

            if b in result['blue']:
                result['blue'][b] += 1
            else:
                result['blue'][b] = 1

    return result

Веселин обнови решението на 14.03.2016 00:40 (преди почти 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
def rotate_left(image):
    number_of_rows = len(image)
    number_of_columns = len(image[0])
    result = [[] for i in range(number_of_columns)]

    for row in range(number_of_rows):
        for column in range(number_of_columns):
            result[number_of_columns - 1 - column].append(image[row][column])

    return result


def rotate_right(image):
    number_of_rows = len(image)
    number_of_columns = len(image[0])
    result = [[] for i in range(number_of_columns)]

    for row in range(number_of_rows - 1, -1, -1):
        for column in range(number_of_columns):
            result[column].append(image[row][column])

    return result


def map_pixels(image, op):
    result = []
    current_row = []

    for row in image:
        for pixel in row:
            new_pixel = tuple(map(op, pixel))
            current_row.append(new_pixel)
        result.append(current_row)
        current_row = []

    return result


def invert(image):
    return map_pixels(image, lambda x: 255 - x)


def lighten(image, p):
    return map_pixels(image, lambda x: x + int(p * (255 - x)))


def darken(image, p):
    return map_pixels(image, lambda x: x - int(p * x))


def create_histogram(image):
    result = {'red': {}, 'green': {}, 'blue': {}}
    for row in image:
        for pixel in row:
            r, g, b = pixel
            if r in result['red']:
                result['red'][r] += 1
            else:
                result['red'][r] = 1

            if g in result['green']:
                result['green'][g] += 1
            else:
                result['green'][g] = 1

            if b in result['blue']:
                result['blue'][b] += 1
            else:
                result['blue'][b] = 1

    return result