timeit

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

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

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

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

Към профила на Виктор Драганов

Резултати

  • 8 точки от тестове
  • 0 бонус точки
  • 8 точки общо
  • 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
def next_line(image, line_position):
    next = []
    for line in image:
        next.append(tuple(line[line_position]))
    return next

def rotate(dir, image):
    rotated = []
    column_positions = list(range(0, len(image[0])))
    left =  dir == 'left'
    if left: column_positions.reverse()
    
    for i in column_positions:
        line = next_line(image, i)
        if not left:
            line.reverse()
        rotated.append(line)
    return rotated

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

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

def invert_pixel(p):
    return (255 - p[0], 255 - p[1], 255 - p[2])

def invert(image):
    inverted = []
    for line in image:
        i_line = list(map(invert_pixel, line))
        inverted.append(i_line)
    return inverted

# Thats one way to do it

# def lighten_line(line, coef):
#     return list(map((lambda p: (p[0] + int(coef*(255 - p[0])),
#                                 p[1] + int(coef*(255 - p[1])),
#                                 p[2] + int(coef*(255 - p[2])))), line))

# def lighten(image, coef):
#     lightened = []
#     for line in image:
#         i_line = lighten_line(line, coef)
#         lightened.append(i_line)
#     return lightened

def lighten_pixel(p, coef):
    return (p[0] + int(coef*(255 - p[0])),
            p[1] + int(coef*(255 - p[1])),
            p[2] + int(coef*(255 - p[2])))

def darken_pixel(p, coef):
    return (p[0] - int(coef*p[0]),
            p[1] - int(coef*p[1]),
            p[2] - int(coef*p[2]))

# More elegant way with list comprehensions

def apply_action(image, coef, func):
    return [[func(p, coef) for p in line] for line in image]

def lighten(image, coef):
    return apply_action(image, coef, lighten_pixel)

def darken(image, coef):
    return apply_action(image, coef, darken_pixel)

def create_histogram(image):
    #hist = {key: {k: None for k in range(0, 256)} for key in ['red', 'green', 'blue']}st
    r = {}
    g = {}
    b = {}
    for line in image:
        for p in line:
            if p in r:
                r[p[0]] += 1
            else:
                r[p[0]] = 1

            if p in g:
                g[p[1]] += 1
            else:
                g[p[1]] = 1

            if p in b:
                b[p[2]] += 1
            else:
                 b[p[2]] = 1
    hist = {'red' : r,
            'green': g,
            'blue': b}
    return hist

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

FF..........
======================================================================
FAIL: test_create_histogram (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
AssertionError: {'green': {0: 1, 255: 1}, 'blue': {0: 1, 255: 1}, 'red': {0: 1, 255: 1}} != {'blue': {0: 6, 255: 3}, 'green': {0: 5, 255: 4}, 'red': {0: 7, 255: 2}}
- {'blue': {0: 1, 255: 1}, 'green': {0: 1, 255: 1}, 'red': {0: 1, 255: 1}}
?              ^       ^                ^       ^              ^       ^

+ {'blue': {0: 6, 255: 3}, 'green': {0: 5, 255: 4}, 'red': {0: 7, 255: 2}}
?              ^       ^                ^       ^              ^       ^


======================================================================
FAIL: test_create_histogram_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
AssertionError: {'green': {0: 1, 54: 1, 60: 1, 42: 1, 12: 1, 255: 1}, '[80 chars]: 1}} != {'green': {0: 4, 54: 1, 12: 1, 42: 1, 60: 1, 255: 4}, '[80 chars]: 3}}
- {'blue': {0: 1, 1: 1, 4: 1, 255: 1},
+ {'blue': {0: 5, 1: 2, 4: 1, 255: 4},
-  'green': {0: 1, 12: 1, 42: 1, 54: 1, 60: 1, 255: 1},
?               ^                                   ^

+  'green': {0: 4, 12: 1, 42: 1, 54: 1, 60: 1, 255: 4},
?               ^                                   ^

-  'red': {0: 1, 1: 1, 72: 1, 90: 1, 100: 1, 255: 1}}
?               ------                            ^

+  'red': {0: 5, 1: 1, 72: 1, 90: 1, 100: 1, 255: 3}}
?             ++++++                              ^


----------------------------------------------------------------------
Ran 12 tests in 0.094s

FAILED (failures=2)

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

