Решение на Работа с картинки от Николай Бабулков

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

Към профила на Николай Бабулков

Резултати

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

Код

MAX_COLOR_VALUE = 255
def rotate_left(image):
rotated_image = []
width = len(image[0])
heigth = len(image)
for row in range(width):
curr_row = [image[pixel][width - row - 1] for pixel in range(heigth)]
rotated_image.append(tuple(curr_row))
return rotated_image
def rotate_right(image):
rotated_image = []
width = len(image[0])
heigth = len(image)
for row in range(width):
curr_row = [image[heigth - pixel - 1][row] for pixel in range(heigth)]
rotated_image.append(tuple(curr_row))
return rotated_image
def invert(image):
new_image = []
for row in image:
new_row = []
for pixel in row:
inverted_pixel = [MAX_COLOR_VALUE - color for color in pixel]
new_row.append(tuple(inverted_pixel))
new_image.append(new_row)
return new_image
def calc_lighten(color, coefficient):
return int(color + coefficient * (MAX_COLOR_VALUE - color))
def lighten(image, coeff):
new_image = []
for row in image:
new_row = []
for pixel in row:
lighten_pixel = [calc_lighten(color, coeff) for color in pixel]
new_row.append(tuple(lighten_pixel))
new_image.append(new_row)
return new_image
def calc_darken(color, coefficient):
return int(color - coefficient * color)
def darken(image, coeff):
new_image = []
for row in image:
new_row = []
for pixel in row:
darken_pixel = [calc_darken(color, coeff) for color in pixel]
new_row.append(tuple(darken_pixel))
new_image.append(new_row)
return new_image
def create_histogram(image):
VALUE = 1
histogram = [('red', {}), ('green', {}), ('blue', {})]
for row in image:
for pixel in row:
for color in range(len(histogram)):
color_values = histogram[color][VALUE]
if pixel[color] in color_values:
color_values[pixel[color]] += 1
else:
color_values[pixel[color]] = 1
color += 1
return dict(histogram)

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.091s

OK

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

Николай обнови решението на 13.03.2016 17:52 (преди над 5 години)

+MAX_COLOR_VALUE = 255
+
+
+def rotate_left(image):
+ rotated_image = []
+ width = len(image[0])
+ heigth = len(image)
+ for row in range(width):
+ curr_row = [image[pixel][width - row - 1] for pixel in range(heigth)]
+ rotated_image.append(tuple(curr_row))
+ return rotated_image
+
+
+def rotate_right(image):
+ rotated_image = []
+ width = len(image[0])
+ heigth = len(image)
+ for row in range(width):
+ curr_row = [image[heigth - pixel - 1][row] for pixel in range(heigth)]
+ rotated_image.append(tuple(curr_row))
+ return rotated_image
+
+
+def invert(image):
+ new_image = []
+ for row in image:
+ new_row = []
+ for pixel in row:
+ inverted_pixel = [MAX_COLOR_VALUE - color for color in pixel]
+ new_row.append(tuple(inverted_pixel))
+ new_image.append(new_row)
+ return new_image
+
+
+def calc_lighten(color, coefficient):
+ return int(color + coefficient * (MAX_COLOR_VALUE - color))
+
+
+def lighten(image, coeff):
+ new_image = []
+ for row in image:
+ new_row = []
+ for pixel in row:
+ lighten_pixel = [calc_lighten(color, coeff) for color in pixel]
+ new_row.append(tuple(lighten_pixel))
+ new_image.append(new_row)
+ return new_image
+
+
+def calc_darken(color, coefficient):
+ return int(color - coefficient * color)
+
+
+def darken(image, coeff):
+ new_image = []
+ for row in image:
+ new_row = []
+ for pixel in row:
+ darken_pixel = [calc_darken(color, coeff) for color in pixel]
+ new_row.append(tuple(darken_pixel))
+ new_image.append(new_row)
+ return new_image
+
+
+def create_histogram(image):
+ VALUE = 1
+ histogram = [('red', {}), ('green', {}), ('blue', {})]
+ for row in image:
+ for pixel in row:
+ for color in range(len(histogram)):
+ color_values = histogram[color][VALUE]
+ if pixel[color] in color_values:
+ color_values[pixel[color]] += 1
+ else:
+ color_values[pixel[color]] = 1
+ color += 1
+ return dict(histogram)