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
def rotate_left(image):
    return(list(zip(*image)))[::-1]


def rotate_right(image):
    return(list(zip(*image[::-1])))


def invert(image):
    tmp = image[:]
    for i in range(len(image)):
        for j in range(len(image[0])):
            pixel = image[i][j]
            pixel2 = (255 - pixel[0], 255 - pixel[1], 255 - pixel[2])
        tmp[i][j] = pixel2
    return tmp


def lighten(image, percent):
    tmp = image[:]
    for i in range(len(image)):
        for j in range(len(image[0])):
            pixel = image[i][j]
            pixel2 = (
                        int(pixel[0] + percent*(255 - pixel[0])),
                        (int(pixel[1] + percent*(255 - pixel[1]))),
                        (int(pixel[2] + percent*(255 - pixel[2]))))
            tmp[i][j] = pixel2
    return tmp


def darken(image, percent):
    tmp = image[:]
    for i in range(len(image)):
        for j in range(len(image[0])):
            pixel = image[i][j]
            pixel2 = (
                        int(pixel[0] - percent*(pixel[0]-0)),
                        int(pixel[1] - percent*(pixel[1]-0)),
                        int(pixel[2] - percent*(pixel[2]-0)))
            tmp[i][j] = pixel2
    return tmp


def create_histogram(image):
    colors = {'red': {}, 'green': {}, 'blue': {}}
    red = colors['red']
    green = colors['green']
    blue = colors['blue']
    for i in range(len(image)):
        for j in range(len(image[0])):
            pixel = image[i][j]
            if pixel[0] in red:
                red[pixel[0]] = red[pixel[0]] + 1
            else:
                red[pixel[0]] = 1
            if pixel[1] in green:
                green[pixel[1]] = green[pixel[1]] + 1
            else:
                green[pixel[1]] = 1
            if pixel[2] in blue:
                blue[pixel[2]] = blue[pixel[2]] + 1
            else:
                blue[pixel[2]] = 1
    colors['red'] = red
    colors['green'] = green
    colors['blue'] = blue
    return colors

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

....FF......
======================================================================
FAIL: test_invert (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: (255, 255, 0) != (0, 0, 255)

First differing element 0:
255
0

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

======================================================================
FAIL: test_invert_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: (155, 255, 0) != (100, 0, 255)

First differing element 0:
155
100

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

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

FAILED (failures=2)

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

Антонио обнови решението на 14.03.2016 00:06 (преди почти 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):
    return(list(zip(*image)))[::-1]


def rotate_right(image):
    return(list(zip(*image[::-1])))


def invert(image):
    tmp = image[:]
    for i in range(len(image)):
        for j in range(len(image[0])):
            pixel = image[i][j]
            pixel2 = (255 - pixel[0], 255 - pixel[1], 255 - pixel[2])
        tmp[i][j] = pixel2
    return tmp


def lighten(image, percent):
    tmp = image[:]
    for i in range(len(image)):
        for j in range(len(image[0])):
            pixel = image[i][j]
            pixel2 = (
                        int(pixel[0] + percent*(255 - pixel[0])),
                        (int(pixel[1] + percent*(255 - pixel[1]))),
                        (int(pixel[2] + percent*(255 - pixel[2]))))
            tmp[i][j] = pixel2
    return tmp


def darken(image, percent):
    tmp = image[:]
    for i in range(len(image)):
        for j in range(len(image[0])):
            pixel = image[i][j]
            pixel2 = (
                        int(pixel[0] - percent*(pixel[0]-0)),
                        int(pixel[1] - percent*(pixel[1]-0)),
                        int(pixel[2] - percent*(pixel[2]-0)))
            tmp[i][j] = pixel2
    return tmp


def create_histogram(image):
    colors = {'red': {}, 'green': {}, 'blue': {}}
    red = colors['red']
    green = colors['green']
    blue = colors['blue']
    for i in range(len(image)):
        for j in range(len(image[0])):
            pixel = image[i][j]
            if pixel[0] in red:
                red[pixel[0]] = red[pixel[0]] + 1
            else:
                red[pixel[0]] = 1
            if pixel[1] in green:
                green[pixel[1]] = green[pixel[1]] + 1
            else:
                green[pixel[1]] = 1
            if pixel[2] in blue:
                blue[pixel[2]] = blue[pixel[2]] + 1
            else:
                blue[pixel[2]] = 1
    colors['red'] = red
    colors['green'] = green
    colors['blue'] = blue
    return colors