Решение на Работа с картинки от Йоан Динков

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

Към профила на Йоан Динков

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 7 успешни тест(а)
  • 5 неуспешни тест(а)

Код

def rotate_left(image):
rows, columns = dimensions(image)
result = [[0 for x in range(rows)] for x in range(columns)]
for row in range(rows):
for column in range(columns):
result[row][column] = image[column][rows-row-1]
return result
def rotate_right(image):
rows, columns = dimensions(image)
result = [[0 for x in range(rows)] for x in range(columns)]
for row in range(rows):
for column in range(columns):
result[row][column] = image[columns-column-1][row]
return result
def invert(image):
return process_image(image, None, invert_pixel)
def lighten(image, value):
return process_image(image, value, lighten_pixel)
def darken(image, value):
return process_image(image, value, darken_pixel)
def process_image(image, value, function):
rows, columns = dimensions(image)
result = [[0 for x in range(rows)] for x in range(columns)]
for row in range(rows):
for column in range(columns):
result[row][column] = function(image[row][column], value)
return result
def dimensions(image):
return len(image), len(image[0])
def invert_pixel(pixel, value):
red, green, blue = pixel
return 255 - red, 255 - green, 255 - blue
def lighten_pixel(pixel, value):
red, green, blue = pixel
return (int(red + value * (255 - red)),
int(green + value * (255 - green)),
int(blue + value * (255 - blue)))
def darken_pixel(pixel, value):
red, green, blue = pixel
return (int(red - value * (red - 0)),
int(green - value * (green - 0)),
int(blue - value * (blue - 0)))
def create_histogram(image):
result = {
'red': {},
'green': {},
'blue': {}
}
flatten_image = [pixel for row in image for pixel in row]
for pixel in flatten_image:
red, green, blue = pixel
update_dict(result['red'], red, 1)
update_dict(result['green'], green, 1)
update_dict(result['blue'], blue, 1)
return result
def update_dict(dict, key, value):
if key in dict:
dict[key] += value
else:
dict[key] = value

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

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

======================================================================
ERROR: test_invert_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 assignment index out of range

======================================================================
ERROR: test_lighten_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 assignment index out of range

======================================================================
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

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

FAILED (errors=5)

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

Йоан обнови решението на 12.03.2016 21:45 (преди над 5 години)

+def rotate_left(image):
+ rows, columns = dimensions(image)
+
+ result = [[0 for x in range(rows)] for x in range(columns)]
+
+ for row in range(rows):
+ for column in range(columns):
+ result[row][column] = image[column][rows-row-1]
+
+ return result
+
+
+def rotate_right(image):
+ rows, columns = dimensions(image)
+
+ result = [[0 for x in range(rows)] for x in range(columns)]
+
+ for row in range(rows):
+ for column in range(columns):
+ result[row][column] = image[columns-column-1][row]
+
+ return result
+
+
+def invert(image):
+ return process_image(image, None, invert_pixel)
+
+
+def lighten(image, value):
+ return process_image(image, value, lighten_pixel)
+
+
+def darken(image, value):
+ return process_image(image, value, darken_pixel)
+
+
+def process_image(image, value, function):
+ rows, columns = dimensions(image)
+
+ result = [[0 for x in range(rows)] for x in range(columns)]
+
+ for row in range(rows):
+ for column in range(columns):
+ result[row][column] = function(image[row][column], value)
+
+ return result
+
+
+def dimensions(image):
+ return len(image), len(image[0])
+
+
+def invert_pixel(pixel, value):
+ red, green, blue = pixel
+
+ return 255 - red, 255 - green, 255 - blue
+
+
+def lighten_pixel(pixel, value):
+ red, green, blue = pixel
+
+ return (int(red + value * (255 - red)),
+ int(green + value * (255 - green)),
+ int(blue + value * (255 - blue)))
+
+
+def darken_pixel(pixel, value):
+ red, green, blue = pixel
+
+ return (int(red - value * (red - 0)),
+ int(green - value * (green - 0)),
+ int(blue - value * (blue - 0)))
+
+
+def create_histogram(image):
+ result = {
+ 'red': {key: 0 for key in range(256)},
+ 'green': {key: 0 for key in range(256)},
+ 'blue': {key: 0 for key in range(256)}
+ }
+
+ flatten_image = [pixel for row in image for pixel in row]
+
+ for pixel in flatten_image:
+ red, green, blue = pixel
+
+ result['red'][red] += 1
+ result['green'][green] += 1
+ result['blue'][blue] += 1
+
+ return result

