Решение на Работа с картинки от Александър Наджарян

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

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

Резултати

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

Код

from itertools import chain
from collections import defaultdict
from collections import Counter
def rotate_left(image):
iteration = range(len(image[0])-1, -1, -1)
return [[row[column] for row in image] for column in iteration]
def rotate_right(image):
iteration = range(len(image[0]))
return [[row[column] for row in image[::-1]] for column in iteration]
def invert(image):
return [[(255-r, 255-g, 255-b) for r, g, b in row] for row in image]
def lighten_(pixel, alpha):
return tuple(map(lambda x: int(x + alpha*(255 - x)), pixel))
def lighten(image, alpha):
return [[lighten_(pixel, alpha) for pixel in row] for row in image]
def darken_(pixel, alpha):
return tuple(map(lambda x: int(x - alpha*x), pixel))
def darken(image, alpha):
return [[darken_(pixel, alpha) for pixel in row] for row in image]
def create_histogram(image):
colours = ('red', 'green', 'blue')
pixels = list(chain.from_iterable(image))
by_colour = [[pixel[i] for pixel in pixels] for i in range(3)]
return {colour: Counter(by_colour[i]) for i, colour in enumerate(colours)}

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.087s

OK

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

Александър обнови решението на 08.03.2016 11:40 (преди над 5 години)

+from itertools import chain
+from collections import defaultdict
+from collections import Counter
+
+
+def rotate_left(image):
+ iteration = range(len(image[0])-1, -1, -1)
+ return [[row[column] for row in image] for column in iteration]
+
+
+def rotate_right(image):
+ iteration = range(len(image[0]))
+ return [[row[column] for row in image[::-1]] for column in iteration]
+
+
+def invert(image):
+ return [[(255-r, 255-g, 255-b) for r, g, b in row] for row in image]
+
+
+def lighten_(pixel, alpha):
+ return tuple(map(lambda x: int(x + alpha*(255 - x)), pixel))
+
+
+def lighten(image, alpha):
+ return [[lighten_(pixel, alpha) for pixel in row] for row in image]
+
+
+def darken_(pixel, alpha):
+ return tuple(map(lambda x: int(x - alpha*x), pixel))
+
+
+def darken(image, alpha):
+ return [[darken_(pixel, alpha) for pixel in row] for row in image]
+
+
+def create_histogram(image):
+ colours = ('red', 'green', 'blue')
+ histogram = {colour: defaultdict(int) for colour in colours}
+ counted_pixels = Counter(chain.from_iterable(image))
+
+ for pixel, occurences in counted_pixels.items():
+ for i, colour in enumerate(colours):
+ histogram[colour][pixel[i]] += occurences
+ return histogram

Александър обнови решението на 08.03.2016 13:30 (преди над 5 години)

from itertools import chain
from collections import defaultdict
from collections import Counter
def rotate_left(image):
iteration = range(len(image[0])-1, -1, -1)
return [[row[column] for row in image] for column in iteration]
def rotate_right(image):
iteration = range(len(image[0]))
return [[row[column] for row in image[::-1]] for column in iteration]
def invert(image):
return [[(255-r, 255-g, 255-b) for r, g, b in row] for row in image]
def lighten_(pixel, alpha):
return tuple(map(lambda x: int(x + alpha*(255 - x)), pixel))
def lighten(image, alpha):
return [[lighten_(pixel, alpha) for pixel in row] for row in image]
def darken_(pixel, alpha):
return tuple(map(lambda x: int(x - alpha*x), pixel))
def darken(image, alpha):
return [[darken_(pixel, alpha) for pixel in row] for row in image]
def create_histogram(image):
colours = ('red', 'green', 'blue')
- histogram = {colour: defaultdict(int) for colour in colours}
- counted_pixels = Counter(chain.from_iterable(image))
+ pixels = list(chain.from_iterable(image))
+ by_colour = [[pixel[i] for pixel in pixels] for i in range(3)]
+ return {colour: Counter(by_colour[i]) for i, colour in enumerate(colours)}
- for pixel, occurences in counted_pixels.items():
- for i, colour in enumerate(colours):
- histogram[colour][pixel[i]] += occurences
- return histogram