Решение на Работа с картинки от Алекс Николов

Обратно към всички решения

Към профила на Алекс Николов

Резултати

  • 10 точки от тестове
  • 0 бонус точки
  • 10 точки общо
  • 12 успешни тест(а)
  • 0 неуспешни тест(а)

Код

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 (преди над 5 години)

+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 (преди над 5 години)

-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)]
+ 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)]
+ 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] + math.floor(coef * (255 - pixel[i]))
+ 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] - math.floor(coef * pixel[i]) for i in range(3))
+ 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[i][j] for i in range(image_pixels)]
- for j in range(3)]
+ 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}