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


def extract_shades(color, pixels):
    return [pixel[color] for pixel in pixels]


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


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


def invert(image):
    pixels = [[[255-elem for elem in pixel] for pixel in row] for row in image]
    return [[tuple(pixel) for pixel in row] for row in pixels]


def lighten(image, number):
    pixels = [[[int(elem+number*(255-elem))
                for elem in pixel]for pixel in row]for row in image]
    return [[tuple(pixel) for pixel in row] for row in pixels]


def darken(image, number):
    pixels = [[[int(elem-number*elem)
                for elem in pixel] for pixel in row] for row in image]
    return [[tuple(pixel) for pixel in row] for row in pixels]


def create_histogram(image):
    pixels = [pixel for row in image for pixel in row]
    red = dict(Counter(extract_shades(0, pixels)))
    green = dict(Counter(extract_shades(1, pixels)))
    blue = dict(Counter(extract_shades(2, pixels)))
    return {'red': red, 'green': green, 'blue': blue}

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

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

OK

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

Деница обнови решението на 13.03.2016 10:54 (преди почти 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
from collections import Counter


def extract(color, pixels):
    return [pixel[color] for pixel in pixels]


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


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


def invert(image):
    pixels = [[[255-elem for elem in pixel] for pixel in row] for row in image]
    return [[tuple(pixel) for pixel in row] for row in pixels]


def lighten(image, number):
    pixels = [[[int(elem+number*(255-elem))
                for elem in pixel]for pixel in row]for row in image]
    return [[tuple(pixel) for pixel in row] for row in pixels]


def darken(image, number):
    pixels = [[[int(elem-number*elem)
                for elem in pixel] for pixel in row] for row in image]
    return [[tuple(pixel) for pixel in row] for row in pixels]


def create_histogram(image):
    pixels = [pixel for row in image for pixel in row]
    red = dict(Counter(extract(0, pixels)))
    green = dict(Counter(extract(1, pixels)))
    blue = dict(Counter(extract(2, pixels)))
    return {'red': red, 'green': green, 'blue': blue}

Деница обнови решението на 14.03.2016 10:37 (преди почти 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
from collections import Counter


def extract_shades(color, pixels):
    return [pixel[color] for pixel in pixels]


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


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


def invert(image):
    pixels = [[[255-elem for elem in pixel] for pixel in row] for row in image]
    return [[tuple(pixel) for pixel in row] for row in pixels]


def lighten(image, number):
    pixels = [[[int(elem+number*(255-elem))
                for elem in pixel]for pixel in row]for row in image]
    return [[tuple(pixel) for pixel in row] for row in pixels]


def darken(image, number):
    pixels = [[[int(elem-number*elem)
                for elem in pixel] for pixel in row] for row in image]
    return [[tuple(pixel) for pixel in row] for row in pixels]


def create_histogram(image):
    pixels = [pixel for row in image for pixel in row]
    red = dict(Counter(extract_shades(0, pixels)))
    green = dict(Counter(extract_shades(1, pixels)))
    blue = dict(Counter(extract_shades(2, pixels)))
    return {'red': red, 'green': green, 'blue': blue}