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
def rotate_left(image):
    """
    Rotate a image by 90 degrees to the left
    :param image: an image represented by2 dimensional list.
    :return: a rotated imaged, again represented by a 2 dimensional list
    """
    rows = len(image) - 1
    columns = len(image[0]) - 1
    return [[image[column][rows - row]
             for column in range(columns + 1)]
            for row in range(rows + 1)]


def rotate_right(image):
    """
    Rotate a image by 90 degrees to the right
    :param image: an image represented by2 dimensional list.
    :return: a rotated imaged, again represented by a 2 dimensional list
    """
    rows = len(image) - 1
    columns = len(image[0]) - 1
    return [[image[columns - column][row]
             for column in range(columns + 1)]
            for row in range(rows + 1)]


def invert(image):
    """
    Create a negative of a given image
    :param image: an image represented by2 dimensional list.
    :return: a rotated imaged, again represented by a 2 dimensional list
    """
    return [[(255 - pixel[0], 255 - pixel[1], 255 - pixel[2])
             for pixel in row] for row in image]


def lighten(image, percent):
    """
    Lighten a picture by X percent
    :param image: A image represented by2 dimensional list.
    :param percent: Value between 0 and 1.
    :return: a rotated imaged, again represented by a 2 dimensional list
    """
    def apply_percent(pixel):
        return tuple(colour + int(percent*(255 - colour)) for colour in pixel)

    return [list(map(apply_percent, row)) for row in image]


def darken(image, percent):
    """
    Darken a picture by X percent
    :param image: A image represented by2 dimensional list.
    :param percent: Value between 0 and 1.
    :return: a rotated imaged, again represented by a 2 dimensional list
    """
    def apply_percent(pixel):
        return tuple(colour - int(percent*colour) for colour in pixel)

    return [list(map(apply_percent, row)) for row in image]


def create_histogram(image):
    """
    Return histogram data for the occurrence of each base pixel colour
    - red blue and green.
    :param image:  A image represented by2 dimensional list.
    :return: Dictionary containing information about the rate
    of occurrence of each value from 0 to 255 for each colour
    """
    base_keys = ("red", "green", "blue")
    histogram = {"red": {}, "blue": {}, "green": {}}
    # Very disgusting tree - should optimise if time permits it.
    # At least it makes one pass
    for row in image:
        for pixel in row:
            for index in range((len(pixel))):
                histogram[base_keys[index]][pixel[index]] = \
                    histogram[base_keys[index]].get(pixel[index], 0) + 1
    return histogram

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

.........E.E
======================================================================
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.085s

FAILED (errors=2)

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

