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
def rotate_left(image):
    return_image = [
        [image[y][-x] for y in range(len(image))]
        for x in range(1, len(image[0]) + 1)
    ]
    return return_image


def rotate_right(image):
    return_image = [
        [image[-y][x] for y in range(1, len(image) + 1)]
        for x in range(len(image[0]))
    ]
    return return_image


def invert(image):
    return_image = [
        [(255 - col[0], 255 - col[1], 255 - col[2]) for col in row]
        for row in image
    ]
    return return_image


def lighten(image, coeff):
    return_image = [
        [
            ((int)(col[0] + coeff*(255 - col[0])),
             (int)(col[1] + coeff*(255 - col[1])),
             (int)(col[2] + coeff*(255 - col[2])))
            for col in row
        ]
        for row in image
    ]
    return return_image


def darken(image, coeff):
    return_image = [
        [
            (col[0] - (int)(coeff*col[0]),
             col[1] - (int)(coeff*col[1]),
             col[2] - (int)(coeff*col[2]))
            for col in row
        ]
        for row in image
    ]
    return return_image


def create_histogram(image):
    red = {i: 0 for i in range(256)}
    green = {i: 0 for i in range(256)}
    blue = {i: 0 for i in range(256)}
    for x in range(len(image)):
        for y in range(len(image[0])):
            red[image[x][y][0]] += 1
            green[image[x][y][1]] += 1
            blue[image[x][y][2]] += 1
    histogram = {
        'red': {i: red[i] for i in range(256) if red[i] != 0},
        'green': {i: green[i] for i in range(256) if green[i] != 0},
        'blue': {i: blue[i] for i in range(256) if blue[i] != 0}
    }
    return histogram

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

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

OK

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

Иван обнови решението на 14.03.2016 11: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
def rotate_left(image):
    return_image = [
        [image[y][-x] for y in range(len(image))]
        for x in range(1, len(image[0]) + 1)
    ]
    return return_image


def rotate_right(image):
    return_image = [
        [image[-y][x] for y in range(1, len(image) + 1)]
        for x in range(len(image[0]))
    ]
    return return_image


def invert(image):
    return_image = [
        [(255 - col[0], 255 - col[1], 255 - col[2]) for col in row]
        for row in image
    ]
    return return_image


def lighten(image, coeff):
    return_image = [
        [
            ((int)(col[0] + coeff*(255 - col[0])),
             (int)(col[1] + coeff*(255 - col[1])),
             (int)(col[2] + coeff*(255 - col[2])))
            for col in row
        ]
        for row in image
    ]
    return return_image


def darken(image, coeff):
    return_image = [
        [
            (col[0] - (int)(coeff*col[0]),
             col[1] - (int)(coeff*col[1]),
             col[2] - (int)(coeff*col[2]))
            for col in row
        ]
        for row in image
    ]
    return return_image


def create_histogram(image):
    red = {i: 0 for i in range(256)}
    green = {i: 0 for i in range(256)}
    blue = {i: 0 for i in range(256)}
    for x in range(len(image)):
        for y in range(len(image[0])):
            red[image[x][y][0]] += 1
            green[image[x][y][1]] += 1
            blue[image[x][y][2]] += 1
    histogram = {
        'red': {i: red[i] for i in range(256) if red[i] != 0},
        'green': {i: green[i] for i in range(256) if green[i] != 0},
        'blue': {i: blue[i] for i in range(256) if blue[i] != 0}
    }
    return histogram