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

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

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

Резултати

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

Код

from collections import Counter
def rotate_left(image):
return list(reversed(list(zip(*image))))
def rotate_right(image):
return list(zip(*reversed(image)))
def invert(image):
return [list(map(lambda x: (255 - x[0], 255 - x[1], 255 - x[2]), row))
for row in image]
def lighten(image, coef):
return [list(map(
lambda x: (x[0] + int(coef*(255 - x[0])),
x[1] + int(coef*(255 - x[1])),
x[2] + int(coef*(255 - x[2]))), row)) for row in image]
def darken(image, coef):
return [list(map(
lambda x: (x[0] - int(coef*(x[0] - 0)),
x[1] - int(coef*(x[1] - 0)),
x[2] - int(coef*(x[2] - 0))), row)) for row in image]
def create_histogram(image):
color_indice = [('red', 0), ('green', 1), ('blue', 2)]
pixels = [pixel for row in image for pixel in row]
histogram = {color: dict(d) for color, index in color_indice
for d in [Counter([elem[index] for elem in pixels])]}
return histogram

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.089s

OK

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

Кристофър обнови решението на 08.03.2016 23:26 (преди над 5 години)

+from collections import defaultdict
+
+
+def rotate_left(image):
+ rotated_image = []
+ height = len(image)
+ width = len(image[0])
+
+ j = width - 1
+ while j >= 0:
+ rotated_image.append([])
+ i = 0
+ while i < height:
+ rotated_image[-1].append(image[i][j])
+ i = i + 1
+ j = j - 1
+
+ return rotated_image
+
+
+def rotate_right(image):
+ rotated_image = []
+ height = len(image)
+ width = len(image[0])
+
+ j = 0
+ while j < width:
+ rotated_image.append([])
+ i = height - 1
+ while i >= 0:
+ rotated_image[-1].append(image[i][j])
+ i = i - 1
+ j = j + 1
+
+ return rotated_image
+
+
+def invert(image):
+ inverted_image = []
+
+ for row in image:
+ inverted_image.append([])
+ for pixel in row:
+ r, g, b = pixel
+ inverted_image[-1].append((255 - r, 255 - g, 255 - b))
+
+ return inverted_image
+
+
+def lighten(image, coef):
+ lightened_image = []
+
+ for row in image:
+ lightened_image.append([])
+ for pixel in row:
+ r, g, b = pixel
+ lightened_image[-1].append((
+ (r + int(coef*(255 - r))),
+ (g + int(coef*(255 - g))),
+ (b + int(coef*(255 - b)))
+ ))
+
+ return lightened_image
+
+
+def darken(image, coef):
+ darkened_image = []
+
+ for row in image:
+ darkened_image.append([])
+ for pixel in row:
+ r, g, b = pixel
+ darkened_image[-1].append((
+ (r - int(coef*(r - 0))),
+ (g - int(coef*(g - 0))),
+ (b - int(coef*(b - 0)))
+ ))
+
+ return darkened_image
+
+
+def create_histogram(image):
+ histogram = {
+ 'red': defaultdict(int),
+ 'green': defaultdict(int),
+ 'blue': defaultdict(int)
+ }
+
+ for row in image:
+ for pixel in row:
+ r, g, b = pixel
+ histogram['red'][r] = histogram['red'][r] + 1
+ histogram['green'][g] = histogram['green'][g] + 1
+ histogram['blue'][b] = histogram['blue'][b] + 1
+
+ return histogram

Кристофър обнови решението на 09.03.2016 17:49 (преди над 5 години)

-from collections import defaultdict
+from collections import Counter
def rotate_left(image):
- rotated_image = []
- height = len(image)
- width = len(image[0])
+ return list(reversed(list(zip(*image))))
- j = width - 1
- while j >= 0:
- rotated_image.append([])
- i = 0
- while i < height:
- rotated_image[-1].append(image[i][j])
- i = i + 1
- j = j - 1
- return rotated_image
-
-
def rotate_right(image):
- rotated_image = []
- height = len(image)
- width = len(image[0])
+ return list(zip(*reversed(image)))
- j = 0
- while j < width:
- rotated_image.append([])
- i = height - 1
- while i >= 0:
- rotated_image[-1].append(image[i][j])
- i = i - 1
- j = j + 1
- return rotated_image
-
-
def invert(image):
- inverted_image = []
+ return [list(map(lambda x: (255 - x[0], 255 - x[1], 255 - x[2]), row))
+ for row in image]
- for row in image:
- inverted_image.append([])
- for pixel in row:
- r, g, b = pixel
- inverted_image[-1].append((255 - r, 255 - g, 255 - b))
- return inverted_image
-
-
def lighten(image, coef):
- lightened_image = []
+ return [list(map(
+ lambda x: (x[0] + int(coef*(255 - x[0])),
+ x[1] + int(coef*(255 - x[1])),
+ x[2] + int(coef*(255 - x[2]))), row)) for row in image]
- for row in image:
- lightened_image.append([])
- for pixel in row:
- r, g, b = pixel
- lightened_image[-1].append((
- (r + int(coef*(255 - r))),
- (g + int(coef*(255 - g))),
- (b + int(coef*(255 - b)))
- ))
- return lightened_image
-
-
def darken(image, coef):
- darkened_image = []
+ return [list(map(
+ lambda x: (x[0] - int(coef*(x[0] - 0)),
+ x[1] - int(coef*(x[1] - 0)),
+ x[2] - int(coef*(x[2] - 0))), row)) for row in image]
- for row in image:
- darkened_image.append([])
- for pixel in row:
- r, g, b = pixel
- darkened_image[-1].append((
- (r - int(coef*(r - 0))),
- (g - int(coef*(g - 0))),
- (b - int(coef*(b - 0)))
- ))
- return darkened_image
-
-
def create_histogram(image):
- histogram = {
- 'red': defaultdict(int),
- 'green': defaultdict(int),
- 'blue': defaultdict(int)
- }
-
- for row in image:
- for pixel in row:
- r, g, b = pixel
- histogram['red'][r] = histogram['red'][r] + 1
- histogram['green'][g] = histogram['green'][g] + 1
- histogram['blue'][b] = histogram['blue'][b] + 1
+ color_indice = [('red', 0), ('green', 1), ('blue', 2)]
+ pixels = [pixel for row in image for pixel in row]
+ histogram = {color: dict(d) for color, index in color_indice
+ for d in [Counter([elem[index] for elem in pixels])]}
return histogram