Божидар обнови решението на 12.03.2016 12:56 (преди почти 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
def rotate_left(image):
    """
    Rotate a image by 90 degrees to the left
    :param image: an image represented by2 dimensional list.
    :return: a rotated imaged, again represented by a 2 dimensional list
    """
    rows = len(image) - 1
    columns = len(image[0]) - 1
    return [[image[column][rows - row]
             for column in range(columns + 1)]
            for row in range(rows + 1)]


def rotate_right(image):
    """
    Rotate a image by 90 degrees to the right
    :param image: an image represented by2 dimensional list.
    :return: a rotated imaged, again represented by a 2 dimensional list
    """
    rows = len(image) - 1
    columns = len(image[0]) - 1
    return [[image[columns - column][row]
             for column in range(columns + 1)]
            for row in range(rows + 1)]


def invert(image):
    """
    Create a negative of a given image
    :param image: an image represented by2 dimensional list.
    :return: a rotated imaged, again represented by a 2 dimensional list
    """
    return [[(255 - pixel[0], 255 - pixel[1], 255 - pixel[2])
             for pixel in row] for row in image]


def lighten(image, percent):
    """
    Lighten a picture by X percent
    :param image: A image represented by2 dimensional list.
    :param percent: Value between 0 and 1.
    :return: a rotated imaged, again represented by a 2 dimensional list
    """
    def apply_percent(pixel):
        return tuple(val + int(percent*(255 - val)) for val in pixel)

    return [list(map(apply_percent, row)) for row in image]


def darken(image, percent):
    """
    Darken a picture by X percent
    :param image: A image represented by2 dimensional list.
    :param percent: Value between 0 and 1.
    :return: a rotated imaged, again represented by a 2 dimensional list
    """
    def apply_percent(pixel):
        return tuple(val - int(percent*val) for val in pixel)

    return [list(map(apply_percent, row)) for row in image]


def create_histogram(image):
    """
    Return histogram data for the occurrence of each base pixel colour
    - red blue and green.
    :param image:  A image represented by2 dimensional list.
    :return: Dictionary containing information about the rate
    of occurrence of each value from 0 to 255 for each colour
    """
    base_keys = ("red", "green", "blue")
    histogram = {"red": {}, "blue": {}, "green": {}}
    # Very disgusting tree - should optimise if time permits it.
    # At least it makes one pass
    for row in image:
        for pixel in row:
            for index in range((len(pixel))):
                if pixel[index] in histogram[base_keys[index]]:
                    histogram[base_keys[index]][pixel[index]] += 1
                else:
                    histogram[base_keys[index]][pixel[index]] = 1
    return histogram

Божидар обнови решението на 13.03.2016 08: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
def rotate_left(image):
    """
    Rotate a image by 90 degrees to the left
    :param image: an image represented by2 dimensional list.
    :return: a rotated imaged, again represented by a 2 dimensional list
    """
    rows = len(image) - 1
    columns = len(image[0]) - 1
    return [[image[column][rows - row]
             for column in range(columns + 1)]
            for row in range(rows + 1)]


def rotate_right(image):
    """
    Rotate a image by 90 degrees to the right
    :param image: an image represented by2 dimensional list.
    :return: a rotated imaged, again represented by a 2 dimensional list
    """
    rows = len(image) - 1
    columns = len(image[0]) - 1
    return [[image[columns - column][row]
             for column in range(columns + 1)]
            for row in range(rows + 1)]


def invert(image):
    """
    Create a negative of a given image
    :param image: an image represented by2 dimensional list.
    :return: a rotated imaged, again represented by a 2 dimensional list
    """
    return [[(255 - pixel[0], 255 - pixel[1], 255 - pixel[2])
             for pixel in row] for row in image]


def lighten(image, percent):
    """
    Lighten a picture by X percent
    :param image: A image represented by2 dimensional list.
    :param percent: Value between 0 and 1.
    :return: a rotated imaged, again represented by a 2 dimensional list
    """
    def apply_percent(pixel):
        return tuple(colour + int(percent*(255 - colour)) for colour in pixel)

    return [list(map(apply_percent, row)) for row in image]


def darken(image, percent):
    """
    Darken a picture by X percent
    :param image: A image represented by2 dimensional list.
    :param percent: Value between 0 and 1.
    :return: a rotated imaged, again represented by a 2 dimensional list
    """
    def apply_percent(pixel):
        return tuple(colour - int(percent*colour) for colour in pixel)

    return [list(map(apply_percent, row)) for row in image]


def create_histogram(image):
    """
    Return histogram data for the occurrence of each base pixel colour
    - red blue and green.
    :param image:  A image represented by2 dimensional list.
    :return: Dictionary containing information about the rate
    of occurrence of each value from 0 to 255 for each colour
    """
    base_keys = ("red", "green", "blue")
    histogram = {"red": {}, "blue": {}, "green": {}}
    # Very disgusting tree - should optimise if time permits it.
    # At least it makes one pass
    for row in image:
        for pixel in row:
            for index in range((len(pixel))):
                histogram[base_keys[index]][pixel[index]] = \
                    histogram[base_keys[index]].get(pixel[index], 0) + 1
    return histogram