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
MAX_COLOR_VALUE = 255


def rotate_left(image):
    rotated_image = []
    width = len(image[0])
    heigth = len(image)
    for row in range(width):
        curr_row = [image[pixel][width - row - 1] for pixel in range(heigth)]
        rotated_image.append(tuple(curr_row))
    return rotated_image


def rotate_right(image):
    rotated_image = []
    width = len(image[0])
    heigth = len(image)
    for row in range(width):
        curr_row = [image[heigth - pixel - 1][row] for pixel in range(heigth)]
        rotated_image.append(tuple(curr_row))
    return rotated_image


def invert(image):
    new_image = []
    for row in image:
        new_row = []
        for pixel in row:
            inverted_pixel = [MAX_COLOR_VALUE - color for color in pixel]
            new_row.append(tuple(inverted_pixel))
        new_image.append(new_row)
    return new_image


def calc_lighten(color, coefficient):
    return int(color + coefficient * (MAX_COLOR_VALUE - color))


def lighten(image, coeff):
    new_image = []
    for row in image:
        new_row = []
        for pixel in row:
            lighten_pixel = [calc_lighten(color, coeff) for color in pixel]
            new_row.append(tuple(lighten_pixel))
        new_image.append(new_row)
    return new_image


def calc_darken(color, coefficient):
    return int(color - coefficient * color)


def darken(image, coeff):
    new_image = []
    for row in image:
        new_row = []
        for pixel in row:
            darken_pixel = [calc_darken(color, coeff) for color in pixel]
            new_row.append(tuple(darken_pixel))
        new_image.append(new_row)
    return new_image


def create_histogram(image):
    VALUE = 1
    histogram = [('red', {}), ('green', {}), ('blue', {})]
    for row in image:
        for pixel in row:
            for color in range(len(histogram)):
                color_values = histogram[color][VALUE]
                if pixel[color] in color_values:
                    color_values[pixel[color]] += 1
                else:
                    color_values[pixel[color]] = 1
                color += 1
    return dict(histogram)

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.091s

OK

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

Николай обнови решението на 13.03.2016 17: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
75
76
77
MAX_COLOR_VALUE = 255


def rotate_left(image):
    rotated_image = []
    width = len(image[0])
    heigth = len(image)
    for row in range(width):
        curr_row = [image[pixel][width - row - 1] for pixel in range(heigth)]
        rotated_image.append(tuple(curr_row))
    return rotated_image


def rotate_right(image):
    rotated_image = []
    width = len(image[0])
    heigth = len(image)
    for row in range(width):
        curr_row = [image[heigth - pixel - 1][row] for pixel in range(heigth)]
        rotated_image.append(tuple(curr_row))
    return rotated_image


def invert(image):
    new_image = []
    for row in image:
        new_row = []
        for pixel in row:
            inverted_pixel = [MAX_COLOR_VALUE - color for color in pixel]
            new_row.append(tuple(inverted_pixel))
        new_image.append(new_row)
    return new_image


def calc_lighten(color, coefficient):
    return int(color + coefficient * (MAX_COLOR_VALUE - color))


def lighten(image, coeff):
    new_image = []
    for row in image:
        new_row = []
        for pixel in row:
            lighten_pixel = [calc_lighten(color, coeff) for color in pixel]
            new_row.append(tuple(lighten_pixel))
        new_image.append(new_row)
    return new_image


def calc_darken(color, coefficient):
    return int(color - coefficient * color)


def darken(image, coeff):
    new_image = []
    for row in image:
        new_row = []
        for pixel in row:
            darken_pixel = [calc_darken(color, coeff) for color in pixel]
            new_row.append(tuple(darken_pixel))
        new_image.append(new_row)
    return new_image


def create_histogram(image):
    VALUE = 1
    histogram = [('red', {}), ('green', {}), ('blue', {})]
    for row in image:
        for pixel in row:
            for color in range(len(histogram)):
                color_values = histogram[color][VALUE]
                if pixel[color] in color_values:
                    color_values[pixel[color]] += 1
                else:
                    color_values[pixel[color]] = 1
                color += 1
    return dict(histogram)