Решение на Работа с картинки от Марианна Владимирова

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

Към профила на Марианна Владимирова

Резултати

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

Код

def rotate_left(image):
row_length = len(image[0])
new_matrix = [[row[index] for row in image] for index in range(row_length)]
rotated_left = new_matrix[::-1]
return rotated_left
def rotate_right(image):
length = len(image[0])
new_matrix = [[row[index] for row in image][::-1]
for index in range(length)]
return new_matrix
def invert(image):
row_length = len(image[0])
for row in image:
for index in range(row_length):
red, green, blue = row[index]
red = 255 - red
green = 255 - green
blue = 255 - blue
row[index] = red, green, blue
return image
def lighten(image, light_coefficient):
row_length = len(image[0])
for row in image:
for index in range(row_length):
red, green, blue = row[index]
red = calculate_lighter_colour(red, light_coefficient)
green = calculate_lighter_colour(green, light_coefficient)
blue = calculate_lighter_colour(blue, light_coefficient)
row[index] = red, green, blue
return image
def darken(image, dark_coefficient):
row_length = len(image)
for row in image:
for index in range(row_length):
red, green, blue = row[index]
red = calculate_darker_colour(red, dark_coefficient)
green = calculate_darker_colour(green, dark_coefficient)
blue = calculate_darker_colour(blue, dark_coefficient)
row[index] = red, green, blue
return image
def calculate_lighter_colour(colour, coefficient):
lighter_colour = int((colour + coefficient * (255 - colour)))
return lighter_colour
def calculate_darker_colour(colour, coefficient):
darker_colour = int((colour - coefficient * (colour - 0)))
return darker_colour
def create_histogram(image):
row_length = len(image)
histogram = {'red': {}, 'green': {}, 'blue': {}}
for row in image:
for index in range(row_length):
red, green, blue = row[index]
if red in histogram['red']:
histogram['red'][red] += 1
else:
histogram['red'][red] = 1
if green in histogram['green']:
histogram['green'][green] += 1
else:
histogram['green'][green] = 1
if blue in histogram['blue']:
histogram['blue'][blue] += 1
else:
histogram['blue'][blue] = 1
return histogram

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

.F.F........
======================================================================
FAIL: test_create_histogram_rectangle (test.TestImages)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: {'blue': {0: 4, 1: 1, 4: 1, 255: 3}, 'green': {0: 3, 4[61 chars]: 2}} != {'blue': {0: 5, 1: 2, 4: 1, 255: 4}, 'green': {0: 4, 5[81 chars]: 3}}
- {'blue': {0: 4, 1: 1, 4: 1, 255: 3},
?              ^       ------      ^

+ {'blue': {0: 5, 1: 2, 4: 1, 255: 4},
?              ^     ++++++        ^

-  'green': {0: 3, 12: 1, 42: 1, 54: 1, 255: 3},
?               ^                            ^

+  'green': {0: 4, 12: 1, 42: 1, 54: 1, 60: 1, 255: 4},
?               ^                      +++++++      ^

-  'red': {0: 5, 72: 1, 100: 1, 255: 2}}
?                                    ^

+  'red': {0: 5, 1: 1, 72: 1, 90: 1, 100: 1, 255: 3}}
?               ++++++       +++++++              ^


======================================================================
FAIL: test_darken_rectangle (test.TestImages)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: 45 not less than or equal to 1

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

FAILED (failures=2)

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

Марианна обнови решението на 12.03.2016 18:50 (преди над 5 години)

+def rotate_left(image):
+ row_length = len(image[0])
+ new_matrix = [[row[index] for row in image] for index in range(row_length)]
+ rotated_left = new_matrix[::-1]
+ return rotated_left
+
+
+def rotate_right(image):
+ length = len(image[0])
+ new_matrix = [[row[index] for row in image][::-1]
+ for index in range(length)]
+ return new_matrix
+
+
+def invert(image):
+ row_length = len(image[0])
+ for row in image:
+ for index in range(row_length):
+ red, green, blue = row[index]
+ red = 255 - red
+ green = 255 - green
+ blue = 255 - blue
+ row[index] = red, green, blue
+ return image
+
+
+def lighten(image, light_coefficient):
+ row_length = len(image[0])
+ for row in image:
+ for index in range(row_length):
+ red, green, blue = row[index]
+ red = calculate_lighter_colour(red, light_coefficient)
+ green = calculate_lighter_colour(green, light_coefficient)
+ blue = calculate_lighter_colour(blue, light_coefficient)
+ row[index] = red, green, blue
+ return image
+
+
+def darken(image, dark_coefficient):
+ row_length = len(image)
+ for row in image:
+ for index in range(row_length):
+ red, green, blue = row[index]
+ red = calculate_darker_colour(red, dark_coefficient)
+ green = calculate_darker_colour(green, dark_coefficient)
+ blue = calculate_darker_colour(blue, dark_coefficient)
+ row[index] = red, green, blue
+ return image
+
+
+def calculate_lighter_colour(colour, coefficient):
+ lighter_colour = int((colour + coefficient * (255 - colour)))
+ return lighter_colour
+
+
+def calculate_darker_colour(colour, coefficient):
+ darker_colour = int((colour - coefficient * (colour - 0)))
+ return darker_colour
+
+
+def create_histogram(image):
+ row_length = len(image)
+ histogram = {'red': {}, 'green': {}, 'blue': {}}
+
+ for row in image:
+ for index in range(row_length):
+ red, green, blue = row[index]
+ if red in histogram['red']:
+ histogram['red'][red] += 1
+ else:
+ histogram['red'][red] = 1
+ if green in histogram['green']:
+ histogram['green'][green] += 1
+ else:
+ histogram['green'][green] = 1
+ if blue in histogram['blue']:
+ histogram['blue'][blue] += 1
+ else:
+ histogram['blue'][blue] = 1
+
+ return histogram