timeit

Програмиране с Python

Курс във Факултета по Математика и Информатика към СУ

Решение на Работа с картинки от Йоан Динков

Обратно към всички решения

Към профила на Йоан Динков

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 7 успешни тест(а)
  • 5 неуспешни тест(а)

Код

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
98
def rotate_left(image):
    rows, columns = dimensions(image)

    result = [[0 for x in range(rows)] for x in range(columns)]

    for row in range(rows):
        for column in range(columns):
            result[row][column] = image[column][rows-row-1]

    return result


def rotate_right(image):
    rows, columns = dimensions(image)

    result = [[0 for x in range(rows)] for x in range(columns)]

    for row in range(rows):
        for column in range(columns):
            result[row][column] = image[columns-column-1][row]

    return result


def invert(image):
    return process_image(image, None, invert_pixel)


def lighten(image, value):
    return process_image(image, value, lighten_pixel)


def darken(image, value):
    return process_image(image, value, darken_pixel)


def process_image(image, value, function):
    rows, columns = dimensions(image)

    result = [[0 for x in range(rows)] for x in range(columns)]

    for row in range(rows):
        for column in range(columns):
            result[row][column] = function(image[row][column], value)

    return result


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


def invert_pixel(pixel, value):
    red, green, blue = pixel

    return 255 - red, 255 - green, 255 - blue


def lighten_pixel(pixel, value):
    red, green, blue = pixel

    return (int(red + value * (255 - red)),
            int(green + value * (255 - green)),
            int(blue + value * (255 - blue)))


def darken_pixel(pixel, value):
    red, green, blue = pixel

    return (int(red - value * (red - 0)),
            int(green - value * (green - 0)),
            int(blue - value * (blue - 0)))


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

    flatten_image = [pixel for row in image for pixel in row]

    for pixel in flatten_image:
        red, green, blue = pixel

        update_dict(result['red'], red, 1)
        update_dict(result['green'], green, 1)
        update_dict(result['blue'], blue, 1)

    return result
    
 
def update_dict(dict, key, value):
    if key in dict:
        dict[key] += value
    else:
        dict[key] = value

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

...E.E.E.E.E
======================================================================
ERROR: test_darken_rectangle (test.TestImages)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
IndexError: list assignment index out of range

======================================================================
ERROR: test_invert_rectangle (test.TestImages)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
IndexError: list assignment index out of range

======================================================================
ERROR: test_lighten_rectangle (test.TestImages)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
IndexError: list assignment index out of range

======================================================================
ERROR: test_rotate_left_rectangle (test.TestImages)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
IndexError: list index out of range

======================================================================
ERROR: test_rotate_right_rectangle (test.TestImages)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
IndexError: list index out of range

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

FAILED (errors=5)

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

