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
def take_column_by_number(number, my_list):
    result = []
    for i in range(len(my_list)):
        result.append(my_list[i][number])
    return result


def rotate_left(image):
    new_image = []
    for i in range(len(image[0])):
        new_image.append(take_column_by_number(i, image))
    return new_image[::-1]


def rotate_right(image):
    new_image = []
    for i in range(len(image[0])):
        new_image.append(take_column_by_number(i, image))
    return new_image


def invert(image):
    new_image = []
    for row in image:
        new_row = []
        for pixel in row:
            new_pixel = (255 - pixel[0], 255 - pixel[1], 255-pixel[2])
            new_row.append(new_pixel)
        new_image.append(new_row)
    return new_image


def lighten(image, coef):
    new_image = []
    for row in image:
        new_row = []
        for pixel in row:
            new_pixel = (
                int(pixel[0] + coef * (255 - pixel[0])),
                int(pixel[1] + coef * (255 - pixel[1])),
                int(pixel[2] + coef * (255 - pixel[2])))
            new_row.append(new_pixel)
        new_image.append(new_row)
    return new_image


def darken(image, coef):
    new_image = []
    for row in image:
        new_row = []
        for pixel in row:
            new_pixel = (
                int(pixel[0] - coef*pixel[0]),
                int(pixel[1] - coef*pixel[1]),
                int(pixel[2] - coef*pixel[2]))
            new_row.append(new_pixel)
        new_image.append(new_row)
    return new_image


def create_histogram(image):
    hist = {'red': {}, 'green': {}, 'blue': {}}

    for color in range(0, 3):
        for row in image:
            for elem in row:
                if color == 0:
                    hist['red'][elem[0]] = hist['red'].get(elem[0], 0) + 1
                elif color == 1:
                    hist['green'][elem[1]] = hist['green'].get(elem[1], 0) + 1
                else:
                    hist['blue'][elem[2]] = hist['blue'].get(elem[2], 0) + 1

    return hist

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

..........FF
======================================================================
FAIL: test_rotate_right (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
AssertionError: Tuples differ: (0, 255, 0) != (0, 0, 255)

First differing element 1:
255
0

- (0, 255, 0)
+ (0, 0, 255)

======================================================================
FAIL: 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
AssertionError: Tuples differ: (0, 255, 1) != (100, 0, 255)

First differing element 0:
0
100

- (0, 255, 1)
+ (100, 0, 255)

----------------------------------------------------------------------
Ran 12 tests in 0.111s

FAILED (failures=2)

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

Симеон обнови решението на 14.03.2016 00:45 (преди почти 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 take_column_by_number(number, my_list):
    result = []
    for i in range(len(my_list)):
        result.append(my_list[i][number])
    return result


def rotate_left(image):
    new_image = []
    for i in range(len(image[0])):
        new_image.append(take_column_by_number(i, image))
    return new_image[::-1]


def rotate_right(image):
    new_image = []
    for i in range(len(image[0])):
        new_image.append(take_column_by_number(i, image))
    return new_image


def invert(image):
    new_image = []
    for row in image:
        new_row = []
        for pixel in row:
            new_pixel = (255 - pixel[0], 255 - pixel[1], 255-pixel[2])
            new_row.append(new_pixel)
        new_image.append(new_row)
    return new_image


def lighten(image, coef):
    new_image = []
    for row in image:
        new_row = []
        for pixel in row:
            new_pixel = (
                int(pixel[0] + coef * (255 - pixel[0])),
                int(pixel[1] + coef * (255 - pixel[1])),
                int(pixel[2] + coef * (255 - pixel[2])))
            new_row.append(new_pixel)
        new_image.append(new_row)
    return new_image


def darken(image, coef):
    new_image = []
    for row in image:
        new_row = []
        for pixel in row:
            new_pixel = (
                int(pixel[0] - coef*pixel[0]),
                int(pixel[1] - coef*pixel[1]),
                int(pixel[2] - coef*pixel[2]))
            new_row.append(new_pixel)
        new_image.append(new_row)
    return new_image


def create_histogram(image):
    hist = {'red': {}, 'green': {}, 'blue': {}}

    for color in range(0, 3):
        for row in image:
            for elem in row:
                if color == 0:
                    hist['red'][elem[0]] = hist['red'].get(elem[0], 0) + 1
                elif color == 1:
                    hist['green'][elem[1]] = hist['green'].get(elem[1], 0) + 1
                else:
                    hist['blue'][elem[2]] = hist['blue'].get(elem[2], 0) + 1

    return hist