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
def rotate_left(image):
    result = []
    transposed = [list(i) for i in zip(*image)]
    for row in transposed[::-1]:
        result.append(row)
    return result


def rotate_right(image):
    return rotate_left(rotate_left(rotate_left(image)))


def edit(image, level, kind):
    editions = {
        'invert': (lambda x: (int(255 - x))),
        'lighten': (lambda x: (int(x + level*(255 - x)))),
        'darken': (lambda x: (int(x - level*(x - 0))))
    }
    result = []
    for row in image:
        new_row = []
        for pixel in row:
            new_pixel = tuple(map(editions[kind], pixel))
            new_row.append(new_pixel)
        result.append(new_row)
    return result


def invert(image):
    return edit(image, None, 'invert')


def lighten(image, level):
    return edit(image, level, 'lighten')


def darken(image, level):
    return edit(image, level, 'darken')


def extract_colors(image):
    result = [[], [], []]
    for item in result:
        for row in image:
            for pixel in row:
                item.append(pixel[result.index(item)])
    return result


def create_histogram(image):
    result = {}
    colors_info = extract_colors(image)
    colors = ['red', 'green', 'blue']
    for color in colors:
        index = colors.index(color)
        levels = {}
        for number in range(0, 256):
            if colors_info[index].count(number) != 0:
                levels[number] = colors_info[index].count(number)
        result[color] = levels
    return result

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.086s

OK

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

Любослава обнови решението на 12.03.2016 12:52 (преди почти 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
def rotate_left(image):
    result = []
    transposed = [list(i) for i in zip(*image)]
    for row in transposed[::-1]:
        result.append(row)
    return result


def rotate_right(image):
    return rotate_left(rotate_left(rotate_left(image)))


def invert(image):
    result = []
    for row in image:
        new_row = []
        for pixel in row:
            new_pixel = ()
            for color in pixel:
                new_pixel = new_pixel + (int(255 - color),)
            new_row.append(new_pixel)
        result.append(new_row)
    return result


def lighten(image, level):
    result = []
    for row in image:
        new_row = []
        for pixel in row:
            new_pixel = ()
            for color in pixel:
                new_pixel = new_pixel + (int(color + level*(255 - color)),)
            new_row.append(new_pixel)
        result.append(new_row)
    return result


def darken(image, level):
    result = []
    for row in image:
        new_row = []
        for pixel in row:
            new_pixel = ()
            for color in pixel:
                new_pixel = new_pixel + (int(color - level*(color - 0)),)
            new_row.append(new_pixel)
        result.append(new_row)
    return result


def extract_colors(image):
    result = [[], [], []]
    for item in result:
        for row in image:
            for pixel in row:
                item.append(pixel[result.index(item)])
    return result


def create_histogram(image):
    result = {}
    colors_info = extract_colors(image)
    colors = ['red', 'green', 'blue']
    for color in colors:
        index = colors.index(color)
        levels = {}
        for number in range(0, 256):
            if colors_info[index].count(number) != 0:
                levels[number] = colors_info[index].count(number)
            else:
                continue
        result[color] = levels
    return result
  • invert, lighten и darken са почти еднакви. Измисли как да избегнеш това повторение
  • Няма добра причина да правиш това new_pixel = (). Няколко реда по-късно не го мутираш, а създаваш нов tuple
  • Опитай се да минеш без конкатенация на tuple-ли
  • Няма смисъл от това else: continue в create_histogram
  • Креативна имплементация на rotate_right :)

Любослава обнови решението на 14.03.2016 01: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
def rotate_left(image):
    result = []
    transposed = [list(i) for i in zip(*image)]
    for row in transposed[::-1]:
        result.append(row)
    return result


def rotate_right(image):
    return rotate_left(rotate_left(rotate_left(image)))


def invert(image):
    result = []
    for row in image:
        new_row = []
        for pixel in row:
            new_pixel = tuple(map((lambda x: (int(255 - x))), pixel))
            new_row.append(new_pixel)
        result.append(new_row)
    return result


