timeit

Програмиране с Python

Курс във Факултета по Математика и Информатика към СУ

Решение на Работа с картинки от Емил Илиев

Обратно към всички решения

Към профила на Емил Илиев

Резултати

  • 8 точки от тестове
  • 0 бонус точки
  • 8 точки общо
  • 10 успешни тест(а)
  • 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
def rotate_left(original_image):
    left_rotated_image = []

    for row in range(len(original_image)):
        left_rotated_image.append([])
        current_col = len(original_image) - row - 1
        for col in range(len(original_image[row])):
            pixel = original_image[col][current_col]
            left_rotated_image[row].append(pixel)

    return left_rotated_image


def rotate_right(original_image):
    right_rotated_image = []

    for row in range(len(original_image)):
        right_rotated_image.append([])
        for col in range(len(original_image[row])):
            pixel = original_image[col][row]
            right_rotated_image[row].insert(0, pixel)
    return right_rotated_image


def invert(original_image):
    inverted_image = []

    for row in range(len(original_image)):
        inverted_row = []

        for col in range(len(original_image[row])):
            red, green, blue = original_image[row][col]
            inverted_row.append((255 - red, 255 - green, 255 - blue))

        inverted_image.append(inverted_row)

    return inverted_image


def lighten_pixel(pixel, coef):
    brighter_pixel = []
    for x in range(len(pixel)):
        cur_color = pixel[x] + int(coef * (255 - pixel[x]))
        brighter_pixel.append(cur_color)

    return tuple(brighter_pixel)


def lighten(original_image, coef):
    lightened_image = []

    for row in range(len(original_image)):
        lightened_row = []

        for col in range(len(original_image[row])):
            pixel = original_image[row][col]
            lightened_row.append(lighten_pixel(pixel, coef))

        lightened_image.append(lightened_row)

    return lightened_image


def darken_pixel(pixel, coef):
    dark_pixel = []
    for x in range(len(pixel)):
        cur_color = pixel[x] - int(coef * (pixel[x] - 0))
        dark_pixel.append(cur_color)
    return tuple(dark_pixel)


def darken(original_image, coef):
    darkened_image = []

    for row in range(len(original_image)):
        darkened_row = []

        for col in range(len(original_image[row])):
            pixel = original_image[row][col]
            darkened_row.append(darken_pixel(pixel, coef))

        darkened_image.append(darkened_row)

    return darkened_image


def create_histogram(original_image):
    rgb = ['red', 'green', 'blue']
    histogram = {rgb[i]: {} for i in range(len(rgb))}

    for row in range(len(original_image)):
        for col in range(len(original_image[row])):
            pixel = original_image[row][col]

            for col in range(len(rgb)):
                if pixel[col] in histogram[rgb[col]]:
                    histogram[rgb[col]][pixel[col]] += 1
                else:
                    histogram[rgb[col]][pixel[col]] = 1

    return histogram

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

.........E.E
======================================================================
ERROR: test_rotate_left_rectangle (test.TestImages)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
IndexError: list index out of range

======================================================================
ERROR: test_rotate_right_rectangle (test.TestImages)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
IndexError: list index out of range

----------------------------------------------------------------------
Ran 12 tests in 0.085s

FAILED (errors=2)

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

Емил обнови решението на 13.03.2016 11:54 (преди почти 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
def rotate_left(original_image):
    left_rotated_image = []

    for row in range(len(original_image)):
        left_rotated_image.append([])
        current_col = len(original_image) - row - 1
        for col in range(len(original_image[row])):
            pixel = original_image[col][current_col]
            left_rotated_image[row].append(pixel)

    return left_rotated_image


def rotate_right(original_image):
    right_rotated_image = []

    for row in range(len(original_image)):
        right_rotated_image.append([])
        for col in range(len(original_image[row])):
            pixel = original_image[col][row]
            right_rotated_image[row].insert(0, pixel)
    return right_rotated_image


def invert(original_image):
    inverted_image = []

    for row in range(len(original_image)):
        inverted_row = []

        for col in range(len(original_image[row])):
            red, green, blue = original_image[row][col]
            inverted_row.append((255 - red, 255 - green, 255 - blue))

        inverted_image.append(inverted_row)

    return inverted_image


def lighten_pixel(pixel, coef):
    brighter_pixel = []
    for x in range(len(pixel)):
        cur_color = pixel[x] + int(coef * (255 - pixel[x]))
        brighter_pixel.append(cur_color)

    return tuple(brighter_pixel)


def lighten(original_image, coef):
    lightened_image = []

    for row in range(len(original_image)):
        lightened_row = []

        for col in range(len(original_image[row])):
            pixel = original_image[row][col]
            lightened_row.append(lighten_pixel(pixel, coef))

        lightened_image.append(lightened_row)

    return lightened_image


def darken_pixel(pixel, coef):
    dark_pixel = []
    for x in range(len(pixel)):
        cur_color = pixel[x] - int(coef * (pixel[x] - 0))
        dark_pixel.append(cur_color)
    return tuple(dark_pixel)


def darken(original_image, coef):
    darkened_image = []

    for row in range(len(original_image)):
        darkened_row = []

        for col in range(len(original_image[row])):
            pixel = original_image[row][col]
            darkened_row.append(darken_pixel(pixel, coef))

        darkened_image.append(darkened_row)

    return darkened_image


def create_histogram(original_image):
    rgb = ['red', 'green', 'blue']
    histogram = {rgb[i]: {} for i in range(len(rgb))}

    for row in range(len(original_image)):
        for col in range(len(original_image[row])):
            pixel = original_image[row][col]

            for col in range(len(rgb)):
                if pixel[col] in histogram[rgb[col]]:
                    histogram[rgb[col]][pixel[col]] += 1
                else:
                    histogram[rgb[col]][pixel[col]] = 1

    return histogram