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


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


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


def invert(image):
    return [list(map(lambda x: (255 - x[0], 255 - x[1], 255 - x[2]), row))
            for row in image]


def lighten(image, coef):
    return [list(map(
             lambda x: (x[0] + int(coef*(255 - x[0])),
                        x[1] + int(coef*(255 - x[1])),
                        x[2] + int(coef*(255 - x[2]))), row)) for row in image]


def darken(image, coef):
    return [list(map(
             lambda x: (x[0] - int(coef*(x[0] - 0)),
                        x[1] - int(coef*(x[1] - 0)),
                        x[2] - int(coef*(x[2] - 0))), row)) for row in image]


def create_histogram(image):
    color_indice = [('red', 0), ('green', 1), ('blue', 2)]
    pixels = [pixel for row in image for pixel in row]
    histogram = {color: dict(d) for color, index in color_indice
                 for d in [Counter([elem[index] for elem in pixels])]}

    return histogram

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

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

OK

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

Кристофър обнови решението на 08.03.2016 23: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
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
from collections import defaultdict


def rotate_left(image):
    rotated_image = []
    height = len(image)
    width = len(image[0])

    j = width - 1
    while j >= 0:
        rotated_image.append([])
        i = 0
        while i < height:
            rotated_image[-1].append(image[i][j])
            i = i + 1
        j = j - 1

    return rotated_image


def rotate_right(image):
    rotated_image = []
    height = len(image)
    width = len(image[0])

    j = 0
    while j < width:
        rotated_image.append([])
        i = height - 1
        while i >= 0:
            rotated_image[-1].append(image[i][j])
            i = i - 1
        j = j + 1

    return rotated_image


def invert(image):
    inverted_image = []

    for row in image:
        inverted_image.append([])
        for pixel in row:
            r, g, b = pixel
            inverted_image[-1].append((255 - r, 255 - g, 255 - b))

    return inverted_image


def lighten(image, coef):
    lightened_image = []

    for row in image:
        lightened_image.append([])
        for pixel in row:
            r, g, b = pixel
            lightened_image[-1].append((
                 (r + int(coef*(255 - r))),
                 (g + int(coef*(255 - g))),
                 (b + int(coef*(255 - b)))
            ))

    return lightened_image


def darken(image, coef):
    darkened_image = []

    for row in image:
        darkened_image.append([])
        for pixel in row:
            r, g, b = pixel
            darkened_image[-1].append((
                 (r - int(coef*(r - 0))),
                 (g - int(coef*(g - 0))),
                 (b - int(coef*(b - 0)))
            ))

    return darkened_image


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

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

    return histogram

Кристофър обнови решението на 09.03.2016 17:49 (преди почти 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
from collections import Counter


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


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


def invert(image):
    return [list(map(lambda x: (255 - x[0], 255 - x[1], 255 - x[2]), row))
            for row in image]


def lighten(image, coef):
    return [list(map(
             lambda x: (x[0] + int(coef*(255 - x[0])),
                        x[1] + int(coef*(255 - x[1])),
                        x[2] + int(coef*(255 - x[2]))), row)) for row in image]


def darken(image, coef):
    return [list(map(
             lambda x: (x[0] - int(coef*(x[0] - 0)),
                        x[1] - int(coef*(x[1] - 0)),
                        x[2] - int(coef*(x[2] - 0))), row)) for row in image]


def create_histogram(image):
    color_indice = [('red', 0), ('green', 1), ('blue', 2)]
    pixels = [pixel for row in image for pixel in row]
    histogram = {color: dict(d) for color, index in color_indice
                 for d in [Counter([elem[index] for elem in pixels])]}

    return histogram