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
58
59
60
61
62
63
64
65
66
from collections import defaultdict


def rotate_left(picture):
    rotated_picture = list(zip(*picture))[::-1]
    return rotated_picture


def rotate_right(picture):
    rotated_picture = list(zip(*picture[::-1]))
    return rotated_picture


def invert(picture):
    def invert_pixel(pixel):
        return 255 - pixel[0], 255 - pixel[1], 255 - pixel[2]

    inverted_picture = [[invert_pixel(pixel) for pixel in row]
                        for row in picture]
    return inverted_picture


def lighten(picture, level):
    def lighten_color(color):
        return int(color + level * (255 - color))

    def lighten_pixel(pixel):
        return (lighten_color(pixel[0]),
                lighten_color(pixel[1]),
                lighten_color(pixel[2]))

    lightened_picture = [[lighten_pixel(pixel)
                         for pixel in row]
                         for row in picture]
    return lightened_picture


def darken(picture, level):
    def darken_color(color):
        return int(color - color * level)

    def darken_pixel(pixel):
        return (darken_color(pixel[0]),
                darken_color(pixel[1]),
                darken_color(pixel[2]))

    darkened_picture = [[darken_pixel(pixel)
                        for pixel in row]
                        for row in picture]
    return darkened_picture


def create_histogram(picture):
    histogram = {}
    red_levels = defaultdict(lambda: 0)
    green_levels = defaultdict(lambda: 0)
    blue_levels = defaultdict(lambda: 0)
    histogram['red'] = red_levels
    histogram['green'] = green_levels
    histogram['blue'] = blue_levels
    for row in picture:
        for pixel in row:
            red_levels[pixel[0]] += 1
            green_levels[pixel[1]] += 1
            blue_levels[pixel[2]] += 1
    return histogram

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.110s

OK

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

Деян обнови решението на 13.03.2016 22:26 (преди почти 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
from collections import defaultdict


def rotate_left(picture):
    rotated_picture = list(zip(*picture))[::-1]
    return rotated_picture


def rotate_right(picture):
    rotated_picture = list(zip(*picture[::-1]))
    return rotated_picture


def invert(picture):
    def invert_pixel(pixel):
        return 255 - pixel[0], 255 - pixel[1], 255 - pixel[2]

    inverted_picture = [[invert_pixel(pixel) for pixel in row]
                        for row in picture]
    return inverted_picture


def lighten(picture, level):
    def lighten_color(color):
        return int(color + level * (255 - color))

    def lighten_pixel(pixel):
        return (lighten_color(pixel[0]),
                lighten_color(pixel[1]),
                lighten_color(pixel[2]))

    lightened_picture = [[lighten_pixel(pixel)
                         for pixel in row]
                         for row in picture]
    return lightened_picture


def darken(picture, level):
    def darken_color(color):
        return int(color - color * level)

    def darken_pixel(pixel):
        return (darken_color(pixel[0]),
                darken_color(pixel[1]),
                darken_color(pixel[2]))

    darkened_picture = [[darken_pixel(pixel)
                        for pixel in row]
                        for row in picture]
    return darkened_picture


def create_histogram(picture):
    histogram = {}
    red_levels = defaultdict(lambda: 0)
    green_levels = defaultdict(lambda: 0)
    blue_levels = defaultdict(lambda: 0)
    histogram['red'] = red_levels
    histogram['green'] = green_levels
    histogram['blue'] = blue_levels
    for row in picture:
        for pixel in row:
            red_levels[pixel[0]] += 1
            green_levels[pixel[1]] += 1
            blue_levels[pixel[2]] += 1
    return histogram