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
def transpose(matrix):
    res = [list(i) for i in zip(*matrix)]
    return res


def rotate_left(image):
    transposed_image = transpose(image)
    new_image = transposed_image[::-1]
    return new_image


def rotate_right(image):
    reversed_image = image[::-1]
    new_image = transpose(reversed_image)
    return new_image


def invert_pixel(pixel):
    return (255 - pixel[0], 255 - pixel[1], 255 - pixel[2])


def invert_row(row):
    return list(map(invert_pixel, row))


def invert(image):
    return list(map(invert_row, image))


def lighten_pixel(pixel, number):
    red = pixel[0] + number * (255 - pixel[0])
    green = pixel[1] + number * (255 - pixel[1])
    blue = pixel[2] + number * (255 - pixel[2])
    return (int(red), int(green), int(blue))


def lighten(image, number):
    new_image = []
    for row in image:
        current_row = list(map(lambda x: lighten_pixel(x, number), row))
        new_image.append(current_row)
    return new_image


def darken_pixel(pixel, number):
    red = pixel[0] - number * pixel[0]
    green = pixel[1] - number * pixel[1]
    blue = pixel[2] - number * pixel[2]
    return (int(red), int(green), int(blue))


def darken(image, number):
    new_image = []
    for row in image:
        current_row = list(map(lambda x: darken_pixel(x, number), row))
        new_image.append(current_row)
    return new_image


def create_histogram(image):
    histogram = {'red': {}, 'green': {}, 'blue': {}}
    for row in image:
        for (r, g, b) in row:
            if r in histogram['red']:
                histogram['red'][r] += 1
            else:
                histogram['red'][r] = 1
            if g in histogram['green']:
                histogram['green'][g] += 1
            else:
                histogram['green'][g] = 1
            if b in histogram['blue']:
                histogram['blue'][b] += 1
            else:
                histogram['blue'][b] = 1
    return histogram

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

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

OK

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

Хризантема обнови решението на 13.03.2016 23:09 (преди почти 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
def transpose(matrix):
    res = [list(i) for i in zip(*matrix)]
    return res


def rotate_left(image):
    transposed_image = transpose(image)
    new_image = transposed_image[::-1]
    return new_image


def rotate_right(image):
    reversed_image = image[::-1]
    new_image = transpose(reversed_image)
    return new_image


def invert_pixel(pixel):
    return (255 - pixel[0], 255 - pixel[1], 255 - pixel[2])


def invert_row(row):
    return list(map(invert_pixel, row))


def invert(image):
    return list(map(invert_row, image))


def lighten_pixel(pixel, number):
    red = pixel[0] + number * (255 - pixel[0])
    green = pixel[1] + number * (255 - pixel[1])
    blue = pixel[2] + number * (255 - pixel[2])
    return (int(red), int(green), int(blue))


def lighten(image, number):
    new_image = []
    for row in image:
        current_row = list(map(lambda x: lighten_pixel(x, number), row))
        new_image.append(current_row)
    return new_image


def darken_pixel(pixel, number):
    red = pixel[0] - number * pixel[0]
    green = pixel[1] - number * pixel[1]
    blue = pixel[2] - number * pixel[2]
    return (int(red), int(green), int(blue))


def darken(image, number):
    new_image = []
    for row in image:
        current_row = list(map(lambda x: darken_pixel(x, number), row))
        new_image.append(current_row)
    return new_image


def create_histogram(image):
    histogram = {'red': {}, 'green': {}, 'blue': {}}
    for row in image:
        for (r, g, b) in row:
            if r in histogram['red']:
                histogram['red'][r] += 1
            else:
                histogram['red'][r] = 1
            if g in histogram['green']:
                histogram['green'][g] += 1
            else:
                histogram['green'][g] = 1
            if b in histogram['blue']:
                histogram['blue'][b] += 1
            else:
                histogram['blue'][b] = 1
    return histogram