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


def transpose(image):
    transposed = [[image[col][row] for col in range(len(image))]
                  for row in range(len(image[0]))]
    return transposed


def rotate_right(image):
    rotated_image = transpose(image)
    rotated_image = [rotated_image[row][::-1]
                     for row in range(len(rotated_image))]
    return rotated_image


def rotate_left(image):
    rotated_image = [image[col][::-1] for col in range(len(image))]
    rotated_image = transpose(rotated_image)
    return rotated_image


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


def invert(image):
    inverted_image = [[invert_pixel(image[row][col])
                       for col in range(len(image[0]))]
                      for row in range(len(image))]
    return inverted_image


def lighten_pixel(pixel, coef):
    pixel = (pixel[0] + int(coef * (255 - pixel[0])), pixel[1] +
             int(coef * (255 - pixel[1])), pixel[2] +
             int(coef * (255 - pixel[2])))
    return pixel


def lighten(image, coef):
    light_image = [[lighten_pixel(image[row][col], coef)
                    for col in range(len(image[0]))]
                   for row in range(len(image))]
    return light_image


def darken_pixel(pixel, coef):
    pixel = (pixel[0] - int(coef * (pixel[0] - 0)), pixel[1] -
             int(coef * (pixel[1] - 0)), pixel[2] - int(coef * (pixel[2] - 0)))
    return pixel


def darken(image, coef):
    dark_image = [[darken_pixel(image[row][col], coef)
                   for col in range(len(image[0]))]
                  for row in range(len(image))]
    return dark_image


def create_histogram(image):
    histogram = {'red': defaultdict(int), 'green': defaultdict(int),
                 'blue': defaultdict(int)}
    for row in range(len(image)):
        for col in range(len(image[0])):
            pixel = image[row][col]
            histogram['red'][pixel[0]] += 1
            histogram['green'][pixel[1]] += 1
            histogram['blue'][pixel[2]] += 1
    return histogram

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.085s

OK

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

Борис обнови решението на 10.03.2016 00:41 (преди почти 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
def transpose(image):
    transposed = [[image[col][row]
                   for col in range(len(image))] for row in range(len(image[0]))]
    return transposed


def rotate_right(image):
    rotated_image = transpose(image)
    rotated_image = [rotated_image[row][::-1]
                     for row in range(len(rotated_image))]
    return rotated_image


def rotate_left(image):
    rotated_image = [image[col][::-1] for col in range(len(image))]
    rotated_image = transpose(rotated_image)
    return rotated_image


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


def invert(image):
    inverted_image = [[invert_pixel(image[row][col])
                       for col in range(len(image[0]))] for row in range(len(image))]
    return inverted_image


def lighten_pixel(pixel, coef):
    pixel = (pixel[0] + int(coef * (255 - pixel[0])), pixel[1] +
             int(coef * (255 - pixel[1])), pixel[2] + int(coef * (255 - pixel[2])))
    return pixel


def lighten(image, coef):
    light_image = [[lighten_pixel(image[row][col], coef)
                    for col in range(len(image[0]))] for row in range(len(image))]
    return light_image


def darken_pixel(pixel, coef):
    pixel = (pixel[0] - int(coef * (pixel[0] - 0)), pixel[1] -
             int(coef * (pixel[1] - 0)), pixel[2] - int(coef * (pixel[2] - 0)))
    return pixel


def darken(image, coef):
    dark_image = [[darken_pixel(image[row][col], coef)
                   for col in range(len(image[0]))] for row in range(len(image))]
    return dark_image


def add_key(key, dict_):
    if key in dict_:
        dict_[key] += 1
    else:
        dict_[key] = 1


def create_histogram(image):
    histogram = {'red': {}, 'green': {}, 'blue': {}}
    for row in range(len(image)):
        for col in range(len(image[0])):
            pixel = (image[row][col])
            add_key(pixel[0], histogram['red'])
            add_key(pixel[1], histogram['green'])
            add_key(pixel[2], histogram['blue'])
    return histogram

Борис обнови решението на 12.03.2016 14:44 (преди почти 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
from collections import defaultdict


def transpose(image):
    transposed = [[image[col][row] for col in range(len(image))]
                  for row in range(len(image[0]))]
    return transposed


def rotate_right(image):
    rotated_image = transpose(image)
    rotated_image = [rotated_image[row][::-1]
                     for row in range(len(rotated_image))]
    return rotated_image


def rotate_left(image):
    rotated_image = [image[col][::-1] for col in range(len(image))]
    rotated_image = transpose(rotated_image)
    return rotated_image


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


def invert(image):
    inverted_image = [[invert_pixel(image[row][col])
                       for col in range(len(image[0]))]
                      for row in range(len(image))]
    return inverted_image


def lighten_pixel(pixel, coef):
    pixel = (pixel[0] + int(coef * (255 - pixel[0])), pixel[1] +
             int(coef * (255 - pixel[1])), pixel[2] +
             int(coef * (255 - pixel[2])))
    return pixel


def lighten(image, coef):
    light_image = [[lighten_pixel(image[row][col], coef)
                    for col in range(len(image[0]))]
                   for row in range(len(image))]
    return light_image


def darken_pixel(pixel, coef):
    pixel = (pixel[0] - int(coef * (pixel[0] - 0)), pixel[1] -
             int(coef * (pixel[1] - 0)), pixel[2] - int(coef * (pixel[2] - 0)))
    return pixel


def darken(image, coef):
    dark_image = [[darken_pixel(image[row][col], coef)
                   for col in range(len(image[0]))]
                  for row in range(len(image))]
    return dark_image


def create_histogram(image):
    histogram = {'red': defaultdict(int), 'green': defaultdict(int),
                 'blue': defaultdict(int)}
    for row in range(len(image)):
        for col in range(len(image[0])):
            pixel = image[row][col]
            histogram['red'][pixel[0]] += 1
            histogram['green'][pixel[1]] += 1
            histogram['blue'][pixel[2]] += 1
    return histogram