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
def rotate_left(image):
    return rotate(image, reversed)


def rotate_right(image):
    return rotate(reversed(image), lambda x: x)


def rotate(image, rev):
    rotated_image = []
    for row in image:
        i = 0
        for col in rev(row):
            if len(rotated_image) <= i:
                rotated_image.append([col])
            else:
                rotated_image[i].append(col)
            i += 1
    return rotated_image


def invert(image):
    inverted_image = []
    i = 0
    for row in image:
        for col in row:
            current_pixel = tuple(255 - x for x in col)
            if len(inverted_image) <= i:
                inverted_image.append([current_pixel])
            else:
                inverted_image[i].append(current_pixel)
        i += 1
    return inverted_image


def darken(image, factor):
    return darken_lighten(image, lambda x: x - factor * x)


def lighten(image, factor):
    return darken_lighten(image, lambda x: x + factor * (255 - x))


def darken_lighten(image, formula):
    new_image = []
    i = 0
    for row in image:
        for col in row:
            current_pixel = tuple(int(formula(x)) for x in col)
            if len(new_image) <= i:
                new_image.append([current_pixel])
            else:
                new_image[i].append(current_pixel)
        i += 1
    return new_image


def create_histogram(image):
    histogram = {'red': {}, 'green': {}, 'blue': {}}
    for row in image:
        for col in row:
            for value, color in zip(col, ['red', 'green', 'blue']):
                if value in histogram[color]:
                    histogram[color][value] += 1
                else:
                    histogram[color][value] = 1
    return histogram

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.084s

OK

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

Николай обнови решението на 08.03.2016 23:12 (преди почти 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
def rotate_left(image):
    rotated_image = []
    for row in image:
        i = 0
        for col in reversed(row):
            if len(rotated_image) <= i:
                rotated_image.append([col])
            else:
                rotated_image[i].append(col)
            i += 1
    return rotated_image
    
def rotate_right(image):
    rotated_image = []
    for row in reversed(image):
        i = 0
        for col in row:
            if len(rotated_image) <= i:
                rotated_image.append([col])
            else:
                rotated_image[i].append(col)
            i += 1
    return rotated_image
   
def invert(image):
    inverted_image = []
    i = 0
    for row in image:
        for col in row:
            current_pixel = tuple( 255 - x for x in col)
            if len(inverted_image) <= i:
                inverted_image.append([current_pixel])
            else:
                inverted_image[i].append(current_pixel)
        i += 1
    return inverted_image
    
def lighten(image, factor):
    lightened_image = []
    i = 0
    for row in image:
        for col in row:
            current_pixel = tuple(int(x + factor * (255 - x)) for x in col)
            if len(lightened_image) <= i:
                lightened_image.append([current_pixel])
            else:
                lightened_image[i].append(current_pixel)
        i += 1
    return lightened_image
    
def darken(image, factor):
    darkened_image = []
    i = 0
    for row in image:
        for col in row:
            current_pixel = tuple(int(x - factor * x) for x in col)
            if len(darkened_image) <= i:
                darkened_image.append([current_pixel])
            else:
                darkened_image[i].append(current_pixel)
        i += 1
    return darkened_image
    
def create_histogram(image):
    histogram = {'red': {}, 'green': {}, 'blue': {}}
    for row in image:
        for col in row:
            if col[0] in histogram['red']:
                histogram['red'][col[0]] += 1
            else:
                histogram['red'][col[0]] = 1
                
            if col[1] in histogram['green']:
                histogram['green'][col[1]] += 1
            else:
                histogram['green'][col[1]] = 1
                
            if col[2] in histogram['blue']:
                histogram['blue'][col[2]] += 1
            else:
                histogram['blue'][col[2]] = 1
    return histogram

Николай обнови решението на 10.03.2016 08:12 (преди почти 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
def rotate_left(image):
    return rotate(image, reversed)


def rotate_right(image):
    return rotate(reversed(image), lambda x: x)


def rotate(image, rev):
    rotated_image = []
    for row in image:
        i = 0
        for col in rev(row):
            if len(rotated_image) <= i:
                rotated_image.append([col])
            else:
                rotated_image[i].append(col)
            i += 1
    return rotated_image


def invert(image):
    inverted_image = []
    i = 0
    for row in image:
        for col in row:
            current_pixel = tuple(255 - x for x in col)
            if len(inverted_image) <= i:
                inverted_image.append([current_pixel])
            else:
                inverted_image[i].append(current_pixel)
        i += 1
    return inverted_image


def darken(image, factor):
    return darken_lighten(image, lambda x: x - factor * x)


def lighten(image, factor):
    return darken_lighten(image, lambda x: x + factor * (255 - x))


def darken_lighten(image, formula):
    new_image = []
    i = 0
    for row in image:
        for col in row:
            current_pixel = tuple(int(formula(x)) for x in col)
            if len(new_image) <= i:
                new_image.append([current_pixel])
            else:
                new_image[i].append(current_pixel)
        i += 1
    return new_image


def create_histogram(image):
    histogram = {'red': {}, 'green': {}, 'blue': {}}
    for row in image:
        for col in row:
            for value, color in zip(col, ['red', 'green', 'blue']):
                if value in histogram[color]:
                    histogram[color][value] += 1
                else:
                    histogram[color][value] = 1
    return histogram