Решение на Работа с картинки от Деян Боиклиев

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

Към профила на Деян Боиклиев

Резултати

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

Код

from collections import defaultdict
def rotate_left(picture):
rotated_picture = list(zip(*picture))[::-1]
return rotated_picture
def rotate_right(picture):
rotated_picture = list(zip(*picture[::-1]))
return rotated_picture
def invert(picture):
def invert_pixel(pixel):
return 255 - pixel[0], 255 - pixel[1], 255 - pixel[2]
inverted_picture = [[invert_pixel(pixel) for pixel in row]
for row in picture]
return inverted_picture
def lighten(picture, level):
def lighten_color(color):
return int(color + level * (255 - color))
def lighten_pixel(pixel):
return (lighten_color(pixel[0]),
lighten_color(pixel[1]),
lighten_color(pixel[2]))
lightened_picture = [[lighten_pixel(pixel)
for pixel in row]
for row in picture]
return lightened_picture
def darken(picture, level):
def darken_color(color):
return int(color - color * level)
def darken_pixel(pixel):
return (darken_color(pixel[0]),
darken_color(pixel[1]),
darken_color(pixel[2]))
darkened_picture = [[darken_pixel(pixel)
for pixel in row]
for row in picture]
return darkened_picture
def create_histogram(picture):
histogram = {}
red_levels = defaultdict(lambda: 0)
green_levels = defaultdict(lambda: 0)
blue_levels = defaultdict(lambda: 0)
histogram['red'] = red_levels
histogram['green'] = green_levels
histogram['blue'] = blue_levels
for row in picture:
for pixel in row:
red_levels[pixel[0]] += 1
green_levels[pixel[1]] += 1
blue_levels[pixel[2]] += 1
return histogram

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.110s

OK

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

Деян обнови решението на 13.03.2016 22:26 (преди над 5 години)

+from collections import defaultdict
+
+
+def rotate_left(picture):
+ rotated_picture = list(zip(*picture))[::-1]
+ return rotated_picture
+
+
+def rotate_right(picture):
+ rotated_picture = list(zip(*picture[::-1]))
+ return rotated_picture
+
+
+def invert(picture):
+ def invert_pixel(pixel):
+ return 255 - pixel[0], 255 - pixel[1], 255 - pixel[2]
+
+ inverted_picture = [[invert_pixel(pixel) for pixel in row]
+ for row in picture]
+ return inverted_picture
+
+
+def lighten(picture, level):
+ def lighten_color(color):
+ return int(color + level * (255 - color))
+
+ def lighten_pixel(pixel):
+ return (lighten_color(pixel[0]),
+ lighten_color(pixel[1]),
+ lighten_color(pixel[2]))
+
+ lightened_picture = [[lighten_pixel(pixel)
+ for pixel in row]
+ for row in picture]
+ return lightened_picture
+
+
+def darken(picture, level):
+ def darken_color(color):
+ return int(color - color * level)
+
+ def darken_pixel(pixel):
+ return (darken_color(pixel[0]),
+ darken_color(pixel[1]),
+ darken_color(pixel[2]))
+
+ darkened_picture = [[darken_pixel(pixel)
+ for pixel in row]
+ for row in picture]
+ return darkened_picture
+
+
+def create_histogram(picture):
+ histogram = {}
+ red_levels = defaultdict(lambda: 0)
+ green_levels = defaultdict(lambda: 0)
+ blue_levels = defaultdict(lambda: 0)
+ histogram['red'] = red_levels
+ histogram['green'] = green_levels
+ histogram['blue'] = blue_levels
+ for row in picture:
+ for pixel in row:
+ red_levels[pixel[0]] += 1
+ green_levels[pixel[1]] += 1
+ blue_levels[pixel[2]] += 1
+ return histogram