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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
def rotate_left(image):
    rows = len(image)
    columns = len(image[0])
    rotated = []

    for col in range(columns)[::-1]:
        temp = []
        for row in range(rows):
            temp.append(image[row][col])
        rotated.append(temp)

    return rotated


def rotate_right(image):
    rows = len(image)
    columns = len(image[0])
    rotated = []

    for col in range(columns):
        temp = []
        for row in range(rows)[::-1]:
            temp.append(image[row][col])
        rotated.append(temp)

    return rotated


def invert(image):
    rows = len(image)
    inv = []

    for row in range(rows):
        temp_row = []
        for pixel in image[row]:
            temp_rgb = ()
            for rgb in pixel:
                temp_rgb = temp_rgb + (255 - rgb,)
            temp_row.append(temp_rgb)
        inv.append(temp_row)

    return inv


def lighten(image, n):
    rows = len(image)
    light = []

    for row in range(rows):
        temp_row = []
        for pixel in image[row]:
            temp_rgb = ()
            for rgb in pixel:
                temp_rgb = temp_rgb + (rgb + int(n * (255 - rgb)),)
            temp_row.append(temp_rgb)
        light.append(temp_row)

    return light


def darken(image, n):
    rows = len(image)
    dark = []

    for row in range(rows):
        temp_row = []
        for pixel in image[row]:
            temp_rgb = ()
            for rgb in pixel:
                temp_rgb = temp_rgb + (rgb - int(n * rgb),)
            temp_row.append(temp_rgb)
        dark.append(temp_row)

    return dark


def create_histogram(image):
    hist = {'red': {}, 'green': {}, 'blue': {}}
    colors = {0: 'red', 1: 'green', 2: 'blue'}

    for row in range(len(image)):
        for pixel in image[row]:
            for i in range(3):
                if pixel[i] in hist[colors[i]]:
                    hist[colors[i]][pixel[i]] += 1
                else:
                    hist[colors[i]][pixel[i]] = 1

    return hist

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.101s

OK

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

Ирина обнови решението на 14.03.2016 11:44 (преди почти 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
def rotate_left(image):
    rows = len(image)
    columns = len(image[0])
    rotated = []

    for col in range(columns)[::-1]:
        temp = []
        for row in range(rows):
            temp.append(image[row][col])
        rotated.append(temp)

    return rotated


def rotate_right(image):
    rows = len(image)
    columns = len(image[0])
    rotated = []

    for col in range(columns):
        temp = []
        for row in range(rows)[::-1]:
            temp.append(image[row][col])
        rotated.append(temp)

    return rotated


def invert(image):
    rows = len(image)
    inv = []

    for row in range(rows):
        temp_row = []
        for pixel in image[row]:
            temp_rgb = ()
            for rgb in pixel:
                temp_rgb = temp_rgb + (255 - rgb,)
            temp_row.append(temp_rgb)
        inv.append(temp_row)

    return inv


def lighten(image, n):
    rows = len(image)
    light = []

    for row in range(rows):
        temp_row = []
        for pixel in image[row]:
            temp_rgb = ()
            for rgb in pixel:
                temp_rgb = temp_rgb + (rgb + int(n * (255 - rgb)),)
            temp_row.append(temp_rgb)
        light.append(temp_row)

    return light


def darken(image, n):
    rows = len(image)
    dark = []

    for row in range(rows):
        temp_row = []
        for pixel in image[row]:
            temp_rgb = ()
            for rgb in pixel:
                temp_rgb = temp_rgb + (rgb - int(n * rgb),)
            temp_row.append(temp_rgb)
        dark.append(temp_row)

    return dark


def create_histogram(image):
    hist = {'red': {}, 'green': {}, 'blue': {}}
    colors = {0: 'red', 1: 'green', 2: 'blue'}

    for row in range(len(image)):
        for pixel in image[row]:
            for i in range(3):
                if pixel[i] in hist[colors[i]]:
                    hist[colors[i]][pixel[i]] += 1
                else:
                    hist[colors[i]][pixel[i]] = 1

    return hist