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

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

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

Резултати

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

Код

from collections import defaultdict
def column(image, index):
return [row[index] for row in image]
def rotate_left(image):
return [column(image, i) for i in range(len(image[0]) - 1, -1, -1)]
def rotate_right(image):
return [reversed(column(image, i)) for i in range(len(image[0]))]
def invert_pixel(pixel_rgb):
return tuple(255 - c for c in pixel_rgb)
def invert(image):
return [[invert_pixel(pixel) for pixel in row] for row in image]
def lighten_pixel(pixel_rgb, factor):
return tuple(c + int(factor * (255 - c)) for c in pixel_rgb)
def lighten(image, factor):
return [[lighten_pixel(pixel, factor) for pixel in row] for row in image]
def darken_pixel(pixel_rgb, factor):
return tuple(int((1 - factor) * c) for c in pixel_rgb)
def darken(image, factor):
return [[darken_pixel(pixel, factor) for pixel in row] for row in image]
def create_histogram(image):
histogram = defaultdict(lambda: defaultdict(int))
for row in image:
for pixel in row:
histogram['red'][pixel[0]] += 1
histogram['green'][pixel[1]] += 1
histogram['blue'][pixel[2]] += 1
return histogram

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

..........EE
======================================================================
ERROR: 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
TypeError: object of type 'list_reverseiterator' has no len()

======================================================================
ERROR: 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
TypeError: object of type 'list_reverseiterator' has no len()

----------------------------------------------------------------------
Ran 12 tests in 0.128s

FAILED (errors=2)

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

Светомир обнови решението на 08.03.2016 14:21 (преди над 5 години)

+from collections import defaultdict
+
+
+def column(image, index):
+ return [row[index] for row in image]
+
+
+def rotate_left(image):
+ return [column(image, i) for i in range(len(image[0]) - 1, -1, -1)]
+
+
+def rotate_right(image):
+ return [reversed(column(image, i)) for i in range(len(image[0]))]
+
+
+def invert_pixel(pixel_rgb):
+ return tuple(255 - c for c in pixel_rgb)
+
+
+def invert(image):
+ return [[invert_pixel(pixel) for pixel in row] for row in image]
+
+
+def lighten_pixel(pixel_rgb, factor):
+ return tuple(c + int(factor * (255 - c)) for c in pixel_rgb)
+
+
+def lighten(image, factor):
+ return [[lighten_pixel(pixel, factor) for pixel in row] for row in image]
+
+
+def darken_pixel(pixel_rgb, factor):
+ return tuple(int((1 - factor) * c) for c in pixel_rgb)
+
+
+def darken(image, factor):
+ return [[darken_pixel(pixel, factor) for pixel in row] for row in image]
+
+
+def create_histogram(image):
+ histogram = defaultdict(lambda: defaultdict(int))
+ for row in image:
+ for pixel in row:
+ histogram['red'][pixel[0]] += 1
+ histogram['green'][pixel[1]] += 1
+ histogram['blue'][pixel[2]] += 1
+ return histogram