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
67
68
69
70
71
72
73
74
75
from collections import defaultdict


def rotate_left(img):
    new = []
    for j in range(len(img[0])-1, -1, -1):
        some = []
        for i in range(len(img)):
            some.append(img[i][j])
        new.append(some)
    return new


def rotate_right(img):
    new = []
    for j in range(len(img[0])):
        some = []
        for i in range(len(img)-1, -1, -1):
            some.append(img[i][j])
        new.append(some)
    return new


def invert(img):
    new = []
    for row in img:
        some = []
        for elem in row:
            r = 255 - elem[0]
            g = 255 - elem[1]
            b = 255 - elem[2]
            pixel = (r, g, b)
            some.append(pixel)
        new.append(some)
    return new


def lighten(img, n):
    new = []
    for row in img:
        some = []
        for elem in row:
            r = elem[0] + int(n * (255 - elem[0]))
            g = elem[1] + int(n * (255 - elem[1]))
            b = elem[2] + int(n * (255 - elem[2]))
            pixel = (r, g, b)
            some.append(pixel)
        new.append(some)
    return new


def darken(img, n):
    new = []
    for row in img:
        some = []
        for elem in row:
            r = elem[0] - int(n * (elem[0]-0))
            g = elem[1] - int(n * (elem[1]-0))
            b = elem[2] - int(n * (elem[2]-0))
            pixel = (r, g, b)
            some.append(pixel)
        new.append(some)
    return new


def create_histogram(img):
    histogram = {'red': defaultdict(int),
                 'green': defaultdict(int),
                 'blue': defaultdict(int)}
    for row in img:
        for elem in row:
            histogram['red'][elem[0]] += 1
            histogram['green'][elem[1]] += 1
            histogram['blue'][elem[2]] += 1
    return histogram

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

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

OK

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

Яница обнови решението на 14.03.2016 14:36 (преди почти 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
from collections import defaultdict


def rotate_left(img):
    new = []
    for j in range(len(img[0])-1, -1, -1):
        some = []
        for i in range(len(img)):
            some.append(img[i][j])
        new.append(some)
    return new


def rotate_right(img):
    new = []
    for j in range(len(img[0])):
        some = []
        for i in range(len(img)-1, -1, -1):
            some.append(img[i][j])
        new.append(some)
    return new


def invert(img):
    new = []
    for row in img:
        some = []
        for elem in row:
            r = 255 - elem[0]
            g = 255 - elem[1]
            b = 255 - elem[2]
            pixel = (r, g, b)
            some.append(pixel)
        new.append(some)
    return new


def lighten(img, n):
    new = []
    for row in img:
        some = []
        for elem in row:
            r = elem[0] + int(n * (255 - elem[0]))
            g = elem[1] + int(n * (255 - elem[1]))
            b = elem[2] + int(n * (255 - elem[2]))
            pixel = (r, g, b)
            some.append(pixel)
        new.append(some)
    return new


def darken(img, n):
    new = []
    for row in img:
        some = []
        for elem in row:
            r = elem[0] - int(n * (elem[0]-0))
            g = elem[1] - int(n * (elem[1]-0))
            b = elem[2] - int(n * (elem[2]-0))
            pixel = (r, g, b)
            some.append(pixel)
        new.append(some)
    return new


def create_histogram(img):
    histogram = {'red': defaultdict(int),
                 'green': defaultdict(int),
                 'blue': defaultdict(int)}
    for row in img:
        for elem in row:
            histogram['red'][elem[0]] += 1
            histogram['green'][elem[1]] += 1
            histogram['blue'][elem[2]] += 1
    return histogram