# Решение на Работа с картинки от Божидар Карааргиров

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

Към профила на Божидар Карааргиров

## Резултати

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

## Код

def rotate_left(image):
"""
Rotate a image by 90 degrees to the left
:param image: an image represented by2 dimensional list.
:return: a rotated imaged, again represented by a 2 dimensional list
"""
rows = len(image) - 1
columns = len(image) - 1
return [[image[column][rows - row]
for column in range(columns + 1)]
for row in range(rows + 1)]
def rotate_right(image):
"""
Rotate a image by 90 degrees to the right
:param image: an image represented by2 dimensional list.
:return: a rotated imaged, again represented by a 2 dimensional list
"""
rows = len(image) - 1
columns = len(image) - 1
return [[image[columns - column][row]
for column in range(columns + 1)]
for row in range(rows + 1)]
def invert(image):
"""
Create a negative of a given image
:param image: an image represented by2 dimensional list.
:return: a rotated imaged, again represented by a 2 dimensional list
"""
return [[(255 - pixel, 255 - pixel, 255 - pixel)
for pixel in row] for row in image]
def lighten(image, percent):
"""
Lighten a picture by X percent
:param image: A image represented by2 dimensional list.
:param percent: Value between 0 and 1.
:return: a rotated imaged, again represented by a 2 dimensional list
"""
def apply_percent(pixel):
return tuple(colour + int(percent*(255 - colour)) for colour in pixel)
return [list(map(apply_percent, row)) for row in image]
def darken(image, percent):
"""
Darken a picture by X percent
:param image: A image represented by2 dimensional list.
:param percent: Value between 0 and 1.
:return: a rotated imaged, again represented by a 2 dimensional list
"""
def apply_percent(pixel):
return tuple(colour - int(percent*colour) for colour in pixel)
return [list(map(apply_percent, row)) for row in image]
def create_histogram(image):
"""
Return histogram data for the occurrence of each base pixel colour
- red blue and green.
:param image: A image represented by2 dimensional list.
:return: Dictionary containing information about the rate
of occurrence of each value from 0 to 255 for each colour
"""
base_keys = ("red", "green", "blue")
histogram = {"red": {}, "blue": {}, "green": {}}
# Very disgusting tree - should optimise if time permits it.
# At least it makes one pass
for row in image:
for pixel in row:
for index in range((len(pixel))):
histogram[base_keys[index]][pixel[index]] = \
histogram[base_keys[index]].get(pixel[index], 0) + 1
return histogram

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

.........E.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

----------------------------------------------------------------------
Ran 12 tests in 0.085s

FAILED (errors=2)

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

### Божидар обнови решението на 12.03.2016 12:56 (преди над 5 години)

+def rotate_left(image):
+ """
+ Rotate a image by 90 degrees to the left
+ :param image: an image represented by2 dimensional list.
+ :return: a rotated imaged, again represented by a 2 dimensional list
+ """
+ rows = len(image) - 1
+ columns = len(image) - 1
+ return [[image[column][rows - row]
+ for column in range(columns + 1)]
+ for row in range(rows + 1)]
+
+
+def rotate_right(image):
+ """
+ Rotate a image by 90 degrees to the right
+ :param image: an image represented by2 dimensional list.
+ :return: a rotated imaged, again represented by a 2 dimensional list
+ """
+ rows = len(image) - 1
+ columns = len(image) - 1
+ return [[image[columns - column][row]
+ for column in range(columns + 1)]
+ for row in range(rows + 1)]
+
+
+def invert(image):
+ """
+ Create a negative of a given image
+ :param image: an image represented by2 dimensional list.
+ :return: a rotated imaged, again represented by a 2 dimensional list
+ """
+ return [[(255 - pixel, 255 - pixel, 255 - pixel)
+ for pixel in row] for row in image]
+
+
+def lighten(image, percent):
+ """
+ Lighten a picture by X percent
+ :param image: A image represented by2 dimensional list.
+ :param percent: Value between 0 and 1.
+ :return: a rotated imaged, again represented by a 2 dimensional list
+ """
+ def apply_percent(pixel):
+ return tuple(val + int(percent*(255 - val)) for val in pixel)
+
+ return [list(map(apply_percent, row)) for row in image]
+
+
+def darken(image, percent):
+ """
+ Darken a picture by X percent
+ :param image: A image represented by2 dimensional list.
+ :param percent: Value between 0 and 1.
+ :return: a rotated imaged, again represented by a 2 dimensional list
+ """
+ def apply_percent(pixel):
+ return tuple(val - int(percent*val) for val in pixel)
+
+ return [list(map(apply_percent, row)) for row in image]
+
+
+def create_histogram(image):
+ """
+ Return histogram data for the occurrence of each base pixel colour
+ - red blue and green.
+ :param image: A image represented by2 dimensional list.
+ :return: Dictionary containing information about the rate
+ of occurrence of each value from 0 to 255 for each colour
+ """
+ base_keys = ("red", "green", "blue")
+ histogram = {"red": {}, "blue": {}, "green": {}}
+ # Very disgusting tree - should optimise if time permits it.
+ # At least it makes one pass
+ for row in image:
+ for pixel in row:
+ for index in range((len(pixel))):
+ if pixel[index] in histogram[base_keys[index]]:
+ histogram[base_keys[index]][pixel[index]] += 1
+ else:
+ histogram[base_keys[index]][pixel[index]] = 1
+ return histogram

