Решение на Работа с картинки от Веселин Иванов

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

Към профила на Веселин Иванов

Резултати

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

Код

def rotate_left(image):
number_of_rows = len(image)
number_of_columns = len(image[0])
result = [[] for i in range(number_of_columns)]
for row in range(number_of_rows):
for column in range(number_of_columns):
result[number_of_columns - 1 - column].append(image[row][column])
return result
def rotate_right(image):
number_of_rows = len(image)
number_of_columns = len(image[0])
result = [[] for i in range(number_of_columns)]
for row in range(number_of_rows - 1, -1, -1):
for column in range(number_of_columns):
result[column].append(image[row][column])
return result
def map_pixels(image, op):
result = []
current_row = []
for row in image:
for pixel in row:
new_pixel = tuple(map(op, pixel))
current_row.append(new_pixel)
result.append(current_row)
current_row = []
return result
def invert(image):
return map_pixels(image, lambda x: 255 - x)
def lighten(image, p):
return map_pixels(image, lambda x: x + int(p * (255 - x)))
def darken(image, p):
return map_pixels(image, lambda x: x - int(p * x))
def create_histogram(image):
result = {'red': {}, 'green': {}, 'blue': {}}
for row in image:
for pixel in row:
r, g, b = pixel
if r in result['red']:
result['red'][r] += 1
else:
result['red'][r] = 1
if g in result['green']:
result['green'][g] += 1
else:
result['green'][g] = 1
if b in result['blue']:
result['blue'][b] += 1
else:
result['blue'][b] = 1
return result

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.084s

OK

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

Веселин обнови решението на 14.03.2016 00:00 (преди над 5 години)

+def rotate_left(image):
+ number_of_rows = len(image)
+ number_of_columns = len(image[0])
+ result = [[] for i in range(number_of_columns)]
+
+ for row in range(number_of_rows):
+ for column in range(number_of_columns):
+ result[number_of_columns - 1 - column].append(image[row][column])
+
+ return result
+
+
+def rotate_right(image):
+ number_of_rows = len(image)
+ number_of_columns = len(image[0])
+ result = [[] for i in range(number_of_columns)]
+
+ for row in range(number_of_rows - 1, -1, -1):
+ for column in range(number_of_columns):
+ result[column].append(image[row][column])
+
+ return result
+
+
+def invert(image):
+ result = []
+ current_row = []
+
+ for row in image:
+ for pixel in row:
+ r, g, b = pixel
+ current_row.append((255 - r, 255 - g, 255 - b))
+ result.append(current_row)
+ current_row = []
+
+ return result
+
+
+def lighten(image, p):
+ result = []
+ current_row = []
+
+ for row in image:
+ for pixel in row:
+ lightened = map(lambda x: x + int(p * (255 - x)), pixel)
+ current_row.append(tuple(lightened))
+ result.append(current_row)
+ current_row = []
+
+ return result
+
+
+def darken(image, p):
+ result = []
+ current_row = []
+
+ for row in image:
+ for pixel in row:
+ lightened = map(lambda x: x - int(p * x), pixel)
+ current_row.append(tuple(lightened))
+ result.append(current_row)
+ current_row = []
+
+ return result
+
+
+def create_histogram(image):
+ result = {'red': {}, 'green': {}, 'blue': {}}
+ for row in image:
+ for pixel in row:
+ r, g, b = pixel
+ if r in result['red']:
+ result['red'][r] += 1
+ else:
+ result['red'][r] = 1
+
+ if g in result['green']:
+ result['green'][g] += 1
+ else:
+ result['green'][g] = 1
+
+ if b in result['blue']:
+ result['blue'][b] += 1
+ else:
+ result['blue'][b] = 1
+
+ return result

Веселин обнови решението на 14.03.2016 00:04 (преди над 5 години)

def rotate_left(image):
number_of_rows = len(image)
number_of_columns = len(image[0])
result = [[] for i in range(number_of_columns)]
for row in range(number_of_rows):
for column in range(number_of_columns):
result[number_of_columns - 1 - column].append(image[row][column])
return result
def rotate_right(image):
number_of_rows = len(image)
number_of_columns = len(image[0])
result = [[] for i in range(number_of_columns)]
for row in range(number_of_rows - 1, -1, -1):
for column in range(number_of_columns):
result[column].append(image[row][column])
return result
def invert(image):
result = []
current_row = []
for row in image:
for pixel in row:
r, g, b = pixel
current_row.append((255 - r, 255 - g, 255 - b))
result.append(current_row)
current_row = []
return result
def lighten(image, p):
result = []
current_row = []
for row in image:
for pixel in row:
lightened = map(lambda x: x + int(p * (255 - x)), pixel)
current_row.append(tuple(lightened))
result.append(current_row)
current_row = []
return result
def darken(image, p):
result = []
current_row = []
for row in image:
for pixel in row:
- lightened = map(lambda x: x - int(p * x), pixel)
- current_row.append(tuple(lightened))
+ darkened = map(lambda x: x - int(p * x), pixel)
+ current_row.append(tuple(darkened))
result.append(current_row)
current_row = []
return result
def create_histogram(image):
result = {'red': {}, 'green': {}, 'blue': {}}
for row in image:
for pixel in row:
r, g, b = pixel
if r in result['red']:
result['red'][r] += 1
else:
result['red'][r] = 1
if g in result['green']:
result['green'][g] += 1
else:
result['green'][g] = 1
if b in result['blue']:
result['blue'][b] += 1
else:
result['blue'][b] = 1
return result

Веселин обнови решението на 14.03.2016 00:40 (преди над 5 години)

def rotate_left(image):
number_of_rows = len(image)
number_of_columns = len(image[0])
result = [[] for i in range(number_of_columns)]
for row in range(number_of_rows):
for column in range(number_of_columns):
result[number_of_columns - 1 - column].append(image[row][column])
return result
def rotate_right(image):
number_of_rows = len(image)
number_of_columns = len(image[0])
result = [[] for i in range(number_of_columns)]
for row in range(number_of_rows - 1, -1, -1):
for column in range(number_of_columns):
result[column].append(image[row][column])
return result
-def invert(image):
+def map_pixels(image, op):
result = []
current_row = []
for row in image:
for pixel in row:
- r, g, b = pixel
- current_row.append((255 - r, 255 - g, 255 - b))
+ new_pixel = tuple(map(op, pixel))
+ current_row.append(new_pixel)
result.append(current_row)
current_row = []
return result
-def lighten(image, p):
- result = []
- current_row = []
+def invert(image):
+ return map_pixels(image, lambda x: 255 - x)
- for row in image:
- for pixel in row:
- lightened = map(lambda x: x + int(p * (255 - x)), pixel)
- current_row.append(tuple(lightened))
- result.append(current_row)
- current_row = []
- return result
+def lighten(image, p):
+ return map_pixels(image, lambda x: x + int(p * (255 - x)))
def darken(image, p):
- result = []
- current_row = []
-
- for row in image:
- for pixel in row:
- darkened = map(lambda x: x - int(p * x), pixel)
- current_row.append(tuple(darkened))
- result.append(current_row)
- current_row = []
-
- return result
+ return map_pixels(image, lambda x: x - int(p * x))
def create_histogram(image):
result = {'red': {}, 'green': {}, 'blue': {}}
for row in image:
for pixel in row:
r, g, b = pixel
if r in result['red']:
result['red'][r] += 1
else:
result['red'][r] = 1
if g in result['green']:
result['green'][g] += 1
else:
result['green'][g] = 1
if b in result['blue']:
result['blue'][b] += 1
else:
result['blue'][b] = 1
return result