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
def rotate_left(image):
    return list(reversed(list(zip(*image))))


def rotate_right(image):
    return (list(zip(*list(reversed(image)))))


def invert(image):
    return list(map(invert_help, image))


def invert_help(each):
    return list(map(func, each))


def func(pixel):
    return (255 - pixel[0], 255 - pixel[1], 255 - pixel[2])


def lighten(image, coef):
    return list([lighten_help(row, coef) for row in image])


def lighten_help(row, coef):
    return list([lightenFunc(each, coef) for each in row])


def lightenFunc(pixel, coef):
    newRed = int(pixel[0] + coef*(255 - pixel[0]))
    newGreen = int(pixel[1] + coef*(255 - pixel[1]))
    newBlue = int(pixel[2] + coef*(255 - pixel[2]))
    return (newRed, newGreen, newBlue)


def darken(image, coef):
    return list([darken_help(row, coef) for row in image])


def darken_help(row, coef):
    return list([darkenFunc(each, coef) for each in row])


def darkenFunc(pixel, coef):
    newRed = int(pixel[0] + coef*pixel[0])
    newGreen = int(pixel[1] + coef*pixel[1])
    newBlue = int(pixel[2] + coef*pixel[2])
    return (newRed, newGreen, newBlue)


def create_histogram(image):
    red = getColour(image, 0)
    green = getColour(image, 1)
    blue = getColour(image, 2)
    return {'red': red, 'green': green, 'blue': blue}


def getColour(image, colour):
    result = {}
    for each in range(0, 256):
        count = countNumber(each, image, colour)
        if count > 0:
            result[each] = count
    return result


def countNumber(el, image, colour):
    result = 0
    for row in image:
        for each in row:
            if each[colour] == el:
                result = result + 1
    return result

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

..FF........
======================================================================
FAIL: test_darken (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: 255 not less than or equal to 1

======================================================================
FAIL: 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
AssertionError: 100 not less than or equal to 1

----------------------------------------------------------------------
Ran 12 tests in 0.089s

FAILED (failures=2)

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

Атанас обнови решението на 13.03.2016 23: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
def rotate_left(image):
    return list(reversed(list(zip(*image))))


def rotate_right(image):
    return (list(zip(*list(reversed(image)))))


def invert(image):
    return list(map(invert_help, image))


def invert_help(each):
    return list(map(func, each))


def func(pixel):
    return (255 - pixel[0], 255 - pixel[1], 255 - pixel[2])


def lighten(image, coef):
    return list([lighten_help(row, coef) for row in image])


def lighten_help(row, coef):
    return list([lightenFunc(each, coef) for each in row])


def lightenFunc(pixel, coef):
    newRed = int(pixel[0] + coef*(255 - pixel[0]))
    newGreen = int(pixel[1] + coef*(255 - pixel[1]))
    newBlue = int(pixel[2] + coef*(255 - pixel[2]))
    return (newRed, newGreen, newBlue)


def darken(image, coef):
    return list([darken_help(row, coef) for row in image])


def darken_help(row, coef):
    return list([darkenFunc(each, coef) for each in row])


def darkenFunc(pixel, coef):
    newRed = int(pixel[0] + coef*pixel[0])
    newGreen = int(pixel[1] + coef*pixel[1])
    newBlue = int(pixel[2] + coef*pixel[2])
    return (newRed, newGreen, newBlue)


def create_histogram(image):
    red = getColour(image, 0)
    green = getColour(image, 1)
    blue = getColour(image, 2)
    return {'red': red, 'green': green, 'blue': blue}


def getColour(image, colour):
    result = {}
    for each in range(0, 256):
        count = countNumber(each, image, colour)
        if count > 0:
            result[each] = count
    return result


def countNumber(el, image, colour):
    result = 0
    for row in image:
        for each in row:
            if each[colour] == el:
                result = result + 1
    return result