Решение на Работа с картинки от Симеон Ролев

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

Към профила на Симеон Ролев

Резултати

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

Код

def take_column_by_number(number, my_list):
result = []
for i in range(len(my_list)):
result.append(my_list[i][number])
return result
def rotate_left(image):
new_image = []
for i in range(len(image[0])):
new_image.append(take_column_by_number(i, image))
return new_image[::-1]
def rotate_right(image):
new_image = []
for i in range(len(image[0])):
new_image.append(take_column_by_number(i, image))
return new_image
def invert(image):
new_image = []
for row in image:
new_row = []
for pixel in row:
new_pixel = (255 - pixel[0], 255 - pixel[1], 255-pixel[2])
new_row.append(new_pixel)
new_image.append(new_row)
return new_image
def lighten(image, coef):
new_image = []
for row in image:
new_row = []
for pixel in row:
new_pixel = (
int(pixel[0] + coef * (255 - pixel[0])),
int(pixel[1] + coef * (255 - pixel[1])),
int(pixel[2] + coef * (255 - pixel[2])))
new_row.append(new_pixel)
new_image.append(new_row)
return new_image
def darken(image, coef):
new_image = []
for row in image:
new_row = []
for pixel in row:
new_pixel = (
int(pixel[0] - coef*pixel[0]),
int(pixel[1] - coef*pixel[1]),
int(pixel[2] - coef*pixel[2]))
new_row.append(new_pixel)
new_image.append(new_row)
return new_image
def create_histogram(image):
hist = {'red': {}, 'green': {}, 'blue': {}}
for color in range(0, 3):
for row in image:
for elem in row:
if color == 0:
hist['red'][elem[0]] = hist['red'].get(elem[0], 0) + 1
elif color == 1:
hist['green'][elem[1]] = hist['green'].get(elem[1], 0) + 1
else:
hist['blue'][elem[2]] = hist['blue'].get(elem[2], 0) + 1
return hist

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

..........FF
======================================================================
FAIL: test_rotate_right (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: Tuples differ: (0, 255, 0) != (0, 0, 255)

First differing element 1:
255
0

- (0, 255, 0)
+ (0, 0, 255)

======================================================================
FAIL: test_rotate_right_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: Tuples differ: (0, 255, 1) != (100, 0, 255)

First differing element 0:
0
100

- (0, 255, 1)
+ (100, 0, 255)

----------------------------------------------------------------------
Ran 12 tests in 0.111s

FAILED (failures=2)

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

Симеон обнови решението на 14.03.2016 00:45 (преди над 5 години)

+def take_column_by_number(number, my_list):
+ result = []
+ for i in range(len(my_list)):
+ result.append(my_list[i][number])
+ return result
+
+
+def rotate_left(image):
+ new_image = []
+ for i in range(len(image[0])):
+ new_image.append(take_column_by_number(i, image))
+ return new_image[::-1]
+
+
+def rotate_right(image):
+ new_image = []
+ for i in range(len(image[0])):
+ new_image.append(take_column_by_number(i, image))
+ return new_image
+
+
+def invert(image):
+ new_image = []
+ for row in image:
+ new_row = []
+ for pixel in row:
+ new_pixel = (255 - pixel[0], 255 - pixel[1], 255-pixel[2])
+ new_row.append(new_pixel)
+ new_image.append(new_row)
+ return new_image
+
+
+def lighten(image, coef):
+ new_image = []
+ for row in image:
+ new_row = []
+ for pixel in row:
+ new_pixel = (
+ int(pixel[0] + coef * (255 - pixel[0])),
+ int(pixel[1] + coef * (255 - pixel[1])),
+ int(pixel[2] + coef * (255 - pixel[2])))
+ new_row.append(new_pixel)
+ new_image.append(new_row)
+ return new_image
+
+
+def darken(image, coef):
+ new_image = []
+ for row in image:
+ new_row = []
+ for pixel in row:
+ new_pixel = (
+ int(pixel[0] - coef*pixel[0]),
+ int(pixel[1] - coef*pixel[1]),
+ int(pixel[2] - coef*pixel[2]))
+ new_row.append(new_pixel)
+ new_image.append(new_row)
+ return new_image
+
+
+def create_histogram(image):
+ hist = {'red': {}, 'green': {}, 'blue': {}}
+
+ for color in range(0, 3):
+ for row in image:
+ for elem in row:
+ if color == 0:
+ hist['red'][elem[0]] = hist['red'].get(elem[0], 0) + 1
+ elif color == 1:
+ hist['green'][elem[1]] = hist['green'].get(elem[1], 0) + 1
+ else:
+ hist['blue'][elem[2]] = hist['blue'].get(elem[2], 0) + 1
+
+ return hist