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


def transpose(picture):
    return [[row[i] for row in picture] for i in range(len(picture[0]))]


def rotate_left(picture):
    for row in picture:
        row.reverse()

    return transpose(picture)


def rotate_right(picture):
    transposed = transpose(picture)

    for row in transposed:
        row.reverse()

    return transposed


def invert_pixel(pixel):
    r = pixel[0]
    g = pixel[1]
    b = pixel[2]

    new_pixel = (255 - r, 255 - g, 255 - b)
    return new_pixel


def invert(picture):
    reversed_picture = []

    for row in picture:
        reversed_picture.append(list(map(invert_pixel, row)))

    return reversed_picture


def lighten_pixel(pixel, coef):
    r = pixel[0]
    g = pixel[1]
    b = pixel[2]

    new_r = int(r + coef * (255 - r))
    new_g = int(g + coef * (255 - g))
    new_b = int(b + coef * (255 - b))

    new_pixel = (new_r, new_g, new_b)
    return new_pixel


def lighten(picture, coef):
    lightened_picture = [[lighten_pixel(pixel, coef)
                          for pixel in row]for row in picture]

    return lightened_picture


def darken_pixel(pixel, coef):
    r = pixel[0]
    g = pixel[1]
    b = pixel[2]

    new_r = int(r - coef * (r - 0))
    new_g = int(g - coef * (g - 0))
    new_b = int(b - coef * (b - 0))

    new_pixel = (new_r, new_g, new_b)
    return new_pixel


def darken(picture, coef):
    darkened_picture = [[darken_pixel(pixel, coef)
                         for pixel in row]for row in picture]

    return darkened_picture


def update_pixel_info(color, values, pixel):
    pixel_color = 0

    if color is 'red':
        pixel_color = pixel[0]
    elif color is 'green':
        pixel_color = pixel[1]
    else:
        pixel_color = pixel[2]

    values[pixel_color] += 1


def create_histogram(picture):
    histogram = {'red': defaultdict(int),
                 'green': defaultdict(int),
                 'blue': defaultdict(int)
                 }

    for row in picture:
        for pixel in row:
            update_pixel_info('red', histogram['red'], pixel)
            update_pixel_info('green', histogram['green'], pixel)
            update_pixel_info('blue', histogram['blue'], pixel)

    return histogram

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.094s

OK

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

Георги обнови решението на 10.03.2016 17:01 (преди почти 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
from collections import defaultdict


def transpose(picture):
    return [[row[i] for row in picture] for i in range(len(picture[0]))]


def rotate_left(picture):
    for row in picture:
        row.reverse()

    return transpose(picture)


def rotate_right(picture):
    transposed = transpose(picture)

    for row in transposed:
        row.reverse()

    return transposed


def invert_pixel(pixel):
    r = pixel[0]
    g = pixel[1]
    b = pixel[2]

    new_pixel = (255 - r, 255 - g, 255 - b)
    return new_pixel


def invert(picture):
    reversed_pic = []

    for row in picture:
        reversed_pic.append(list(map(invert_pixel, row)))

    return reversed_pic


def lighten_pixel(pixel, coef):
    r = pixel[0]
    g = pixel[1]
    b = pixel[2]

    new_r = int(r + coef * (255 - r))
    new_g = int(g + coef * (255 - g))
    new_b = int(b + coef * (255 - b))

    new_pixel = (new_r, new_g, new_b)
    return new_pixel


def lighten(picture, coef):
    lightened_pic = [[lighten_pixel(pixel, coef)
                      for pixel in row]for row in picture]

    return lightened_pic


def darken_pixel(pixel, coef):
    r = pixel[0]
    g = pixel[1]
    b = pixel[2]

    new_r = int(r - coef * (r - 0))
    new_g = int(g - coef * (g - 0))
    new_b = int(b - coef * (b - 0))

    new_pixel = (new_r, new_g, new_b)
    return new_pixel


def darken(picture, coef):
    darkened_pic = [[darken_pixel(pixel, coef)
                     for pixel in row]for row in picture]

    return darkened_pic


def update_r_info(values, pixel):
    r = pixel[0]
    values[r] += 1


def update_g_info(values, pixel):
    g = pixel[1]
    values[g] += 1


def update_b_info(values, pixel):
    b = pixel[2]
    values[b] += 1


def create_histogram(picture):
    histogram = {'red': defaultdict(int),
                 'green': defaultdict(int),
                 'blue': defaultdict(int)
                 }

    for row in picture:
        for pixel in row:
            update_r_info(histogram['red'], pixel)
            update_g_info(histogram['green'], pixel)
            update_b_info(histogram['blue'], pixel)

    return histogram
  • Имаш ли нужда наистина от трите допълнителни функции на create_histogram или можеш да си преструктурираш кода, за да го избегнеш
  • Имената с _pic не са много добри, или напиши цялата дума, или измисли друго име

Като цяло доста изчистено и подредено решение :)

Георги обнови решението на 11.03.2016 23: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
102
103
104
105
106
107
from collections import defaultdict


def transpose(picture):
    return [[row[i] for row in picture] for i in range(len(picture[0]))]


def rotate_left(picture):
    for row in picture:
        row.reverse()

    return transpose(picture)


def rotate_right(picture):
    transposed = transpose(picture)

    for row in transposed:
        row.reverse()

    return transposed


def invert_pixel(pixel):
    r = pixel[0]
    g = pixel[1]
    b = pixel[2]

    new_pixel = (255 - r, 255 - g, 255 - b)
    return new_pixel


def invert(picture):
    reversed_picture = []

    for row in picture:
        reversed_picture.append(list(map(invert_pixel, row)))

    return reversed_picture


def lighten_pixel(pixel, coef):
    r = pixel[0]
    g = pixel[1]
    b = pixel[2]

    new_r = int(r + coef * (255 - r))
    new_g = int(g + coef * (255 - g))
    new_b = int(b + coef * (255 - b))

    new_pixel = (new_r, new_g, new_b)
    return new_pixel


def lighten(picture, coef):
    lightened_picture = [[lighten_pixel(pixel, coef)
                          for pixel in row]for row in picture]

    return lightened_picture


def darken_pixel(pixel, coef):
    r = pixel[0]
    g = pixel[1]
    b = pixel[2]

    new_r = int(r - coef * (r - 0))
    new_g = int(g - coef * (g - 0))
    new_b = int(b - coef * (b - 0))

    new_pixel = (new_r, new_g, new_b)
    return new_pixel


def darken(picture, coef):
    darkened_picture = [[darken_pixel(pixel, coef)
                         for pixel in row]for row in picture]

    return darkened_picture


def update_pixel_info(color, values, pixel):
    pixel_color = 0

    if color is 'red':
        pixel_color = pixel[0]
    elif color is 'green':
        pixel_color = pixel[1]
    else:
        pixel_color = pixel[2]

    values[pixel_color] += 1


def create_histogram(picture):
    histogram = {'red': defaultdict(int),
                 'green': defaultdict(int),
                 'blue': defaultdict(int)
                 }

    for row in picture:
        for pixel in row:
            update_pixel_info('red', histogram['red'], pixel)
            update_pixel_info('green', histogram['green'], pixel)
            update_pixel_info('blue', histogram['blue'], pixel)

    return histogram