Решение на Работа с картинки от Николай Желязков

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

Към профила на Николай Желязков

Резултати

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

Код

def rotate_left(image):
return rotate(image, reversed)
def rotate_right(image):
return rotate(reversed(image), lambda x: x)
def rotate(image, rev):
rotated_image = []
for row in image:
i = 0
for col in rev(row):
if len(rotated_image) <= i:
rotated_image.append([col])
else:
rotated_image[i].append(col)
i += 1
return rotated_image
def invert(image):
inverted_image = []
i = 0
for row in image:
for col in row:
current_pixel = tuple(255 - x for x in col)
if len(inverted_image) <= i:
inverted_image.append([current_pixel])
else:
inverted_image[i].append(current_pixel)
i += 1
return inverted_image
def darken(image, factor):
return darken_lighten(image, lambda x: x - factor * x)
def lighten(image, factor):
return darken_lighten(image, lambda x: x + factor * (255 - x))
def darken_lighten(image, formula):
new_image = []
i = 0
for row in image:
for col in row:
current_pixel = tuple(int(formula(x)) for x in col)
if len(new_image) <= i:
new_image.append([current_pixel])
else:
new_image[i].append(current_pixel)
i += 1
return new_image
def create_histogram(image):
histogram = {'red': {}, 'green': {}, 'blue': {}}
for row in image:
for col in row:
for value, color in zip(col, ['red', 'green', 'blue']):
if value in histogram[color]:
histogram[color][value] += 1
else:
histogram[color][value] = 1
return histogram

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

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

OK

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

Николай обнови решението на 08.03.2016 23:12 (преди над 5 години)

+def rotate_left(image):
+ rotated_image = []
+ for row in image:
+ i = 0
+ for col in reversed(row):
+ if len(rotated_image) <= i:
+ rotated_image.append([col])
+ else:
+ rotated_image[i].append(col)
+ i += 1
+ return rotated_image
+
+def rotate_right(image):
+ rotated_image = []
+ for row in reversed(image):
+ i = 0
+ for col in row:
+ if len(rotated_image) <= i:
+ rotated_image.append([col])
+ else:
+ rotated_image[i].append(col)
+ i += 1
+ return rotated_image
+
+def invert(image):
+ inverted_image = []
+ i = 0
+ for row in image:
+ for col in row:
+ current_pixel = tuple( 255 - x for x in col)
+ if len(inverted_image) <= i:
+ inverted_image.append([current_pixel])
+ else:
+ inverted_image[i].append(current_pixel)
+ i += 1
+ return inverted_image
+
+def lighten(image, factor):
+ lightened_image = []
+ i = 0
+ for row in image:
+ for col in row:
+ current_pixel = tuple(int(x + factor * (255 - x)) for x in col)
+ if len(lightened_image) <= i:
+ lightened_image.append([current_pixel])
+ else:
+ lightened_image[i].append(current_pixel)
+ i += 1
+ return lightened_image
+
+def darken(image, factor):
+ darkened_image = []
+ i = 0
+ for row in image:
+ for col in row:
+ current_pixel = tuple(int(x - factor * x) for x in col)
+ if len(darkened_image) <= i:
+ darkened_image.append([current_pixel])
+ else:
+ darkened_image[i].append(current_pixel)
+ i += 1
+ return darkened_image
+
+def create_histogram(image):
+ histogram = {'red': {}, 'green': {}, 'blue': {}}
+ for row in image:
+ for col in row:
+ if col[0] in histogram['red']:
+ histogram['red'][col[0]] += 1
+ else:
+ histogram['red'][col[0]] = 1
+
+ if col[1] in histogram['green']:
+ histogram['green'][col[1]] += 1
+ else:
+ histogram['green'][col[1]] = 1
+
+ if col[2] in histogram['blue']:
+ histogram['blue'][col[2]] += 1
+ else:
+ histogram['blue'][col[2]] = 1
+ return histogram

Николай обнови решението на 10.03.2016 08:12 (преди над 5 години)

def rotate_left(image):
+ return rotate(image, reversed)
+
+
+def rotate_right(image):
+ return rotate(reversed(image), lambda x: x)
+
+
+def rotate(image, rev):
rotated_image = []
for row in image:
i = 0
- for col in reversed(row):
+ for col in rev(row):
if len(rotated_image) <= i:
rotated_image.append([col])
else:
rotated_image[i].append(col)
i += 1
return rotated_image
-
-def rotate_right(image):
- rotated_image = []
- for row in reversed(image):
- i = 0
- for col in row:
- if len(rotated_image) <= i:
- rotated_image.append([col])
- else:
- rotated_image[i].append(col)
- i += 1
- return rotated_image
-
+
+
def invert(image):
inverted_image = []
i = 0
for row in image:
for col in row:
- current_pixel = tuple( 255 - x for x in col)
+ current_pixel = tuple(255 - x for x in col)
if len(inverted_image) <= i:
inverted_image.append([current_pixel])
else:
inverted_image[i].append(current_pixel)
i += 1
return inverted_image
-
+
+
+def darken(image, factor):
+ return darken_lighten(image, lambda x: x - factor * x)
+
+
def lighten(image, factor):
- lightened_image = []
+ return darken_lighten(image, lambda x: x + factor * (255 - x))
+
+
+def darken_lighten(image, formula):
+ new_image = []
i = 0
for row in image:
for col in row:
- current_pixel = tuple(int(x + factor * (255 - x)) for x in col)
- if len(lightened_image) <= i:
- lightened_image.append([current_pixel])
+ current_pixel = tuple(int(formula(x)) for x in col)
+ if len(new_image) <= i:
+ new_image.append([current_pixel])
else:
- lightened_image[i].append(current_pixel)
+ new_image[i].append(current_pixel)
i += 1
- return lightened_image
-
-def darken(image, factor):
- darkened_image = []
- i = 0
- for row in image:
- for col in row:
- current_pixel = tuple(int(x - factor * x) for x in col)
- if len(darkened_image) <= i:
- darkened_image.append([current_pixel])
- else:
- darkened_image[i].append(current_pixel)
- i += 1
- return darkened_image
-
+ return new_image
+
+
def create_histogram(image):
histogram = {'red': {}, 'green': {}, 'blue': {}}
for row in image:
for col in row:
- if col[0] in histogram['red']:
- histogram['red'][col[0]] += 1
+ for value, color in zip(col, ['red', 'green', 'blue']):
- else:
+ if value in histogram[color]:
- histogram['red'][col[0]] = 1
+ histogram[color][value] += 1
-
+ else:
- if col[1] in histogram['green']:
+ histogram[color][value] = 1
- histogram['green'][col[1]] += 1
+ return histogram
- else:
- histogram['green'][col[1]] = 1
-
- if col[2] in histogram['blue']:
- histogram['blue'][col[2]] += 1
- else:
- histogram['blue'][col[2]] = 1
- return histogram