Виктор обнови решението на 14.03.2016 16:58 (преди почти 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 next_line(image, line_position):
    next = []
    for line in image:
        next.append(tuple(line[line_position]))
    return next

def rotate(dir, image):
    rotated = []
    column_positions = list(range(0, len(image[0])))
    left =  dir == 'left'
    if left: column_positions.reverse()
    
    for i in column_positions:
        line = next_line(image, i)
        if not left:
            line.reverse()
        rotated.append(line)
    return rotated

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

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

def invert_pixel(p):
    return (255 - p[0], 255 - p[1], 255 - p[2])

def invert(image):
    inverted = []
    for line in image:
        i_line = list(map(invert_pixel, line))
        inverted.append(i_line)
    return inverted

# Thats one way to do it

# def lighten_line(line, coef):
#     return list(map((lambda p: (p[0] + int(coef*(255 - p[0])),
#                                 p[1] + int(coef*(255 - p[1])),
#                                 p[2] + int(coef*(255 - p[2])))), line))

# def lighten(image, coef):
#     lightened = []
#     for line in image:
#         i_line = lighten_line(line, coef)
#         lightened.append(i_line)
#     return lightened

def lighten_pixel(p, coef):
    return (p[0] + int(coef*(255 - p[0])),
            p[1] + int(coef*(255 - p[1])),
            p[2] + int(coef*(255 - p[2])))

def darken_pixel(p, coef):
    return (p[0] - int(coef*p[0]),
            p[1] - int(coef*p[1]),
            p[2] - int(coef*p[2]))

# More elegant way with list comprehensions

def apply_action(image, coef, func):
    return [[func(p, coef) for p in line] for line in image]

def lighten(image, coef):
    return apply_action(image, coef, lighten_pixel)

def darken(image, coef):
    return apply_action(image, coef, darken_pixel)

def create_histogram(image):
    #hist = {key: {k: None for k in range(0, 256)} for key in ['red', 'green', 'blue']}st
    r = {}
    g = {}
    b = {}
    hist = {'red' : r,
            'green': g,
            'blue': b}
    print(hist)
    for line in image:
        for p in line:
            if p in r:
                r[p[0]] += 1
            else:
                r[p[0]] = 1

            if p in g:
                g[p[1]] += 1
            else:
                g[p[1]] = 1

            if p in b:
                b[p[2]] += 1
            else:
                 b[p[2]] = 1
    return hist

Виктор обнови решението на 14.03.2016 16:59 (преди почти 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
def next_line(image, line_position):
    next = []
    for line in image:
        next.append(tuple(line[line_position]))
    return next

def rotate(dir, image):
    rotated = []
    column_positions = list(range(0, len(image[0])))
    left =  dir == 'left'
    if left: column_positions.reverse()
    
    for i in column_positions:
        line = next_line(image, i)
        if not left:
            line.reverse()
        rotated.append(line)
    return rotated

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

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

def invert_pixel(p):
    return (255 - p[0], 255 - p[1], 255 - p[2])

def invert(image):
    inverted = []
    for line in image:
        i_line = list(map(invert_pixel, line))
        inverted.append(i_line)
    return inverted

# Thats one way to do it

# def lighten_line(line, coef):
#     return list(map((lambda p: (p[0] + int(coef*(255 - p[0])),
#                                 p[1] + int(coef*(255 - p[1])),
#                                 p[2] + int(coef*(255 - p[2])))), line))

# def lighten(image, coef):
#     lightened = []
#     for line in image:
#         i_line = lighten_line(line, coef)
#         lightened.append(i_line)
#     return lightened

def lighten_pixel(p, coef):
    return (p[0] + int(coef*(255 - p[0])),
            p[1] + int(coef*(255 - p[1])),
            p[2] + int(coef*(255 - p[2])))

def darken_pixel(p, coef):
    return (p[0] - int(coef*p[0]),
            p[1] - int(coef*p[1]),
            p[2] - int(coef*p[2]))

# More elegant way with list comprehensions

def apply_action(image, coef, func):
    return [[func(p, coef) for p in line] for line in image]

def lighten(image, coef):
    return apply_action(image, coef, lighten_pixel)

def darken(image, coef):
    return apply_action(image, coef, darken_pixel)

def create_histogram(image):
    #hist = {key: {k: None for k in range(0, 256)} for key in ['red', 'green', 'blue']}st
    r = {}
    g = {}
    b = {}
    for line in image:
        for p in line:
            if p in r:
                r[p[0]] += 1
            else:
                r[p[0]] = 1

            if p in g:
                g[p[1]] += 1
            else:
                g[p[1]] = 1

            if p in b:
                b[p[2]] += 1
            else:
                 b[p[2]] = 1
    hist = {'red' : r,
            'green': g,
            'blue': b}
    return hist