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


def rotate_left(image):
    iteration = range(len(image[0])-1, -1, -1)
    return [[row[column] for row in image] for column in iteration]


def rotate_right(image):
    iteration = range(len(image[0]))
    return [[row[column] for row in image[::-1]] for column in iteration]


def invert(image):
    return [[(255-r, 255-g, 255-b) for r, g, b in row] for row in image]


def lighten_(pixel, alpha):
    return tuple(map(lambda x: int(x + alpha*(255 - x)), pixel))


def lighten(image, alpha):
    return [[lighten_(pixel, alpha) for pixel in row] for row in image]


def darken_(pixel, alpha):
    return tuple(map(lambda x: int(x - alpha*x), pixel))


def darken(image, alpha):
    return [[darken_(pixel, alpha) for pixel in row] for row in image]


def create_histogram(image):
    colours = ('red', 'green', 'blue')
    pixels = list(chain.from_iterable(image))
    by_colour = [[pixel[i] for pixel in pixels] for i in range(3)]
    return {colour: Counter(by_colour[i]) for i, colour in enumerate(colours)}

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.087s

OK

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

Александър обнови решението на 08.03.2016 11:40 (преди почти 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
from itertools import chain
from collections import defaultdict
from collections import Counter


def rotate_left(image):
    iteration = range(len(image[0])-1, -1, -1)
    return [[row[column] for row in image] for column in iteration]


def rotate_right(image):
    iteration = range(len(image[0]))
    return [[row[column] for row in image[::-1]] for column in iteration]


def invert(image):
    return [[(255-r, 255-g, 255-b) for r, g, b in row] for row in image]


def lighten_(pixel, alpha):
    return tuple(map(lambda x: int(x + alpha*(255 - x)), pixel))


def lighten(image, alpha):
    return [[lighten_(pixel, alpha) for pixel in row] for row in image]


def darken_(pixel, alpha):
    return tuple(map(lambda x: int(x - alpha*x), pixel))


def darken(image, alpha):
    return [[darken_(pixel, alpha) for pixel in row] for row in image]


def create_histogram(image):
    colours = ('red', 'green', 'blue')
    histogram = {colour: defaultdict(int) for colour in colours}
    counted_pixels = Counter(chain.from_iterable(image))

    for pixel, occurences in counted_pixels.items():
        for i, colour in enumerate(colours):
            histogram[colour][pixel[i]] += occurences
    return histogram

Александър обнови решението на 08.03.2016 13:30 (преди почти 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
from itertools import chain
from collections import defaultdict
from collections import Counter


def rotate_left(image):
    iteration = range(len(image[0])-1, -1, -1)
    return [[row[column] for row in image] for column in iteration]


def rotate_right(image):
    iteration = range(len(image[0]))
    return [[row[column] for row in image[::-1]] for column in iteration]


def invert(image):
    return [[(255-r, 255-g, 255-b) for r, g, b in row] for row in image]


def lighten_(pixel, alpha):
    return tuple(map(lambda x: int(x + alpha*(255 - x)), pixel))


def lighten(image, alpha):
    return [[lighten_(pixel, alpha) for pixel in row] for row in image]


def darken_(pixel, alpha):
    return tuple(map(lambda x: int(x - alpha*x), pixel))


def darken(image, alpha):
    return [[darken_(pixel, alpha) for pixel in row] for row in image]


def create_histogram(image):
    colours = ('red', 'green', 'blue')
    pixels = list(chain.from_iterable(image))
    by_colour = [[pixel[i] for pixel in pixels] for i in range(3)]
    return {colour: Counter(by_colour[i]) for i, colour in enumerate(colours)}