Решение на Работа с картинки от Николай Мантаров

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

Към профила на Николай Мантаров

Резултати

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

Код

from collections import defaultdict
def rotate_left(image):
image1 = [list(col) for col in zip(*image)]
new_image = image1[::-1]
return new_image
def rotate_right(image):
image1 = image[::-1]
new_image = [list(col) for col in zip(*image1)]
return new_image
def pixel(how, pix, coef):
if how == 0:
return (255 - pix[0], 255 - pix[1], 255 - pix[2])
if how == 1:
pix_0 = int(coef * (255 - pix[0]))
pix_1 = int(coef * (255 - pix[1]))
pix_2 = int(coef * (255 - pix[2]))
return (pix[0] + pix_0, pix[1] + pix_1, pix[2] + pix_2)
if how == 2:
a = int((1 - coef) * pix[0])
b = int((1 - coef) * pix[1])
c = int((1 - coef) * pix[2])
return (a, b, c)
def invert(image):
res = [[pixel(0, col, 0) for col in row] for row in image]
return res
def lighten(image, coef):
new_image = [[pixel(1, col, coef) for col in row] for row in image]
return new_image
def darken(image, coef):
new_image = [[pixel(2, col, coef) for col in row] for row in image]
return new_image
def create_histogram(image):
red_clr = defaultdict(int)
green_clr = defaultdict(int)
blue_clr = defaultdict(int)
for row in image:
for red, green, blue in row:
red_clr[red] += 1
green_clr[green] += 1
blue_clr[blue] += 1
histogram = {'red': red_clr, 'green': green_clr, 'blue': blue_clr}
return histogram

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

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

OK

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

Николай обнови решението на 10.03.2016 00:10 (преди над 5 години)

+def rotate_left(image):
+ new_image = []
+ for column in range(len(image[0])-1, -1, -1):
+ to_push = []
+ for row in range(len(image)):
+ to_push.append(image[row][column])
+ new_image.append(to_push)
+ return new_image
+
+
+def rotate_right(image):
+ new_image = []
+ for column in range(len(image[0])):
+ to_push = []
+ for row in range(len(image)-1, -1, -1):
+ to_push.append(image[row][column])
+ new_image.append(to_push)
+ return new_image
+
+
+def invert(image):
+ new_image = []
+ for row in image:
+ to_push = []
+ for element in row:
+ a = (255 - element[0], 255 - element[1], 255 - element[2])
+ to_push.append(a)
+ new_image.append(to_push)
+ return new_image
+
+
+def lighten(image, coef):
+ new_image = []
+ for row in image:
+ to_push = []
+ for element in row:
+ a = int(element[0] + coef*(255 - element[0]))
+ b = int(element[1] + coef*(255 - element[1]))
+ c = int(element[2] + coef*(255 - element[2]))
+ to_push.append((a, b, c))
+ new_image.append(to_push)
+ return new_image
+
+
+def darken(image, coef):
+ new_image = []
+ for row in image:
+ to_push = []
+ for element in row:
+ a = int((1-coef) * element[0])
+ b = int((1-coef) * element[1])
+ c = int((1-coef) * element[2])
+ to_push.append((a, b, c))
+ new_image.append(to_push)
+ return new_image
+
+
+def create_histogram(image):
+ histogram = {'red': {}, 'green': {}, 'blue': {}}
+ for row in image:
+ for color in row:
+ red = color[0]
+ green = color[1]
+ blue = color[2]
+ if red in histogram['red']:
+ histogram['red'][red] += 1
+ else:
+ histogram['red'][red] = 1
+ if green in histogram['green']:
+ histogram['green'][green] += 1
+ else:
+ histogram['green'][green] = 1
+ if blue in histogram['blue']:
+ histogram['blue'][blue] += 1
+ else:
+ histogram['blue'][blue] = 1
+ return histogram

Николай обнови решението на 12.03.2016 21:03 (преди над 5 години)

+from collections import defaultdict
+
+
def rotate_left(image):
- new_image = []
- for column in range(len(image[0])-1, -1, -1):
- to_push = []
- for row in range(len(image)):
- to_push.append(image[row][column])
- new_image.append(to_push)
+ image1 = [list(col) for col in zip(*image)]
+ new_image = image1[::-1]
return new_image
def rotate_right(image):
- new_image = []
- for column in range(len(image[0])):
- to_push = []
- for row in range(len(image)-1, -1, -1):
- to_push.append(image[row][column])
- new_image.append(to_push)
+ image1 = image[::-1]
+ new_image = [list(col) for col in zip(*image1)]
return new_image
+def pixel(how, pix, coef):
+ if how == 0:
+ return (255 - pix[0], 255 - pix[1], 255 - pix[2])
+ if how == 1:
+ pix_0 = int(coef * (255 - pix[0]))
+ pix_1 = int(coef * (255 - pix[1]))
+ pix_2 = int(coef * (255 - pix[2]))
+ return (pix[0] + pix_0, pix[1] + pix_1, pix[2] + pix_2)
+ if how == 2:
+ a = int((1 - coef) * pix[0])
+ b = int((1 - coef) * pix[1])
+ c = int((1 - coef) * pix[2])
+ return (a, b, c)
+
+
def invert(image):
- new_image = []
- for row in image:
- to_push = []
- for element in row:
- a = (255 - element[0], 255 - element[1], 255 - element[2])
- to_push.append(a)
- new_image.append(to_push)
- return new_image
+ res = [[pixel(0, col, 0) for col in row] for row in image]
+ return res
def lighten(image, coef):
- new_image = []
- for row in image:
- to_push = []
- for element in row:
- a = int(element[0] + coef*(255 - element[0]))
- b = int(element[1] + coef*(255 - element[1]))
- c = int(element[2] + coef*(255 - element[2]))
- to_push.append((a, b, c))
- new_image.append(to_push)
+ new_image = [[pixel(1, col, coef) for col in row] for row in image]
return new_image
def darken(image, coef):
- new_image = []
- for row in image:
- to_push = []
- for element in row:
- a = int((1-coef) * element[0])
- b = int((1-coef) * element[1])
- c = int((1-coef) * element[2])
- to_push.append((a, b, c))
- new_image.append(to_push)
+ new_image = [[pixel(2, col, coef) for col in row] for row in image]
return new_image
def create_histogram(image):
- histogram = {'red': {}, 'green': {}, 'blue': {}}
+ red_clr = defaultdict(int)
+ green_clr = defaultdict(int)
+ blue_clr = defaultdict(int)
for row in image:
- for color in row:
- red = color[0]
- green = color[1]
- blue = color[2]
- if red in histogram['red']:
- histogram['red'][red] += 1
- else:
- histogram['red'][red] = 1
- if green in histogram['green']:
- histogram['green'][green] += 1
- else:
- histogram['green'][green] = 1
- if blue in histogram['blue']:
- histogram['blue'][blue] += 1
- else:
- histogram['blue'][blue] = 1
+ for red, green, blue in row:
+ red_clr[red] += 1
+ green_clr[green] += 1
+ blue_clr[blue] += 1
+ histogram = {'red': red_clr, 'green': green_clr, 'blue': blue_clr}
return histogram