Решение на Работа с картинки от Бисер Кръстев

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

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

Резултати

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

Код

from collections import defaultdict
def dup(image):
rows = len(image)
cols = len(image[0])
#couldn't import copy for some reason ?!
copy = [ [ image[x][y] for y in range(cols) ] for x in range(rows) ]
return rows, cols, copy
def process_pixel(p, f, args):
return int(f(p[0], args)), int(f(p[1], args)), int(f(p[2], args))
def process_image(image, f, *args):
rows, cols, copy = dup(image)
for i in range(rows):
for j in range(cols):
copy[i][j] = process_pixel(image[i][j], f, args)
return copy
###########################################################
def rotate_left(image):
rows, cols, copy = dup(image)
for i in range(rows):
for j in range(cols):
copy[j][i] = image[i][j]
return copy
def rotate_right(image):
rows, cols, copy = dup(image)
for i in range(rows):
for j in range(cols):
copy[j][cols - i - 1] = image[i][j]
return copy
def invert(image):
def invert_comp(p, x):
return 255 - p
return process_image(image, invert_comp)
def lighten(image, x):
def lighten_comp(p, x):
return p + x[0]*(255 - p)
return process_image(image, lighten_comp, x)
def darken(image, x):
def lighten_comp(p, x):
return p - x[0]*(p - 0)
return process_image(image, lighten_comp, x)
def create_histogram(image):
reds = defaultdict(lambda: 0)
greens = defaultdict(lambda: 0)
blues = defaultdict(lambda: 0)
#we don't need a copy this time (:
rows = len(image)
cols = len(image[0])
for i in range(rows):
for j in range(cols):
pixel = image[i][j]
reds [pixel[0]] += 1
greens[pixel[1]] += 1
blues [pixel[2]] += 1
return { "red": reds, "green": greens, "blue": blues }

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

........FE.E
======================================================================
ERROR: test_rotate_left_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
IndexError: list index out of range

======================================================================
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
IndexError: list index out of range

======================================================================
FAIL: test_rotate_left (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) != (255, 0, 0)

First differing element 0:
0
255

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

----------------------------------------------------------------------
Ran 12 tests in 0.086s

FAILED (failures=1, errors=2)

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

Бисер обнови решението на 12.03.2016 09:43 (преди над 5 години)

+from collections import defaultdict
+
+def dup(image):
+ rows = len(image)
+ cols = len(image[0])
+ #couldn't import copy for some reason ?!
+ copy = [ [ image[x][y] for y in range(cols) ] for x in range(rows) ]
+ return rows, cols, copy
+
+def process_pixel(p, f, args):
+ return int(f(p[0], args)), int(f(p[1], args)), int(f(p[2], args))
+
+def process_image(image, f, *args):
+ rows, cols, copy = dup(image)
+ for i in range(rows):
+ for j in range(cols):
+ copy[i][j] = process_pixel(image[i][j], f, args)
+ return copy
+
+###########################################################
+
+def rotate_left(image):
+ rows, cols, copy = dup(image)
+ for i in range(rows):
+ for j in range(cols):
+ copy[j][i] = image[i][j]
+ return copy
+
+def rotate_right(image):
+ rows, cols, copy = dup(image)
+ for i in range(rows):
+ for j in range(cols):
+ copy[j][cols - i - 1] = image[i][j]
+ return copy
+
+def invert(image):
+ def invert_comp(p, x):
+ return 255 - p
+ return process_image(image, invert_comp)
+
+def lighten(image, x):
+ def lighten_comp(p, x):
+ return p + x[0]*(255 - p)
+ return process_image(image, lighten_comp, x)
+
+def darken(image, x):
+ def lighten_comp(p, x):
+ return p - x[0]*(p - 0)
+ return process_image(image, lighten_comp, x)
+
+def create_histogram(image):
+ reds = defaultdict(lambda: 0)
+ greens = defaultdict(lambda: 0)
+ blues = defaultdict(lambda: 0)
+
+ #we don't need a copy this time (:
+ rows = len(image)
+ cols = len(image[0])
+
+ for i in range(rows):
+ for j in range(cols):
+ pixel = image[i][j]
+ reds [pixel[0]] += 1
+ greens[pixel[1]] += 1
+ blues [pixel[2]] += 1
+
+ return { "red": reds, "green": greens, "blue": blues }
  • Как се опита да импортнеш copy?
  • Има функция, която може да ти спести тези два for-а в rotate_* и ще ти помогне да намериш грешката в едната от двете функции :)
  • В create_histogram дефинираш rows и cols, а ги ползваш само на едно място
  • Допуснал си стилистични грешки. Мини пак през PEP8