Решение на Работа с картинки от Ивета Чампоева

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

Към профила на Ивета Чампоева

Резултати

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

Код

def rotate_left(image):
row = len(image)
col = len(image[0])
return [[image[i][j] for i in range(row)]
for j in range(col - 1, -1, -1)]
def rotate_right(image):
row = len(image)
col = len(image[0])
return [[image[i][j] for i in range(row - 1, -1, -1)]
for j in range(col)]
def invert(image):
row = len(image)
col = len(image[0])
return [[tuple([255 - x for x in image[i][j]])
for j in range(col)] for i in range(row)]
def lighten(image, index):
row = len(image)
col = len(image[0])
return [[tuple([x + int(index*(255 - x)) for x in image[i][j]])
for j in range(col)] for i in range(row)]
def darken(image, index):
row = len(image)
col = len(image[0])
return [[tuple([x - int(index*(x - 0)) for x in image[i][j]])
for j in range(col)] for i in range(row)]
def create_histogram(image):
histogram = {'red': {}, 'green': {}, 'blue': {}}
rows = len(image)
columns = len(image[0])
for i in range(rows):
for j in range(columns):
r, g, b = image[i][j]
if r in histogram['red'].keys():
histogram['red'][r] += 1
else:
histogram['red'][r] = 1
if g in histogram['green'].keys():
histogram['green'][g] += 1
else:
histogram['green'][g] = 1
if b in histogram['blue'].keys():
histogram['blue'][b] += 1
else:
histogram['blue'][b] = 1
return histogram

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.084s

OK

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

Ивета обнови решението на 12.03.2016 22:30 (преди над 5 години)

+
+def rotate_left(image):
+ row = len(image)
+ col = len(image[0])
+ return [[image[i][j] for i in range(row)]
+ for j in range(col - 1, -1, -1)]
+
+def rotate_right(image):
+ row = len(image)
+ col = len(image[0])
+ return [[image[i][j] for i in range(row - 1, -1, -1)]
+ for j in range(col)]
+
+def invert(image):
+ row = len(image)
+ col = len(image[0])
+ return [[tuple([255 - x for x in image[i][j]])
+ for j in range(col)] for i in range(row)]
+
+def lighten(image, index):
+ row = len(image)
+ col = len(image[0])
+ return [[tuple([x + int(index*(255 - x)) for x in image[i][j]])
+ for j in range(col)] for i in range(row)]
+
+def darken(image, index):
+ row = len(image)
+ col = len(image[0])
+ return [[tuple([x - int(index*(x - 0)) for x in image[i][j]])
+ for j in range(col)] for i in range(row)]
+
+def create_histogram(image):
+ histogram = {'red': {}, 'green': {}, 'blue': {}}
+ rows = len(image)
+ columns = len(image[0])
+ for i in range(rows):
+ for j in range(columns):
+ r, g, b = image[i][j]
+ if r in histogram['red'].keys():
+ histogram['red'][r] += 1
+ else:
+ histogram['red'][r] = 1
+ if g in histogram['green'].keys():
+ histogram['green'][g] += 1
+ else:
+ histogram['green'][g] = 1
+ if b in histogram['blue'].keys():
+ histogram['blue'][b] += 1
+ else:
+ histogram['blue'][b] = 1
+ return histogram

Само да попитам по-добър вариант ли щеше да е:

histogram = {}
list_of_separated_colours = list(zip(*(list_of_lists_to_list(image))))
histogram['red'] = Counter(list_of_separated_colours[0])
histogram['green'] = Counter(list_of_separated_colours[1])
histogram['blue'] = Counter(list_of_separated_colours[2])
return histogram

Като list_of_lists_to_list ми прави картинката на списък от tuple-и