Йоан обнови решението на 14.03.2016 15:15 (преди над 5 години)

def rotate_left(image):
rows, columns = dimensions(image)
result = [[0 for x in range(rows)] for x in range(columns)]
for row in range(rows):
for column in range(columns):
result[row][column] = image[column][rows-row-1]
return result
def rotate_right(image):
rows, columns = dimensions(image)
result = [[0 for x in range(rows)] for x in range(columns)]
for row in range(rows):
for column in range(columns):
result[row][column] = image[columns-column-1][row]
return result
def invert(image):
return process_image(image, None, invert_pixel)
def lighten(image, value):
return process_image(image, value, lighten_pixel)
def darken(image, value):
return process_image(image, value, darken_pixel)
def process_image(image, value, function):
rows, columns = dimensions(image)
result = [[0 for x in range(rows)] for x in range(columns)]
for row in range(rows):
for column in range(columns):
result[row][column] = function(image[row][column], value)
return result
def dimensions(image):
return len(image), len(image[0])
def invert_pixel(pixel, value):
red, green, blue = pixel
return 255 - red, 255 - green, 255 - blue
def lighten_pixel(pixel, value):
red, green, blue = pixel
return (int(red + value * (255 - red)),
int(green + value * (255 - green)),
int(blue + value * (255 - blue)))
def darken_pixel(pixel, value):
red, green, blue = pixel
return (int(red - value * (red - 0)),
int(green - value * (green - 0)),
int(blue - value * (blue - 0)))
def create_histogram(image):
result = {
'red': {key: 0 for key in range(256)},
'green': {key: 0 for key in range(256)},
'blue': {key: 0 for key in range(256)}
}
flatten_image = [pixel for row in image for pixel in row]
for pixel in flatten_image:
red, green, blue = pixel
- result['red'][red] += 1
- result['green'][green] += 1
- result['blue'][blue] += 1
+ update_dict(result['red'], red, 1)
+ update_dict(result['green'], green, 1)
+ update_dict(result['blue'], blue, 1)
return result
+
+
+def update_dict(dict, key, value):
+ if key in dict:
+ dict[key] += value
+ else:
+ dict[key] = value

Йоан обнови решението на 14.03.2016 15:44 (преди над 5 години)

def rotate_left(image):
rows, columns = dimensions(image)
result = [[0 for x in range(rows)] for x in range(columns)]
for row in range(rows):
for column in range(columns):
result[row][column] = image[column][rows-row-1]
return result
def rotate_right(image):
rows, columns = dimensions(image)
result = [[0 for x in range(rows)] for x in range(columns)]
for row in range(rows):
for column in range(columns):
result[row][column] = image[columns-column-1][row]
return result
def invert(image):
return process_image(image, None, invert_pixel)
def lighten(image, value):
return process_image(image, value, lighten_pixel)
def darken(image, value):
return process_image(image, value, darken_pixel)
def process_image(image, value, function):
rows, columns = dimensions(image)
result = [[0 for x in range(rows)] for x in range(columns)]
for row in range(rows):
for column in range(columns):
result[row][column] = function(image[row][column], value)
return result
def dimensions(image):
return len(image), len(image[0])
def invert_pixel(pixel, value):
red, green, blue = pixel
return 255 - red, 255 - green, 255 - blue
def lighten_pixel(pixel, value):
red, green, blue = pixel
return (int(red + value * (255 - red)),
int(green + value * (255 - green)),
int(blue + value * (255 - blue)))
def darken_pixel(pixel, value):
red, green, blue = pixel
return (int(red - value * (red - 0)),
int(green - value * (green - 0)),
int(blue - value * (blue - 0)))
def create_histogram(image):
result = {
- 'red': {key: 0 for key in range(256)},
- 'green': {key: 0 for key in range(256)},
- 'blue': {key: 0 for key in range(256)}
+ 'red': {},
+ 'green': {},
+ 'blue': {}
}
flatten_image = [pixel for row in image for pixel in row]
for pixel in flatten_image:
red, green, blue = pixel
update_dict(result['red'], red, 1)
update_dict(result['green'], green, 1)
update_dict(result['blue'], blue, 1)
return result
def update_dict(dict, key, value):
if key in dict:
dict[key] += value
else:
dict[key] = value