Решение на Работа с картинки от Пламен Начев

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

Към профила на Пламен Начев

Резултати

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

Код

def rotate_right(image):
rotated_image = []
number_of_rows = len(image)
number_of_columns = len(image[0])
for column in range(number_of_columns):
current_row = []
for row in range(number_of_rows-1, -1, -1):
current_row.append(image[row][column])
rotated_image.append(current_row)
return rotated_image
def rotate_left(image):
rotated_image = []
number_of_rows = len(image)
number_of_columns = len(image[0])
for column in range(number_of_columns-1, -1, -1):
current_row = []
for row in range(number_of_rows):
current_row.append(image[row][column])
rotated_image.append(current_row)
return rotated_image
def inverted_pixel(pixel):
return (255-pixel[0],
255-pixel[1],
255-pixel[2])
def invert(image):
inverted_image = []
number_of_rows = len(image)
number_of_columns = len(image[0])
for row in range(number_of_rows):
current_row = []
for column in range(number_of_columns):
current_row.append(inverted_pixel(image[row][column]))
inverted_image.append(current_row)
return inverted_image
def lightened_pixel(pixel, factor):
return (int(pixel[0] + factor*(255-pixel[0])),
int(pixel[1] + factor*(255-pixel[1])),
int(pixel[2] + factor*(255-pixel[2])))
def lighten(image, factor):
lightened_image = []
number_of_rows = len(image)
number_of_columns = len(image[0])
for row in range(number_of_rows):
current_row = []
for column in range(number_of_columns):
current_row.append(lightened_pixel(image[row][column], factor))
lightened_image.append(current_row)
return lightened_image
def darkened_pixel(pixel, factor):
return (int(pixel[0] - factor*(pixel[0]-0)),
int(pixel[1] - factor*(pixel[1]-0)),
int(pixel[2] - factor*(pixel[2]-0)))
def darken(image, factor):
darkened_image = []
number_of_rows = len(image)
number_of_columns = len(image[0])
for row in range(number_of_rows):
current_row = []
for column in range(number_of_columns):
current_row.append(darkened_pixel(image[row][column], factor))
darkened_image.append(current_row)
return darkened_image
def create_histogram(image):
red_values = {}
green_values = {}
blue_values = {}
number_of_rows = len(image)
number_of_columns = len(image[0])
for row in range(number_of_rows):
for column in range(number_of_columns):
red = image[row][column][0]
green = image[row][column][1]
blue = image[row][column][2]
if red in red_values:
red_values[red] += 1
else:
red_values[red] = 1
if green in green_values:
green_values[green] += 1
else:
green_values[green] = 1
if blue in blue_values:
blue_values[blue] += 1
else:
blue_values[blue] = 1
colors = {}
colors['red'] = red_values
colors['green'] = green_values
colors['blue'] = blue_values
return colors

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.089s

OK

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

Пламен обнови решението на 13.03.2016 23:54 (преди над 5 години)

+def rotate_right(image):
+
+ rotated_image = []
+ number_of_rows = len(image)
+ number_of_columns = len(image[0])
+ for column in range(number_of_columns):
+ current_row = []
+ for row in range(number_of_rows-1, -1, -1):
+ current_row.append(image[row][column])
+ rotated_image.append(current_row)
+
+ return rotated_image
+
+
+def rotate_left(image):
+
+ rotated_image = []
+ number_of_rows = len(image)
+ number_of_columns = len(image[0])
+ for column in range(number_of_columns-1, -1, -1):
+ current_row = []
+ for row in range(number_of_rows):
+ current_row.append(image[row][column])
+ rotated_image.append(current_row)
+
+ return rotated_image
+
+
+def inverted_pixel(pixel):
+ return (255-pixel[0],
+ 255-pixel[1],
+ 255-pixel[2])
+
+
+def invert(image):
+
+ inverted_image = []
+ number_of_rows = len(image)
+ number_of_columns = len(image[0])
+ for row in range(number_of_rows):
+ current_row = []
+ for column in range(number_of_columns):
+ current_row.append(inverted_pixel(image[row][column]))
+ inverted_image.append(current_row)
+
+ return inverted_image
+
+
+def lightened_pixel(pixel, factor):
+ return (int(pixel[0] + factor*(255-pixel[0])),
+ int(pixel[1] + factor*(255-pixel[1])),
+ int(pixel[2] + factor*(255-pixel[2])))
+
+
+def lighten(image, factor):
+
+ lightened_image = []
+ number_of_rows = len(image)
+ number_of_columns = len(image[0])
+ for row in range(number_of_rows):
+ current_row = []
+ for column in range(number_of_columns):
+ current_row.append(lightened_pixel(image[row][column], factor))
+ lightened_image.append(current_row)
+
+ return lightened_image
+
+
+def darkened_pixel(pixel, factor):
+ return (int(pixel[0] - factor*(pixel[0]-0)),
+ int(pixel[1] - factor*(pixel[1]-0)),
+ int(pixel[2] - factor*(pixel[2]-0)))
+
+
+def darken(image, factor):
+
+ darkened_image = []
+ number_of_rows = len(image)
+ number_of_columns = len(image[0])
+ for row in range(number_of_rows):
+ current_row = []
+ for column in range(number_of_columns):
+ current_row.append(darkened_pixel(image[row][column], factor))
+ darkened_image.append(current_row)
+
+ return darkened_image
+
+
+def create_histogram(image):
+
+ red_values = {}
+ green_values = {}
+ blue_values = {}
+ number_of_rows = len(image)
+ number_of_columns = len(image[0])
+ for row in range(number_of_rows):
+ for column in range(number_of_columns):
+ red = image[row][column][0]
+ green = image[row][column][1]
+ blue = image[row][column][2]
+ if red in red_values:
+ red_values[red] += 1
+ else:
+ red_values[red] = 1
+ if green in green_values:
+ green_values[green] += 1
+ else:
+ green_values[green] = 1
+ if blue in blue_values:
+ blue_values[blue] += 1
+ else:
+ blue_values[blue] = 1
+
+ colors = {}
+ colors['red'] = red_values
+ colors['green'] = green_values
+ colors['blue'] = blue_values
+
+ return colors