Йоан обнови решението на 12.03.2016 21:45 (преди почти 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
def rotate_left(image):
    rows, columns = dimensions(image)

    result = [[0 for x in range(rows)] for x in range(columns)]

    for row in range(rows):
        for column in range(columns):
            result[row][column] = image[column][rows-row-1]

    return result


def rotate_right(image):
    rows, columns = dimensions(image)

    result = [[0 for x in range(rows)] for x in range(columns)]

    for row in range(rows):
        for column in range(columns):
            result[row][column] = image[columns-column-1][row]

    return result


def invert(image):
    return process_image(image, None, invert_pixel)


def lighten(image, value):
    return process_image(image, value, lighten_pixel)


def darken(image, value):
    return process_image(image, value, darken_pixel)


def process_image(image, value, function):
    rows, columns = dimensions(image)

    result = [[0 for x in range(rows)] for x in range(columns)]

    for row in range(rows):
        for column in range(columns):
            result[row][column] = function(image[row][column], value)

    return result


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


def invert_pixel(pixel, value):
    red, green, blue = pixel

    return 255 - red, 255 - green, 255 - blue


def lighten_pixel(pixel, value):
    red, green, blue = pixel

    return (int(red + value * (255 - red)),
            int(green + value * (255 - green)),
            int(blue + value * (255 - blue)))


def darken_pixel(pixel, value):
    red, green, blue = pixel

    return (int(red - value * (red - 0)),
            int(green - value * (green - 0)),
            int(blue - value * (blue - 0)))


def create_histogram(image):
    result = {
        'red': {key: 0 for key in range(256)},
        'green': {key: 0 for key in range(256)},
        'blue': {key: 0 for key in range(256)}
    }

    flatten_image = [pixel for row in image for pixel in row]

    for pixel in flatten_image:
        red, green, blue = pixel

        result['red'][red] += 1
        result['green'][green] += 1
        result['blue'][blue] += 1

    return result

Йоан обнови решението на 14.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
93
94
95
96
97
98
def rotate_left(image):
    rows, columns = dimensions(image)

    result = [[0 for x in range(rows)] for x in range(columns)]

    for row in range(rows):
        for column in range(columns):
            result[row][column] = image[column][rows-row-1]

    return result


def rotate_right(image):
    rows, columns = dimensions(image)

    result = [[0 for x in range(rows)] for x in range(columns)]

    for row in range(rows):
        for column in range(columns):
            result[row][column] = image[columns-column-1][row]

    return result


def invert(image):
    return process_image(image, None, invert_pixel)


def lighten(image, value):
    return process_image(image, value, lighten_pixel)


def darken(image, value):
    return process_image(image, value, darken_pixel)


def process_image(image, value, function):
    rows, columns = dimensions(image)

    result = [[0 for x in range(rows)] for x in range(columns)]

    for row in range(rows):
        for column in range(columns):
            result[row][column] = function(image[row][column], value)

    return result


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


def invert_pixel(pixel, value):
    red, green, blue = pixel

    return 255 - red, 255 - green, 255 - blue


def lighten_pixel(pixel, value):
    red, green, blue = pixel

    return (int(red + value * (255 - red)),
            int(green + value * (255 - green)),
            int(blue + value * (255 - blue)))


def darken_pixel(pixel, value):
    red, green, blue = pixel

    return (int(red - value * (red - 0)),
            int(green - value * (green - 0)),
            int(blue - value * (blue - 0)))


def create_histogram(image):
    result = {
        'red': {key: 0 for key in range(256)},
        'green': {key: 0 for key in range(256)},
        'blue': {key: 0 for key in range(256)}
    }

    flatten_image = [pixel for row in image for pixel in row]

    for pixel in flatten_image:
        red, green, blue = pixel

        update_dict(result['red'], red, 1)
        update_dict(result['green'], green, 1)
        update_dict(result['blue'], blue, 1)

    return result
    
 
def update_dict(dict, key, value):
    if key in dict:
        dict[key] += value
    else:
        dict[key] = value

Йоан обнови решението на 14.03.2016 15:44 (преди почти 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
98
def rotate_left(image):
    rows, columns = dimensions(image)

    result = [[0 for x in range(rows)] for x in range(columns)]

    for row in range(rows):
        for column in range(columns):
            result[row][column] = image[column][rows-row-1]

    return result


def rotate_right(image):
    rows, columns = dimensions(image)

    result = [[0 for x in range(rows)] for x in range(columns)]

    for row in range(rows):
        for column in range(columns):
            result[row][column] = image[columns-column-1][row]

    return result


def invert(image):
    return process_image(image, None, invert_pixel)


def lighten(image, value):
    return process_image(image, value, lighten_pixel)


def darken(image, value):
    return process_image(image, value, darken_pixel)


def process_image(image, value, function):
    rows, columns = dimensions(image)

    result = [[0 for x in range(rows)] for x in range(columns)]

    for row in range(rows):
        for column in range(columns):
            result[row][column] = function(image[row][column], value)

    return result


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


def invert_pixel(pixel, value):
    red, green, blue = pixel

    return 255 - red, 255 - green, 255 - blue


def lighten_pixel(pixel, value):
    red, green, blue = pixel

    return (int(red + value * (255 - red)),
            int(green + value * (255 - green)),
            int(blue + value * (255 - blue)))


def darken_pixel(pixel, value):
    red, green, blue = pixel

    return (int(red - value * (red - 0)),
            int(green - value * (green - 0)),
            int(blue - value * (blue - 0)))


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

    flatten_image = [pixel for row in image for pixel in row]

    for pixel in flatten_image:
        red, green, blue = pixel

        update_dict(result['red'], red, 1)
        update_dict(result['green'], green, 1)
        update_dict(result['blue'], blue, 1)

    return result
    
 
def update_dict(dict, key, value):
    if key in dict:
        dict[key] += value
    else:
        dict[key] = value