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


def dimensions_of_image(image):
    return [len(image), len(image[0])]


def rotate_left(image):
    height, width = dimensions_of_image(image)
    return [[image[row][col] for row in range(height)]
            for col in range(width - 1, -1, -1)]


def rotate_right(image):
    height, width = dimensions_of_image(image)
    return [[image[row][col] for row in range(height - 1, -1, -1)]
            for col in range(width)]


def invert_pixel(pixel):
    return tuple(255 - pixel[i] for i in range(3))


def invert(image):
    height, width = dimensions_of_image(image)
    return [[invert_pixel(image[row][col]) for col in range(width)]
            for row in range(height)]


def lighten_pixel(pixel, coef):
    return tuple(pixel[i] + int(coef * (255 - pixel[i]))
                 for i in range(3))


def lighten(image, coef):
    height, width = dimensions_of_image(image)
    return [[lighten_pixel(image[row][col], coef) for col in range(width)]
            for row in range(height)]


def darken_pixel(pixel, coef):
    return tuple(pixel[i] - int(coef * pixel[i]) for i in range(3))


def darken(image, coef):
    height, width = dimensions_of_image(image)
    return [[darken_pixel(image[row][col], coef) for col in range(width)]
            for row in range(height)]


def create_histogram(image):
    flattened_image = [item for sublist in image for item in sublist]
    image_pixels = len(flattened_image)

    rgb_values = [[flattened_image[pixel][i] for pixel in range(image_pixels)]
                  for i in range(3)]
    red_values, green_values, blue_values = rgb_values

    red_counter = collections.Counter(red_values)
    green_counter = collections.Counter(green_values)
    blue_counter = collections.Counter(blue_values)

    return {'red': red_counter, 'green': green_counter, 'blue': blue_counter}

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

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

OK

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

Алекс обнови решението на 08.03.2016 23:21 (преди почти 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
import math
import collections


def dimensions_of_image(image):
    return [len(image), len(image[0])]


def rotate_left(image):
    height, width = dimensions_of_image(image)
    return [[image[row][col] for row in range(height - 1, -1)]
            for col in range(width - 1, -1)]


def rotate_right(image):
    height, width = dimensions_of_image(image)
    return [[image[row][col] for row in range(height - 1, -1)]
            for col in range(width)]


def invert_pixel(pixel):
    return tuple(255 - pixel[i] for i in range(3))


def invert(image):
    height, width = dimensions_of_image(image)
    return [[invert_pixel(image[row][col]) for col in range(width)]
            for row in range(height)]


def lighten_pixel(pixel, coef):
    return tuple(pixel[i] + math.floor(coef * (255 - pixel[i]))
                 for i in range(3))


def lighten(image, coef):
    height, width = dimensions_of_image(image)
    return [[lighten_pixel(image[row][col], coef) for col in range(width)]
            for row in range(height)]


def darken_pixel(pixel, coef):
    return tuple(pixel[i] - math.floor(coef * pixel[i]) for i in range(3))


def darken(image, coef):
    height, width = dimensions_of_image(image)
    return [[darken_pixel(image[row][col], coef) for col in range(width)]
            for row in range(height)]


def create_histogram(image):
    flattened_image = [item for sublist in image for item in sublist]
    image_pixels = len(flattened_image)

    rgb_values = [[flattened_image[i][j] for i in range(image_pixels)]
                  for j in range(3)]
    red_values, green_values, blue_values = rgb_values

    red_counter = collections.Counter(red_values)
    green_counter = collections.Counter(green_values)
    blue_counter = collections.Counter(blue_values)

    return {'red': red_counter, 'green': green_counter, 'blue': blue_counter}

Алекс обнови решението на 09.03.2016 23:38 (преди почти 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
import collections


def dimensions_of_image(image):
    return [len(image), len(image[0])]


def rotate_left(image):
    height, width = dimensions_of_image(image)
    return [[image[row][col] for row in range(height)]
            for col in range(width - 1, -1, -1)]


def rotate_right(image):
    height, width = dimensions_of_image(image)
    return [[image[row][col] for row in range(height - 1, -1, -1)]
            for col in range(width)]


def invert_pixel(pixel):
    return tuple(255 - pixel[i] for i in range(3))


def invert(image):
    height, width = dimensions_of_image(image)
    return [[invert_pixel(image[row][col]) for col in range(width)]
            for row in range(height)]


def lighten_pixel(pixel, coef):
    return tuple(pixel[i] + int(coef * (255 - pixel[i]))
                 for i in range(3))


def lighten(image, coef):
    height, width = dimensions_of_image(image)
    return [[lighten_pixel(image[row][col], coef) for col in range(width)]
            for row in range(height)]


def darken_pixel(pixel, coef):
    return tuple(pixel[i] - int(coef * pixel[i]) for i in range(3))


def darken(image, coef):
    height, width = dimensions_of_image(image)
    return [[darken_pixel(image[row][col], coef) for col in range(width)]
            for row in range(height)]


def create_histogram(image):
    flattened_image = [item for sublist in image for item in sublist]
    image_pixels = len(flattened_image)

    rgb_values = [[flattened_image[pixel][i] for pixel in range(image_pixels)]
                  for i in range(3)]
    red_values, green_values, blue_values = rgb_values

    red_counter = collections.Counter(red_values)
    green_counter = collections.Counter(green_values)
    blue_counter = collections.Counter(blue_values)

    return {'red': red_counter, 'green': green_counter, 'blue': blue_counter}