def lighten(image, level):
    result = []
    for row in image:
        new_row = []
        for pixel in row:
            a = tuple(map((lambda x: (int(x + level*(255 - x)))), pixel))
            new_row.append(a)
        result.append(new_row)
    return result


def darken(image, level):
    result = []
    for row in image:
        new_row = []
        for pixel in row:
            new_pixel = tuple(map((lambda x: (int(x - level*(x - 0)))), pixel))
            new_row.append(new_pixel)
        result.append(new_row)
    return result


def extract_colors(image):
    result = [[], [], []]
    for item in result:
        for row in image:
            for pixel in row:
                item.append(pixel[result.index(item)])
    return result


def create_histogram(image):
    result = {}
    colors_info = extract_colors(image)
    colors = ['red', 'green', 'blue']
    for color in colors:
        index = colors.index(color)
        levels = {}
        for number in range(0, 256):
            if colors_info[index].count(number) != 0:
                levels[number] = colors_info[index].count(number)
            else:
                continue
        result[color] = levels
    return result

Любослава обнови решението на 14.03.2016 01:14 (преди почти 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
def rotate_left(image):
    result = []
    transposed = [list(i) for i in zip(*image)]
    for row in transposed[::-1]:
        result.append(row)
    return result


def rotate_right(image):
    return rotate_left(rotate_left(rotate_left(image)))


def invert(image):
    result = []
    for row in image:
        new_row = []
        for pixel in row:
            new_pixel = tuple(map((lambda x: (int(255 - x))), pixel))
            new_row.append(new_pixel)
        result.append(new_row)
    return result


def lighten(image, level):
    result = []
    for row in image:
        new_row = []
        for pixel in row:
            a = tuple(map((lambda x: (int(x + level*(255 - x)))), pixel))
            new_row.append(a)
        result.append(new_row)
    return result


def darken(image, level):
    result = []
    for row in image:
        new_row = []
        for pixel in row:
            new_pixel = tuple(map((lambda x: (int(x - level*(x - 0)))), pixel))
            new_row.append(new_pixel)
        result.append(new_row)
    return result


def extract_colors(image):
    result = [[], [], []]
    for item in result:
        for row in image:
            for pixel in row:
                item.append(pixel[result.index(item)])
    return result


def create_histogram(image):
    result = {}
    colors_info = extract_colors(image)
    colors = ['red', 'green', 'blue']
    for color in colors:
        index = colors.index(color)
        levels = {}
        for number in range(0, 256):
            if colors_info[index].count(number) != 0:
                levels[number] = colors_info[index].count(number)
        result[color] = levels
    return result

Любослава обнови решението на 14.03.2016 01:25 (преди почти 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
def rotate_left(image):
    result = []
    transposed = [list(i) for i in zip(*image)]
    for row in transposed[::-1]:
        result.append(row)
    return result


def rotate_right(image):
    return rotate_left(rotate_left(rotate_left(image)))


def edit(image, level, kind):
    editions = {
        'invert': (lambda x: (int(255 - x))),
        'lighten': (lambda x: (int(x + level*(255 - x)))),
        'darken': (lambda x: (int(x - level*(x - 0))))
    }
    result = []
    for row in image:
        new_row = []
        for pixel in row:
            new_pixel = tuple(map(editions[kind], pixel))
            new_row.append(new_pixel)
        result.append(new_row)
    return result


def invert(image):
    return edit(image, None, 'invert')


def lighten(image, level):
    return edit(image, level, 'lighten')


def darken(image, level):
    return edit(image, level, 'darken')


def extract_colors(image):
    result = [[], [], []]
    for item in result:
        for row in image:
            for pixel in row:
                item.append(pixel[result.index(item)])
    return result


def create_histogram(image):
    result = {}
    colors_info = extract_colors(image)
    colors = ['red', 'green', 'blue']
    for color in colors:
        index = colors.index(color)
        levels = {}
        for number in range(0, 256):
            if colors_info[index].count(number) != 0:
                levels[number] = colors_info[index].count(number)
        result[color] = levels
    return result