Решение на Работа с картинки от Андрей Иванов

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

Към профила на Андрей Иванов

Резултати

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

Код

import collections
def rotate_left(image):
colN = len(image[0])
res = [[row[i] for row in image] for i in range(colN - 1, -1, -1)]
return res
def rotate_right(image):
rows = len(image)
colN = len(image[0])
res = [[image[j][i] for j in range(rows - 1, -1, -1)] for i in range(colN)]
return res
def invertHelp(pixel):
return (255 - pixel[0], 255 - pixel[1], 255 - pixel[2])
def invert(image):
colN = len(image[0])
res = [[invertHelp(pixel) for pixel in row] for row in image]
return res
def darkenHelp(pixel, number):
r = int((1-number) * pixel[0])
g = int((1-number) * pixel[1])
b = int((1-number) * pixel[2])
return (r, g, b)
def darken(image, number):
res = [[darkenHelp(pixel, number) for pixel in row] for row in image]
return res
def lightenHelp(pixel, number):
r = int(pixel[0] + number * (255 - pixel[0]))
g = int(pixel[1] + number * (255 - pixel[1]))
b = int(pixel[2] + number * (255 - pixel[2]))
return (r, g, b)
def lighten(image, number):
res = [[lightenHelp(pixel, number) for pixel in row] for row in image]
return res
def create_histogram(image):
redD = collections.defaultdict(int)
greenD = collections.defaultdict(int)
blueD = collections.defaultdict(int)
for row in image:
for pixel in row:
redD[pixel[0]] += 1
greenD[pixel[1]] += 1
blueD[pixel[2]] += 1
res = {'red': redD, 'green': greenD, 'blue': blueD}
return res

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.112s

OK

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

Андрей обнови решението на 10.03.2016 01:40 (преди над 5 години)

+def rotate_left(image):
+ result = []
+ new = []
+ rows = len(image)
+ columns = len(image[0])
+ for i in range(columns-1, -1, -1):
+ for j in range(0, rows):
+ new.append(image[j][i])
+ result.append(new)
+ new = []
+ return result
+
+
+def rotate_right(image):
+ result = []
+ new = []
+ rows = len(image)
+ columns = len(image[0])
+ for i in range(0, columns):
+ for j in range(rows-1, -1, -1):
+ new.append(image[j][i])
+ result.append(new)
+ new = []
+ return result
+
+
+def invert(image):
+ rows = len(image)
+ columns = len(image[0])
+ result = []
+ new = []
+ for i in range(0, rows):
+ for j in range(0, columns):
+ r = 255 - image[i][j][0]
+ g = 255 - image[i][j][1]
+ b = 255 - image[i][j][2]
+ new.append((r, g, b))
+ result.append(new)
+ new = []
+ return result
+
+
+def darken(image, number):
+ rows = len(image)
+ columns = len(image[0])
+ result = []
+ new = []
+ for i in range(0, rows):
+ for j in range(0, columns):
+ r = int(image[i][j][0]-number*(image[i][j][0]-0))
+ g = int(image[i][j][1]-number*(image[i][j][1]-0))
+ b = int(image[i][j][2]-number*(image[i][j][2]-0))
+ new.append((r, g, b))
+ result.append(new)
+ new = []
+ return result
+
+
+def lighten(image, number):
+ rows = len(image)
+ columns = len(image[0])
+ result = []
+ new = []
+ for i in range(0, rows):
+ for j in range(0, columns):
+ r = int(image[i][j][0]+number*(255-image[i][j][0]))
+ g = int(image[i][j][1]+number*(255-image[i][j][1]))
+ b = int(image[i][j][2]+number*(255-image[i][j][2]))
+ new.append((r, g, b))
+ result.append(new)
+ new = []
+ return result
+
+
+def member(list, number):
+ n = len(list)
+ for i in range(0, n):
+ if number == list[i][0]:
+ return i
+ return -1
+
+
+def create_histogram(image):
+ red = []
+ green = []
+ blue = []
+ rows = len(image)
+ columns = len(image[0])
+ for i in range(0, rows):
+ for j in range(0, columns):
+ indexRed = member(red, image[i][j][0])
+ if indexRed != -1:
+ red[indexRed] = (image[i][j][0], red[indexRed][1] + 1)
+ else:
+ red.append((image[i][j][0], 1))
+ indexGreen = member(green, image[i][j][1])
+ if indexGreen != -1:
+ green[indexGreen] = (image[i][j][1], green[indexGreen][1] + 1)
+ else:
+ green.append((image[i][j][1], 1))
+ indexBlue = member(blue, image[i][j][2])
+ if indexBlue != -1:
+ blue[indexBlue] = (image[i][j][2], blue[indexBlue][1] + 1)
+ else:
+ blue.append((image[i][j][2], 1))
+ redD = dict(red)
+ greenD = dict(green)
+ blueD = dict(blue)
+ result = {'red': redD, 'green': greenD, 'blue': blueD}
+ return result