### Божидар обнови решението на 13.03.2016 08:45 (преди над 5 години)

def rotate_left(image):
"""
Rotate a image by 90 degrees to the left
:param image: an image represented by2 dimensional list.
:return: a rotated imaged, again represented by a 2 dimensional list
"""
rows = len(image) - 1
columns = len(image) - 1
return [[image[column][rows - row]
for column in range(columns + 1)]
for row in range(rows + 1)]
def rotate_right(image):
"""
Rotate a image by 90 degrees to the right
:param image: an image represented by2 dimensional list.
:return: a rotated imaged, again represented by a 2 dimensional list
"""
rows = len(image) - 1
columns = len(image) - 1
return [[image[columns - column][row]
for column in range(columns + 1)]
for row in range(rows + 1)]
def invert(image):
"""
Create a negative of a given image
:param image: an image represented by2 dimensional list.
:return: a rotated imaged, again represented by a 2 dimensional list
"""
return [[(255 - pixel, 255 - pixel, 255 - pixel)
for pixel in row] for row in image]
def lighten(image, percent):
"""
Lighten a picture by X percent
:param image: A image represented by2 dimensional list.
:param percent: Value between 0 and 1.
:return: a rotated imaged, again represented by a 2 dimensional list
"""
def apply_percent(pixel):
- return tuple(val + int(percent*(255 - val)) for val in pixel)
+ return tuple(colour + int(percent*(255 - colour)) for colour in pixel)
return [list(map(apply_percent, row)) for row in image]
def darken(image, percent):
"""
Darken a picture by X percent
:param image: A image represented by2 dimensional list.
:param percent: Value between 0 and 1.
:return: a rotated imaged, again represented by a 2 dimensional list
"""
def apply_percent(pixel):
- return tuple(val - int(percent*val) for val in pixel)
+ return tuple(colour - int(percent*colour) for colour in pixel)
return [list(map(apply_percent, row)) for row in image]
def create_histogram(image):
"""
Return histogram data for the occurrence of each base pixel colour
- red blue and green.
:param image: A image represented by2 dimensional list.
:return: Dictionary containing information about the rate
of occurrence of each value from 0 to 255 for each colour
"""
base_keys = ("red", "green", "blue")
histogram = {"red": {}, "blue": {}, "green": {}}
# Very disgusting tree - should optimise if time permits it.
# At least it makes one pass
for row in image:
for pixel in row:
for index in range((len(pixel))):
- if pixel[index] in histogram[base_keys[index]]:
- histogram[base_keys[index]][pixel[index]] += 1
- else:
- histogram[base_keys[index]][pixel[index]] = 1
+ histogram[base_keys[index]][pixel[index]] = \
+ histogram[base_keys[index]].get(pixel[index], 0) + 1
return histogram