Решение на Работа с картинки от Кузман Белев

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

Към профила на Кузман Белев

Резултати

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

Код

COLOR = {0: 'red', 1: 'green', 2: 'blue'}
def rotate_right(image):
return list(zip(*image[::-1]))
def rotate_left(image):
return list(zip(*image))[::-1]
def invert(image):
return [[tuple([255 - x for x in rgb]) for rgb in row] for row in image]
def add(x, coef):
return int(x + coef * (255 - x))
def lighten(image, c):
return modify(image, c)
def darken(image, c):
return modify(image, -c)
def modify(image, c):
return [[tuple([add(x, c) for x in rgb]) for rgb in row] for row in image]
def create_histogram(image):
size = range(len(COLOR))
flt = list(zip(*sum(image, [])))
return {COLOR[i]:
{clr: flt[i].count(clr) for clr in flt[i]} for i in size}

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

..FF........
======================================================================
FAIL: test_darken (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: 127 not less than or equal to 1

======================================================================
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: 28 not less than or equal to 1

----------------------------------------------------------------------
Ran 12 tests in 0.093s

FAILED (failures=2)

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

Кузман обнови решението на 12.03.2016 10:37 (преди над 5 години)

+def rotate_right(image):
+ return list(zip(*iamge[::-1]))
+
+def rotate_left(image):
+ return list(zip(*image))[::-1]
+
+def invert(image):
+ return [[tuple([255 - x for x in rgb]) for rgb in row] for row in image]
+
+def add(x, coef):
+ return int(x + coef * (255 - x))
+
+def sub(x, coef):
+ return int(x - coef * (255 - x))
+
+def lighten(image, c):
+ return [[tuple([add(x, c) for x in rgb]) for rgb in row] for row in image]
+
+def darken(image, c):
+ return [[tuple([sub(x, c) for x in rgb]) for rgb in row] for row in image]
+
+# def create_histogram(image):
+# return {}

Кузман обнови решението на 12.03.2016 16:40 (преди над 5 години)

def rotate_right(image):
return list(zip(*iamge[::-1]))
def rotate_left(image):
return list(zip(*image))[::-1]
def invert(image):
return [[tuple([255 - x for x in rgb]) for rgb in row] for row in image]
def add(x, coef):
return int(x + coef * (255 - x))
def sub(x, coef):
return int(x - coef * (255 - x))
def lighten(image, c):
return [[tuple([add(x, c) for x in rgb]) for rgb in row] for row in image]
def darken(image, c):
return [[tuple([sub(x, c) for x in rgb]) for rgb in row] for row in image]
-# def create_histogram(image):
-# return {}
+def create_histogram(image):
+ result = {"red": {}, "green": {}, "blue": {}}
+ for row in image:
+ for rgb in row:
+ for i in range(3):
+ if i == 0:
+ if rgb[i] in result["red"]:
+ result["red"][rgb[i]] += 1
+ else:
+ result["red"][rgb[i]] = 1
+ if i == 1:
+ if rgb[i] in result["green"]:
+ result["green"][rgb[i]] += 1
+ else:
+ result["green"][rgb[i]] = 1
+ if i == 2:
+ if rgb[i] in result["blue"]:
+ result["blue"][rgb[i]] += 1
+ else:
+ result["blue"][rgb[i]] = 1
+ return result

Кузман обнови решението на 12.03.2016 16:44 (преди над 5 години)

def rotate_right(image):
return list(zip(*iamge[::-1]))
def rotate_left(image):
return list(zip(*image))[::-1]
def invert(image):
return [[tuple([255 - x for x in rgb]) for rgb in row] for row in image]
def add(x, coef):
return int(x + coef * (255 - x))
def sub(x, coef):
return int(x - coef * (255 - x))
def lighten(image, c):
return [[tuple([add(x, c) for x in rgb]) for rgb in row] for row in image]
def darken(image, c):
return [[tuple([sub(x, c) for x in rgb]) for rgb in row] for row in image]
def create_histogram(image):
- result = {"red": {}, "green": {}, "blue": {}}
+ result = {'red': {}, 'green': {}, 'blue': {}}
for row in image:
for rgb in row:
for i in range(3):
if i == 0:
- if rgb[i] in result["red"]:
- result["red"][rgb[i]] += 1
+ if rgb[i] in result['red']:
+ result['red'][rgb[i]] += 1
else:
- result["red"][rgb[i]] = 1
+ result['red'][rgb[i]] = 1
if i == 1:
- if rgb[i] in result["green"]:
- result["green"][rgb[i]] += 1
+ if rgb[i] in result['green']:
+ result['green'][rgb[i]] += 1
else:
- result["green"][rgb[i]] = 1
+ result['green'][rgb[i]] = 1
if i == 2:
- if rgb[i] in result["blue"]:
- result["blue"][rgb[i]] += 1
+ if rgb[i] in result['blue']:
+ result['blue'][rgb[i]] += 1
else:
- result["blue"][rgb[i]] = 1
+ result['blue'][rgb[i]] = 1
return result

Кузман обнови решението на 12.03.2016 17:16 (преди над 5 години)

def rotate_right(image):
- return list(zip(*iamge[::-1]))
+ return list(zip(*image[::-1]))
def rotate_left(image):
return list(zip(*image))[::-1]
def invert(image):
return [[tuple([255 - x for x in rgb]) for rgb in row] for row in image]
def add(x, coef):
return int(x + coef * (255 - x))
def sub(x, coef):
return int(x - coef * (255 - x))
def lighten(image, c):
return [[tuple([add(x, c) for x in rgb]) for rgb in row] for row in image]
def darken(image, c):
return [[tuple([sub(x, c) for x in rgb]) for rgb in row] for row in image]
def create_histogram(image):
result = {'red': {}, 'green': {}, 'blue': {}}
for row in image:
for rgb in row:
for i in range(3):
if i == 0:
if rgb[i] in result['red']:
result['red'][rgb[i]] += 1
else:
result['red'][rgb[i]] = 1
if i == 1:
if rgb[i] in result['green']:
result['green'][rgb[i]] += 1
else:
result['green'][rgb[i]] = 1
if i == 2:
if rgb[i] in result['blue']:
result['blue'][rgb[i]] += 1
else:
result['blue'][rgb[i]] = 1
return result

Не ме радва, че има доста повтарящ се код, направо си е разхищение :)