Андрей обнови решението на 11.03.2016 18:17 (преди над 5 години)

+import collections
+
+
def rotate_left(image):
- result = []
- new = []
- rows = len(image)
- columns = len(image[0])
- for i in range(columns-1, -1, -1):
- for j in range(0, rows):
- new.append(image[j][i])
- result.append(new)
- new = []
- return result
+ colN = len(image[0])
+ res = [[row[i] for row in image] for i in range(colN - 1, -1, -1)]
+ return res
def rotate_right(image):
- result = []
- new = []
rows = len(image)
- columns = len(image[0])
- for i in range(0, columns):
- for j in range(rows-1, -1, -1):
- new.append(image[j][i])
- result.append(new)
- new = []
- return result
+ colN = len(image[0])
+ res = [[image[j][i] for j in range(rows - 1, -1, -1)] for i in range(colN)]
+ return res
+def invertHelp(pixel):
+ return (255 - pixel[0], 255 - pixel[1], 255 - pixel[2])
+
+
def invert(image):
- rows = len(image)
- columns = len(image[0])
- result = []
- new = []
- for i in range(0, rows):
- for j in range(0, columns):
- r = 255 - image[i][j][0]
- g = 255 - image[i][j][1]
- b = 255 - image[i][j][2]
- new.append((r, g, b))
- result.append(new)
- new = []
- return result
+ colN = len(image[0])
+ res = [[invertHelp(pixel) for pixel in row] for row in image]
+ return res
+def darkenHelp(pixel, number):
+ r = int((1-number) * pixel[0])
+ g = int((1-number) * pixel[1])
+ b = int((1-number) * pixel[2])
+ return (r, g, b)
+
+
def darken(image, number):
- rows = len(image)
- columns = len(image[0])
- result = []
- new = []
- for i in range(0, rows):
- for j in range(0, columns):
- r = int(image[i][j][0]-number*(image[i][j][0]-0))
- g = int(image[i][j][1]-number*(image[i][j][1]-0))
- b = int(image[i][j][2]-number*(image[i][j][2]-0))
- new.append((r, g, b))
- result.append(new)
- new = []
- return result
+ res = [[darkenHelp(pixel, number) for pixel in row] for row in image]
+ return res
-def lighten(image, number):
- rows = len(image)
- columns = len(image[0])
- result = []
- new = []
- for i in range(0, rows):
- for j in range(0, columns):
- r = int(image[i][j][0]+number*(255-image[i][j][0]))
- g = int(image[i][j][1]+number*(255-image[i][j][1]))
- b = int(image[i][j][2]+number*(255-image[i][j][2]))
- new.append((r, g, b))
- result.append(new)
- new = []
- return result
+def lightenHelp(pixel, number):
+ r = int(pixel[0] + number * (255 - pixel[0]))
+ g = int(pixel[1] + number * (255 - pixel[1]))
+ b = int(pixel[2] + number * (255 - pixel[2]))
+ return (r, g, b)
-def member(list, number):
- n = len(list)
- for i in range(0, n):
- if number == list[i][0]:
- return i
- return -1
+def lighten(image, number):
+ res = [[lightenHelp(pixel, number) for pixel in row] for row in image]
+ return res
def create_histogram(image):
- red = []
- green = []
+ redD = collections.defaultdict(int)
- blue = []
+ greenD = collections.defaultdict(int)
- rows = len(image)
+ blueD = collections.defaultdict(int)
- columns = len(image[0])
+ for row in image:
- for i in range(0, rows):
+ for pixel in row:
- for j in range(0, columns):
+ redD[pixel[0]] += 1
- indexRed = member(red, image[i][j][0])
+ greenD[pixel[1]] += 1
- if indexRed != -1:
+ blueD[pixel[2]] += 1
- red[indexRed] = (image[i][j][0], red[indexRed][1] + 1)
+ res = {'red': redD, 'green': greenD, 'blue': blueD}
- else:
+ return res
- red.append((image[i][j][0], 1))
- indexGreen = member(green, image[i][j][1])
- if indexGreen != -1:
- green[indexGreen] = (image[i][j][1], green[indexGreen][1] + 1)
- else:
- green.append((image[i][j][1], 1))
- indexBlue = member(blue, image[i][j][2])
- if indexBlue != -1:
- blue[indexBlue] = (image[i][j][2], blue[indexBlue][1] + 1)
- else:
- blue.append((image[i][j][2], 1))
- redD = dict(red)
- greenD = dict(green)
- blueD = dict(blue)
- result = {'red': redD, 'green': greenD, 'blue': blueD}
- return result