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

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

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

Резултати

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

Код

from collections import defaultdict
def transpose(image):
transposed = [[image[col][row] for col in range(len(image))]
for row in range(len(image[0]))]
return transposed
def rotate_right(image):
rotated_image = transpose(image)
rotated_image = [rotated_image[row][::-1]
for row in range(len(rotated_image))]
return rotated_image
def rotate_left(image):
rotated_image = [image[col][::-1] for col in range(len(image))]
rotated_image = transpose(rotated_image)
return rotated_image
def invert_pixel(pixel):
pixel = (255 - pixel[0], 255 - pixel[1], 255 - pixel[2])
return pixel
def invert(image):
inverted_image = [[invert_pixel(image[row][col])
for col in range(len(image[0]))]
for row in range(len(image))]
return inverted_image
def lighten_pixel(pixel, coef):
pixel = (pixel[0] + int(coef * (255 - pixel[0])), pixel[1] +
int(coef * (255 - pixel[1])), pixel[2] +
int(coef * (255 - pixel[2])))
return pixel
def lighten(image, coef):
light_image = [[lighten_pixel(image[row][col], coef)
for col in range(len(image[0]))]
for row in range(len(image))]
return light_image
def darken_pixel(pixel, coef):
pixel = (pixel[0] - int(coef * (pixel[0] - 0)), pixel[1] -
int(coef * (pixel[1] - 0)), pixel[2] - int(coef * (pixel[2] - 0)))
return pixel
def darken(image, coef):
dark_image = [[darken_pixel(image[row][col], coef)
for col in range(len(image[0]))]
for row in range(len(image))]
return dark_image
def create_histogram(image):
histogram = {'red': defaultdict(int), 'green': defaultdict(int),
'blue': defaultdict(int)}
for row in range(len(image)):
for col in range(len(image[0])):
pixel = image[row][col]
histogram['red'][pixel[0]] += 1
histogram['green'][pixel[1]] += 1
histogram['blue'][pixel[2]] += 1
return histogram

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

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

OK

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

Борис обнови решението на 10.03.2016 00:41 (преди над 5 години)

+def transpose(image):
+ transposed = [[image[col][row]
+ for col in range(len(image))] for row in range(len(image[0]))]
+ return transposed
+
+
+def rotate_right(image):
+ rotated_image = transpose(image)
+ rotated_image = [rotated_image[row][::-1]
+ for row in range(len(rotated_image))]
+ return rotated_image
+
+
+def rotate_left(image):
+ rotated_image = [image[col][::-1] for col in range(len(image))]
+ rotated_image = transpose(rotated_image)
+ return rotated_image
+
+
+def invert_pixel(pixel):
+ pixel = (255 - pixel[0], 255 - pixel[1], 255 - pixel[2])
+ return pixel
+
+
+def invert(image):
+ inverted_image = [[invert_pixel(image[row][col])
+ for col in range(len(image[0]))] for row in range(len(image))]
+ return inverted_image
+
+
+def lighten_pixel(pixel, coef):
+ pixel = (pixel[0] + int(coef * (255 - pixel[0])), pixel[1] +
+ int(coef * (255 - pixel[1])), pixel[2] + int(coef * (255 - pixel[2])))
+ return pixel
+
+
+def lighten(image, coef):
+ light_image = [[lighten_pixel(image[row][col], coef)
+ for col in range(len(image[0]))] for row in range(len(image))]
+ return light_image
+
+
+def darken_pixel(pixel, coef):
+ pixel = (pixel[0] - int(coef * (pixel[0] - 0)), pixel[1] -
+ int(coef * (pixel[1] - 0)), pixel[2] - int(coef * (pixel[2] - 0)))
+ return pixel
+
+
+def darken(image, coef):
+ dark_image = [[darken_pixel(image[row][col], coef)
+ for col in range(len(image[0]))] for row in range(len(image))]
+ return dark_image
+
+
+def add_key(key, dict_):
+ if key in dict_:
+ dict_[key] += 1
+ else:
+ dict_[key] = 1
+
+
+def create_histogram(image):
+ histogram = {'red': {}, 'green': {}, 'blue': {}}
+ for row in range(len(image)):
+ for col in range(len(image[0])):
+ pixel = (image[row][col])
+ add_key(pixel[0], histogram['red'])
+ add_key(pixel[1], histogram['green'])
+ add_key(pixel[2], histogram['blue'])
+ return histogram

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

+from collections import defaultdict
+
+
def transpose(image):
- transposed = [[image[col][row]
- for col in range(len(image))] for row in range(len(image[0]))]
+ transposed = [[image[col][row] for col in range(len(image))]
+ for row in range(len(image[0]))]
return transposed
def rotate_right(image):
rotated_image = transpose(image)
rotated_image = [rotated_image[row][::-1]
for row in range(len(rotated_image))]
return rotated_image
def rotate_left(image):
rotated_image = [image[col][::-1] for col in range(len(image))]
rotated_image = transpose(rotated_image)
return rotated_image
def invert_pixel(pixel):
pixel = (255 - pixel[0], 255 - pixel[1], 255 - pixel[2])
return pixel
def invert(image):
inverted_image = [[invert_pixel(image[row][col])
- for col in range(len(image[0]))] for row in range(len(image))]
+ for col in range(len(image[0]))]
+ for row in range(len(image))]
return inverted_image
def lighten_pixel(pixel, coef):
pixel = (pixel[0] + int(coef * (255 - pixel[0])), pixel[1] +
- int(coef * (255 - pixel[1])), pixel[2] + int(coef * (255 - pixel[2])))
+ int(coef * (255 - pixel[1])), pixel[2] +
+ int(coef * (255 - pixel[2])))
return pixel
def lighten(image, coef):
light_image = [[lighten_pixel(image[row][col], coef)
- for col in range(len(image[0]))] for row in range(len(image))]
+ for col in range(len(image[0]))]
+ for row in range(len(image))]
return light_image
def darken_pixel(pixel, coef):
pixel = (pixel[0] - int(coef * (pixel[0] - 0)), pixel[1] -
int(coef * (pixel[1] - 0)), pixel[2] - int(coef * (pixel[2] - 0)))
return pixel
def darken(image, coef):
dark_image = [[darken_pixel(image[row][col], coef)
- for col in range(len(image[0]))] for row in range(len(image))]
+ for col in range(len(image[0]))]
+ for row in range(len(image))]
return dark_image
-def add_key(key, dict_):
- if key in dict_:
- dict_[key] += 1
- else:
- dict_[key] = 1
-
-
def create_histogram(image):
- histogram = {'red': {}, 'green': {}, 'blue': {}}
+ histogram = {'red': defaultdict(int), 'green': defaultdict(int),
+ 'blue': defaultdict(int)}
for row in range(len(image)):
for col in range(len(image[0])):
- pixel = (image[row][col])
- add_key(pixel[0], histogram['red'])
- add_key(pixel[1], histogram['green'])
- add_key(pixel[2], histogram['blue'])
+ pixel = image[row][col]
+ histogram['red'][pixel[0]] += 1
+ histogram['green'][pixel[1]] += 1
+ histogram['blue'][pixel[2]] += 1
return histogram