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
import collections


def rotate_left(image):
    colN = len(image[0])
    res = [[row[i] for row in image] for i in range(colN - 1, -1, -1)]
    return res


def rotate_right(image):
    rows = len(image)
    colN = len(image[0])
    res = [[image[j][i] for j in range(rows - 1, -1, -1)] for i in range(colN)]
    return res


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


def invert(image):
    colN = len(image[0])
    res = [[invertHelp(pixel) for pixel in row] for row in image]
    return res


def darkenHelp(pixel, number):
    r = int((1-number) * pixel[0])
    g = int((1-number) * pixel[1])
    b = int((1-number) * pixel[2])
    return (r, g, b)


def darken(image, number):
    res = [[darkenHelp(pixel, number) for pixel in row] for row in image]
    return res


def lightenHelp(pixel, number):
    r = int(pixel[0] + number * (255 - pixel[0]))
    g = int(pixel[1] + number * (255 - pixel[1]))
    b = int(pixel[2] + number * (255 - pixel[2]))
    return (r, g, b)


def lighten(image, number):
    res = [[lightenHelp(pixel, number) for pixel in row] for row in image]
    return res


def create_histogram(image):
    redD = collections.defaultdict(int)
    greenD = collections.defaultdict(int)
    blueD = collections.defaultdict(int)
    for row in image:
        for pixel in row:
            redD[pixel[0]] += 1
            greenD[pixel[1]] += 1
            blueD[pixel[2]] += 1
    res = {'red': redD, 'green': greenD, 'blue': blueD}
    return res

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.112s

OK

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

Андрей обнови решението на 10.03.2016 01:40 (преди почти 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
102
103
104
105
106
107
108
109
110
def rotate_left(image):
    result = []
    new = []
    rows = len(image)
    columns = len(image[0])
    for i in range(columns-1, -1, -1):
        for j in range(0, rows):
            new.append(image[j][i])
        result.append(new)
        new = []
    return result


def rotate_right(image):
    result = []
    new = []
    rows = len(image)
    columns = len(image[0])
    for i in range(0, columns):
        for j in range(rows-1, -1, -1):
            new.append(image[j][i])
        result.append(new)
        new = []
    return result


def invert(image):
    rows = len(image)
    columns = len(image[0])
    result = []
    new = []
    for i in range(0, rows):
        for j in range(0, columns):
            r = 255 - image[i][j][0]
            g = 255 - image[i][j][1]
            b = 255 - image[i][j][2]
            new.append((r, g, b))
        result.append(new)
        new = []
    return result


def darken(image, number):
    rows = len(image)
    columns = len(image[0])
    result = []
    new = []
    for i in range(0, rows):
        for j in range(0, columns):
            r = int(image[i][j][0]-number*(image[i][j][0]-0))
            g = int(image[i][j][1]-number*(image[i][j][1]-0))
            b = int(image[i][j][2]-number*(image[i][j][2]-0))
            new.append((r, g, b))
        result.append(new)
        new = []
    return result


def lighten(image, number):
    rows = len(image)
    columns = len(image[0])
    result = []
    new = []
    for i in range(0, rows):
        for j in range(0, columns):
            r = int(image[i][j][0]+number*(255-image[i][j][0]))
            g = int(image[i][j][1]+number*(255-image[i][j][1]))
            b = int(image[i][j][2]+number*(255-image[i][j][2]))
            new.append((r, g, b))
        result.append(new)
        new = []
    return result


def member(list, number):
    n = len(list)
    for i in range(0, n):
        if number == list[i][0]:
            return i
    return -1


def create_histogram(image):
    red = []
    green = []
    blue = []
    rows = len(image)
    columns = len(image[0])
    for i in range(0, rows):
        for j in range(0, columns):
            indexRed = member(red, image[i][j][0])
            if indexRed != -1:
                red[indexRed] = (image[i][j][0], red[indexRed][1] + 1)
            else:
                red.append((image[i][j][0], 1))
            indexGreen = member(green, image[i][j][1])
            if indexGreen != -1:
                green[indexGreen] = (image[i][j][1], green[indexGreen][1] + 1)
            else:
                green.append((image[i][j][1], 1))
            indexBlue = member(blue, image[i][j][2])
            if indexBlue != -1:
                blue[indexBlue] = (image[i][j][2], blue[indexBlue][1] + 1)
            else:
                blue.append((image[i][j][2], 1))
    redD = dict(red)
    greenD = dict(green)
    blueD = dict(blue)
    result = {'red': redD, 'green': greenD, 'blue': blueD}
    return result

Андрей обнови решението на 11.03.2016 18:17 (преди почти 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
import collections


def rotate_left(image):
    colN = len(image[0])
    res = [[row[i] for row in image] for i in range(colN - 1, -1, -1)]
    return res


def rotate_right(image):
    rows = len(image)
    colN = len(image[0])
    res = [[image[j][i] for j in range(rows - 1, -1, -1)] for i in range(colN)]
    return res


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


def invert(image):
    colN = len(image[0])
    res = [[invertHelp(pixel) for pixel in row] for row in image]
    return res


def darkenHelp(pixel, number):
    r = int((1-number) * pixel[0])
    g = int((1-number) * pixel[1])
    b = int((1-number) * pixel[2])
    return (r, g, b)


def darken(image, number):
    res = [[darkenHelp(pixel, number) for pixel in row] for row in image]
    return res


def lightenHelp(pixel, number):
    r = int(pixel[0] + number * (255 - pixel[0]))
    g = int(pixel[1] + number * (255 - pixel[1]))
    b = int(pixel[2] + number * (255 - pixel[2]))
    return (r, g, b)


def lighten(image, number):
    res = [[lightenHelp(pixel, number) for pixel in row] for row in image]
    return res


def create_histogram(image):
    redD = collections.defaultdict(int)
    greenD = collections.defaultdict(int)
    blueD = collections.defaultdict(int)
    for row in image:
        for pixel in row:
            redD[pixel[0]] += 1
            greenD[pixel[1]] += 1
            blueD[pixel[2]] += 1
    res = {'red': redD, 'green': greenD, 'blue': blueD}
    return res