Решение на Работа с картинки от Данислав Киров

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

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

Резултати

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

Код

def rotate_left(image):
new_image = []
rows = len(image)
columns = len(image[0])
for column in range(columns - 1, -1, -1):
new_row = []
for row in range(rows):
new_row.append(image[row][column])
new_image.append(new_row)
return new_image
def rotate_right(image):
new_image = []
rows = len(image)
columns = len(image[0])
for column in range(columns):
new_row = []
for row in range(rows - 1, -1, -1):
new_row.append(image[row][column])
new_image.append(new_row)
return new_image
def invert(image):
new_image = []
rows = len(image)
columns = len(image[0])
for row in range(rows):
new_row = []
for column in range(columns):
red = image[row][column][0]
green = image[row][column][1]
blue = image[row][column][2]
new_row.append((255 - red, 255 - green, 255 - blue))
new_image.append(new_row)
return new_image
def lighten(image, coef):
new_image = []
rows = len(image)
columns = len(image[0])
for row in range(rows):
new_row = []
for column in range(columns):
red = image[row][column][0]
green = image[row][column][1]
blue = image[row][column][2]
new_red = int(red + coef * (255 - red))
new_green = int(green + coef * (255 - green))
new_blue = int(blue + coef * (255 - blue))
new_row.append((new_red, new_green, new_blue))
new_image.append(new_row)
return new_image
def darken(image, coef):
new_image = []
rows = len(image)
columns = len(image[0])
for row in range(rows):
new_row = []
for column in range(columns):
red = image[row][column][0]
green = image[row][column][1]
blue = image[row][column][2]
new_red = int(red - coef * red)
new_green = int(green - coef * green)
new_blue = int(blue - coef * blue)
new_row.append((new_red, new_green, new_blue))
new_image.append(new_row)
return new_image
def create_histogram(image):
rows = len(image)
columns = len(image[0])
red = {}
green = {}
blue = {}
for row in range(rows):
for column in range(columns):
if image[row][column][0] in red:
red[image[row][column][0]] += 1
else:
red[image[row][column][0]] = 1
if image[row][column][1] in green:
green[image[row][column][1]] += 1
else:
green[image[row][column][1]] = 1
if image[row][column][2] in blue:
blue[image[row][column][2]] += 1
else:
blue[image[row][column][2]] = 1
histogram = {'red': red, 'green': green, 'blue': blue}
return histogram

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.083s

OK

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

Данислав обнови решението на 13.03.2016 14:33 (преди над 5 години)

+def rotate_left(image):
+ new_image = []
+
+ rows = len(image)
+ columns = len(image[0])
+
+ for column in range(columns - 1, -1, -1):
+ new_row = []
+ for row in range(rows):
+ new_row.append(image[row][column])
+ new_image.append(new_row)
+
+ return new_image
+
+
+def rotate_right(image):
+ new_image = []
+
+ rows = len(image)
+ columns = len(image[0])
+
+ for column in range(columns):
+ new_row = []
+ for row in range(rows - 1, -1, -1):
+ new_row.append(image[row][column])
+ new_image.append(new_row)
+
+ return new_image
+
+
+def invert(image):
+ new_image = []
+
+ rows = len(image)
+ columns = len(image[0])
+
+ for row in range(rows):
+ new_row = []
+ for column in range(columns):
+ red = image[row][column][0]
+ green = image[row][column][1]
+ blue = image[row][column][2]
+ new_row.append((255 - red, 255 - green, 255 - blue))
+ new_image.append(new_row)
+
+ return new_image
+
+
+def lighten(image, coef):
+ new_image = []
+
+ rows = len(image)
+ columns = len(image[0])
+
+ for row in range(rows):
+ new_row = []
+ for column in range(columns):
+ red = image[row][column][0]
+ green = image[row][column][1]
+ blue = image[row][column][2]
+
+ new_red = int(red + coef * (255 - red))
+ new_green = int(green + coef * (255 - green))
+ new_blue = int(blue + coef * (255 - blue))
+ new_row.append((new_red, new_green, new_blue))
+ new_image.append(new_row)
+
+ return new_image
+
+
+def darken(image, coef):
+ new_image = []
+
+ rows = len(image)
+ columns = len(image[0])
+
+ for row in range(rows):
+ new_row = []
+ for column in range(columns):
+ red = image[row][column][0]
+ green = image[row][column][1]
+ blue = image[row][column][2]
+
+ new_red = int(red - coef * red)
+ new_green = int(green - coef * green)
+ new_blue = int(blue - coef * blue)
+ new_row.append((new_red, new_green, new_blue))
+ new_image.append(new_row)
+
+ return new_image
+
+
+def create_histogram(image):
+ rows = len(image)
+ columns = len(image[0])
+
+ red = {}
+ green = {}
+ blue = {}
+
+ for row in range(rows):
+ for column in range(columns):
+ if image[row][column][0] in red:
+ red[image[row][column][0]] += 1
+ else:
+ red[image[row][column][0]] = 1
+
+ if image[row][column][1] in green:
+ green[image[row][column][1]] += 1
+ else:
+ green[image[row][column][1]] = 1
+
+ if image[row][column][2] in blue:
+ blue[image[row][column][2]] += 1
+ else:
+ blue[image[row][column][2]] = 1
+
+ histogram = {'red': red, 'green': green, 'blue': blue}
+ return histogram