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


def rotate_left(image):
    return [list(x) for x in zip(*[list(reversed(x)) for x in image])]


def inverting(row):
    new_image = []
    for pix in row:
        new_image.append((255 - pix[0], 255 - pix[1], 255 - pix[2]))
    return new_image


def invert(image):
    return [x for x in (map(inverting, image))]


def darken(image, coeff):
    new_image = []
    for row in image:
        new_image.append((list(map(lambda x: (int(x[0] - coeff*x[0]),
                                              int(x[1] - coeff*x[1]),
                                              int(x[2] - coeff*x[2])), row))))
    return new_image


def lighten(image, coeff):
    new_image = []
    for row in image:
        new_image.append((list(map(lambda x: (int(x[0] + coeff*(255 - x[0])),
                                              int(x[1] + coeff*(255 - x[1])),
                                              int(x[2] + coeff*(255 - x[2]))), row))))
    return new_image


def create_histogram(image):
    histogram = {'red': {x: 0 for x in range(0, 256)},
                 'green': {x: 0 for x in range(0, 256)},
                 'blue': {x: 0 for x in range(0, 256)}}
    for row in image:
        for pixel in row:
            histogram['red'][pixel[0]] += 1
            histogram['green'][pixel[1]] += 1
            histogram['blue'][pixel[2]] += 1
    for colour in histogram:
        histogram[colour] = {key: value for key, value
                             in histogram[colour].items()
                             if value is not 0}
    return histogram

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.126s

OK

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

Александра обнови решението на 12.03.2016 15:26 (преди почти 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
def rotate_right(image):
    return [list(x)[::-1] for x in zip(*image)]


def rotate_left(image):
    new_image = [list(reversed(x)) for x in image]
    return [list(x) for x in zip(*new_image)]


def inverting(row):
    temp = []
    for pix in row:
        temp.append((255 - pix[0], 255 - pix[1], 255 - pix[2]))
    return temp


def invert(image):
    return [x for x in (map(inverting, image))]


def darken(image, coeff):
    new_image = []
    for row in image:
        temp = (list(map(lambda x: (int(x[0] - coeff*x[0]),
                                    int(x[1] - coeff*x[1]),
                                    int(x[2] - coeff*x[2])), row)))
        new_image.append(temp)
    return new_image


def lighten(image, coeff):
    new_image = []
    for row in image:
        temp = (list(map(lambda x: (int(x[0] + coeff*(255 - x[0])),
                                    int(x[1] + coeff*(255 - x[1])),
                                    int(x[2] + coeff*(255 - x[2]))), row)))
        new_image.append(temp)
    return new_image


def create_histogram(image):
    histogram = {'red': {}, 'green': {}, 'blue': {}}
    r, g, b = [], [], []
    temp = list(zip(*[item for sublist in image for item in sublist]))

    for x in range(0, len(temp[0])):
        r.append((temp[0][x], 0))
        g.append((temp[1][x], 0))
        b.append((temp[2][x], 0))

    histogram = {'red': dict(r), 'green': dict(g), 'blue': dict(b)}
    for row in image:
        for pixel in row:
            histogram['red'][pixel[0]] += 1
            histogram['green'][pixel[1]] += 1
            histogram['blue'][pixel[2]] += 1
    return histogram
  • Оправи си подравняването (използваме 4 интервала, а не два)
  • Можеш да дадеш по-описателни имена, temp нищо не значи
  • Дали не можеш да измислиш по-подходяща структура за create_histogram, която да има стойност по подразбиране?

Александра обнови решението на 14.03.2016 14:18 (преди почти 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
def rotate_right(image):
    return [list(x)[::-1] for x in zip(*image)]


def rotate_left(image):
    return [list(x) for x in zip(*[list(reversed(x)) for x in image])]


def inverting(row):
    new_image = []
    for pix in row:
        new_image.append((255 - pix[0], 255 - pix[1], 255 - pix[2]))
    return new_image


def invert(image):
    return [x for x in (map(inverting, image))]


def darken(image, coeff):
    new_image = []
    for row in image:
        new_image.append((list(map(lambda x: (int(x[0] - coeff*x[0]),
                                              int(x[1] - coeff*x[1]),
                                              int(x[2] - coeff*x[2])), row))))
    return new_image


def lighten(image, coeff):
    new_image = []
    for row in image:
        new_image.append((list(map(lambda x: (int(x[0] + coeff*(255 - x[0])),
                                              int(x[1] + coeff*(255 - x[1])),
                                              int(x[2] + coeff*(255 - x[2]))), row))))
    return new_image


def create_histogram(image):
    histogram = {'red': {x: 0 for x in range(0, 256)},
                 'green': {x: 0 for x in range(0, 256)},
                 'blue': {x: 0 for x in range(0, 256)}}
    for row in image:
        for pixel in row:
            histogram['red'][pixel[0]] += 1
            histogram['green'][pixel[1]] += 1
            histogram['blue'][pixel[2]] += 1
    for colour in histogram:
        histogram[colour] = {key: value for key, value
                             in histogram[colour].items()
                             if value is not 0}
    return histogram