timeit

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

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

Решение на Работа с картинки от Галин Ангелов

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

Към профила на Галин Ангелов

Резултати

  • 8 точки от тестове
  • 0 бонус точки
  • 8 точки общо
  • 9 успешни тест(а)
  • 3 неуспешни тест(а)

Код

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
99
100
101
102
def rotate_left(image):
    newImage = []
    k = 0
    p = 1
    for row in range(0, len(image)):
        line = []
        for col in range(0, len(image[row])):
            el = image[k][len(image[row])-p]
            line.append(el)
            k += 1
        p += 1
        k = 0
        newImage.append(line)
    return newImage


def rotate_right(image):
    newImage = []
    k = 0
    p = 1
    for row in range(0, len(image)):
        line = []
        for col in range(0, len(image[row])):
            el = image[len(image)-p][k]
            line.append(el)
            k += 1
        p += 1
        k = 0
        newImage.append(line)
    return newImage


def invert(image):
    newImage = []
    for row in range(0, len(image)):
        line = []
        for col in range(0, len(image[row])):
            innerTuple = list(image[row][col])
            for index in range(len(innerTuple)):
                innerTuple[index] = 255 - innerTuple[index]
            line.append(tuple(innerTuple))
        newImage.append(line)
    return newImage


def lighten(image, value):
    newImage = []
    for row in range(0, len(image)):
        line = []
        for col in range(0, len(image[row])):
            innerTuple = list(image[row][col])
            for index in range(len(innerTuple)):
                element = innerTuple[index]
                innerTuple[index] = int(element + value*(255 - element))
            line.append(tuple(innerTuple))
        newImage.append(line)
    return newImage


def darken(image, value):
    newImage = []
    for row in range(0, len(image)):
        line = []
        for col in range(0, len(image[row])):
            innerTuple = list(image[row][col])
            for index in range(len(innerTuple)):
                element = innerTuple[index]
                innerTuple[index] = int(element - value*(element - 0))
            line.append(tuple(innerTuple))
        newImage.append(line)
    return newImage


def create_histogram(image):
    histogram = {}
    red = {}
    green = {}
    blue = {}
    for row in range(0, len(image)):
        for col in range(0, len(image[row])):

            for index in range(len(image[row][col])):
                element = image[row][col][index]
                if index is 0:
                    if element not in red:
                        red[element] = 1
                    else:
                        red[element] += 1
                    histogram['red'] = red
                if index is 1:
                    if element not in green:
                        green[element] = 1
                    else:
                        green[element] += 1
                    histogram['green'] = green
                if index is 2:
                    if element not in blue:
                        blue[element] = 1
                    else:
                        blue[element] += 1
                    histogram['blue'] = blue
    return histogram

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

.........EFF
======================================================================
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

======================================================================
FAIL: test_rotate_right (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: Tuples differ: (255, 0, 0) != (0, 255, 0)

First differing element 0:
255
0

- (255, 0, 0)
+ (0, 255, 0)

======================================================================
FAIL: 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
AssertionError: Tuples differ: (255, 0, 4) != (72, 255, 0)

First differing element 0:
255
72

- (255, 0, 4)
+ (72, 255, 0)

----------------------------------------------------------------------
Ran 12 tests in 0.101s

FAILED (failures=2, errors=1)

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

Галин обнови решението на 14.03.2016 01: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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
def rotate_left(image):
    newImage = []
    k = 0
    p = 1
    for row in range(0, len(image)):
        line = []
        for col in range(0, len(image[row])):
            el = image[k][len(image[row])-p]
            line.append(el)
            k += 1
        p += 1
        k = 0
        newImage.append(line)
    return newImage


def rotate_right(image):
    newImage = []
    k = 0
    p = 1
    for row in range(0, len(image)):
        line = []
        for col in range(0, len(image[row])):
            el = image[len(image)-p][k]
            line.append(el)
            k += 1
        p += 1
        k = 0
        newImage.append(line)
    return newImage


def invert(image):
    newImage = []
    for row in range(0, len(image)):
        line = []
        for col in range(0, len(image[row])):
            innerTuple = list(image[row][col])
            for index in range(len(innerTuple)):
                innerTuple[index] = 255 - innerTuple[index]
            line.append(tuple(innerTuple))
        newImage.append(line)
    return newImage


def lighten(image, value):
    newImage = []
    for row in range(0, len(image)):
        line = []
        for col in range(0, len(image[row])):
            innerTuple = list(image[row][col])
            for index in range(len(innerTuple)):
                element = innerTuple[index]
                innerTuple[index] = int(element + value*(255 - element))
            line.append(tuple(innerTuple))
        newImage.append(line)
    return newImage


def darken(image, value):
    newImage = []
    for row in range(0, len(image)):
        line = []
        for col in range(0, len(image[row])):
            innerTuple = list(image[row][col])
            for index in range(len(innerTuple)):
                element = innerTuple[index]
                innerTuple[index] = int(element - value*(element - 0))
            line.append(tuple(innerTuple))
        newImage.append(line)
    return newImage


def create_histogram(image):
    histogram = {}
    red = {}
    green = {}
    blue = {}
    for row in range(0, len(image)):
        for col in range(0, len(image[row])):

            for index in range(len(image[row][col])):
                element = image[row][col][index]
                if index is 0:
                    if element not in red:
                        red[element] = 1
                    else:
                        red[element] += 1
                    histogram['red'] = red
                if index is 1:
                    if element not in green:
                        green[element] = 1
                    else:
                        green[element] += 1
                    histogram['green'] = green
                if index is 2:
                    if element not in blue:
                        blue[element] = 1
                    else:
                        blue[element] += 1
                    histogram['blue'] = blue
    return histogram