Особено този в create_histogram е доста неприятен. Представи си, че в един пиксел има 100, вместо три. Тогава ще си по-принуден да не повтаряш if-else-блокове. Току виж си отървал и единия for.

Кузман обнови решението на 13.03.2016 23:22 (преди над 5 години)

+COLOR = {0: 'red', 1: 'green', 2: 'blue'}
+
def rotate_right(image):
return list(zip(*image[::-1]))
def rotate_left(image):
return list(zip(*image))[::-1]
def invert(image):
return [[tuple([255 - x for x in rgb]) for rgb in row] for row in image]
def add(x, coef):
return int(x + coef * (255 - x))
-def sub(x, coef):
- return int(x - coef * (255 - x))
-
def lighten(image, c):
- return [[tuple([add(x, c) for x in rgb]) for rgb in row] for row in image]
+ return modify(image, c)
def darken(image, c):
- return [[tuple([sub(x, c) for x in rgb]) for rgb in row] for row in image]
+ return modify(image, -c)
+def modify(image, c):
+ return [[tuple([add(x, c) for x in rgb]) for rgb in row] for row in image]
+
def create_histogram(image):
- result = {'red': {}, 'green': {}, 'blue': {}}
- for row in image:
+ size = range(len(COLOR))
- for rgb in row:
+ flt = list(zip(*sum(image, [])))
- for i in range(3):
+ return {COLOR[i]:
- if i == 0:
+ { clr: flt[i].count(clr) for clr in flt[i] } for i in size}
- if rgb[i] in result['red']:
- result['red'][rgb[i]] += 1
- else:
- result['red'][rgb[i]] = 1
- if i == 1:
- if rgb[i] in result['green']:
- result['green'][rgb[i]] += 1
- else:
- result['green'][rgb[i]] = 1
- if i == 2:
- if rgb[i] in result['blue']:
- result['blue'][rgb[i]] += 1
- else:
- result['blue'][rgb[i]] = 1
- return result

Кузман обнови решението на 13.03.2016 23:31 (преди над 5 години)

COLOR = {0: 'red', 1: 'green', 2: 'blue'}
+
def rotate_right(image):
return list(zip(*image[::-1]))
+
def rotate_left(image):
return list(zip(*image))[::-1]
+
def invert(image):
return [[tuple([255 - x for x in rgb]) for rgb in row] for row in image]
+
def add(x, coef):
return int(x + coef * (255 - x))
+
def lighten(image, c):
return modify(image, c)
+
def darken(image, c):
return modify(image, -c)
+
def modify(image, c):
return [[tuple([add(x, c) for x in rgb]) for rgb in row] for row in image]
+
def create_histogram(image):
size = range(len(COLOR))
flt = list(zip(*sum(image, [])))
return {COLOR[i]:
- { clr: flt[i].count(clr) for clr in flt[i] } for i in size}
+ {clr: flt[i].count(clr) for clr in flt[i]} for i in size}