timeit

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

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

Решение на Работа с картинки от Христо Владев

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

Към профила на Христо Владев

Резултати

  • 10 точки от тестове
  • 0 бонус точки
  • 10 точки общо
  • 12 успешни тест(а)
  • 0 неуспешни тест(а)

Код

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
from collections import defaultdict


def rotate_left(image):
    return list(zip(*image))[::-1]


def rotate_right(image):
    return list(zip(*image[::-1]))


def invert(image):
    return [[invert_pixel(pixel) for pixel in row] for row in image]


def invert_pixel(pixel):
    return tuple(255 - intensity for intensity in pixel)


def lighten(image, coefficient):
    return [
        [lighten_pixel(pixel, coefficient) for pixel in row] for row in image
    ]


def lighten_pixel(pixel, coefficient):
    return tuple(
        int(intensity + coefficient*(255 - intensity)) for intensity in pixel
    )


def darken(image, coefficient):
    return [
        [darken_pixel(pixel, coefficient) for pixel in row] for row in image
    ]


def darken_pixel(pixel, coefficient):
    return tuple(
        int(intensity - coefficient*(intensity - 0)) for intensity in pixel
    )


def create_histogram(image):
    histogram = {
        'red': defaultdict(lambda: 0),
        'green': defaultdict(lambda: 0),
        'blue': defaultdict(lambda: 0)
    }

    for row in image:
        for pixel in row:
            histogram['red'][pixel[0]] += 1
            histogram['green'][pixel[1]] += 1
            histogram['blue'][pixel[2]] += 1

    return histogram

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.092s

OK

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

Христо обнови решението на 14.03.2016 16:50 (преди почти 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
def rotate_left(image):
    return list(zip(*image))[::-1]


def rotate_right(image):
    return list(zip(*image[::-1]))


def invert(image):
    return [[invert_pixel(pixel) for pixel in row] for row in image]


def invert_pixel(pixel):
    return tuple(255 - intensity for intensity in pixel)


def lighten(image, coefficient):
    return [
        [lighten_pixel(pixel, coefficient) for pixel in row] for row in image
    ]


def lighten_pixel(pixel, coefficient):
    return tuple(
        int(intensity + coefficient*(255 - intensity)) for intensity in pixel
    )


def darken(image, coefficient):
    return [
        [darken_pixel(pixel, coefficient) for pixel in row] for row in image
    ]


def darken_pixel(pixel, coefficient):
    return tuple(
        int(intensity - coefficient*(intensity - 0)) for intensity in pixel
    )

Христо обнови решението на 14.03.2016 16: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
from collections import defaultdict


def rotate_left(image):
    return list(zip(*image))[::-1]


def rotate_right(image):
    return list(zip(*image[::-1]))


def invert(image):
    return [[invert_pixel(pixel) for pixel in row] for row in image]


def invert_pixel(pixel):
    return tuple(255 - intensity for intensity in pixel)


def lighten(image, coefficient):
    return [
        [lighten_pixel(pixel, coefficient) for pixel in row] for row in image
    ]


def lighten_pixel(pixel, coefficient):
    return tuple(
        int(intensity + coefficient*(255 - intensity)) for intensity in pixel
    )


def darken(image, coefficient):
    return [
        [darken_pixel(pixel, coefficient) for pixel in row] for row in image
    ]


def darken_pixel(pixel, coefficient):
    return tuple(
        int(intensity - coefficient*(intensity - 0)) for intensity in pixel
    )


def create_histogram(image):
    histogram = {
        'red': defaultdict(lambda: 0),
        'green': defaultdict(lambda: 0),
        'blue': defaultdict(lambda: 0)
    }

    for row in image:
        for pixel in row:
            for red, green, blue in pixel:
                histogram['red'][pixel[red]] += 1
                histogram['green'][pixel[green]] += 1
                histogram['blue'][pixel[blue]] += 1

    return histogram

Христо обнови решението на 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
from collections import defaultdict


def rotate_left(image):
    return list(zip(*image))[::-1]


def rotate_right(image):
    return list(zip(*image[::-1]))


def invert(image):
    return [[invert_pixel(pixel) for pixel in row] for row in image]


def invert_pixel(pixel):
    return tuple(255 - intensity for intensity in pixel)


def lighten(image, coefficient):
    return [
        [lighten_pixel(pixel, coefficient) for pixel in row] for row in image
    ]


def lighten_pixel(pixel, coefficient):
    return tuple(
        int(intensity + coefficient*(255 - intensity)) for intensity in pixel
    )


def darken(image, coefficient):
    return [
        [darken_pixel(pixel, coefficient) for pixel in row] for row in image
    ]


def darken_pixel(pixel, coefficient):
    return tuple(
        int(intensity - coefficient*(intensity - 0)) for intensity in pixel
    )


def create_histogram(image):
    histogram = {
        'red': defaultdict(lambda: 0),
        'green': defaultdict(lambda: 0),
        'blue': defaultdict(lambda: 0)
    }

    for row in image:
        for pixel in row:
            histogram['red'][pixel[0]] += 1
            histogram['green'][pixel[1]] += 1
            histogram['blue'][pixel[2]] += 1

    return histogram