timeit

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

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

Решение на Работа с картинки от Ивелин Тодоров

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

Към профила на Ивелин Тодоров

Резултати

  • 8 точки от тестове
  • 0 бонус точки
  • 8 точки общо
  • 10 успешни тест(а)
  • 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
from collections import defaultdict


def rotate_left(img):
    rotated = zip(*(r[::-1] for r in img))
    return list(rotated)


def rotate_right(img):
    rotated = zip(*(r for r in img))
    return list(rotated)


def invert(img):
    inverted = [[tuple([255 - el for el in pix])
                for pix in row]
                for row in img]

    return inverted


def lighten(img, coef):
    lightened = [[tuple([int(el + coef*(255 - el)) for el in pix])
                 for pix in row]
                 for row in img]

    return lightened


def darken(img, coef):
    darkened = [[tuple([int(el - coef*(el - 0)) for el in pix])
                for pix in row]
                for row in img]

    return darkened


def create_histogram(img):
    histogram = {}
    temp = defaultdict(int)
    keys = ('red', 'green', 'blue')
    for ind, key in enumerate(keys):
        for row in img:
            for pix in row:
                temp[pix[ind]] += 1
        histogram[key] = temp
        temp = defaultdict(int)
    return histogram

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

..........FF
======================================================================
FAIL: test_rotate_right (test.TestImages)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: Tuples differ: (0, 255, 0) != (0, 0, 255)

First differing element 1:
255
0

- (0, 255, 0)
+ (0, 0, 255)

======================================================================
FAIL: test_rotate_right_rectangle (test.TestImages)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: Tuples differ: (0, 255, 1) != (100, 0, 255)

First differing element 0:
0
100

- (0, 255, 1)
+ (100, 0, 255)

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

FAILED (failures=2)

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

Ивелин обнови решението на 13.03.2016 20: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
from collections import defaultdict


def rotate_left(img):
    rotated = zip(*(r[::-1] for r in img))
    return list(rotated)


def rotate_right(img):
    rotated = zip(*(r for r in img))
    return list(rotated)


def invert(img):
    inverted = [[tuple([255 - el for el in pix])
                for pix in row]
                for row in img]

    return inverted


def lighten(img, coef):
    lightened = [[tuple([int(el + coef*(255 - el)) for el in pix])
                 for pix in row]
                 for row in img]

    return lightened


def darken(img, coef):
    darkened = [[tuple([int(el - coef*(el - 0)) for el in pix])
                for pix in row]
                for row in img]

    return darkened


def create_histogram(img):
    histogram = {}
    temp = defaultdict(int)
    keys = ('red', 'green', 'blue')
    for ind, key in enumerate(keys):
        for row in img:
            for pix in row:
                temp[pix[ind]] += 1
        histogram[key] = temp
        temp = defaultdict(int)
    return histogram