Решение на Работа с картинки от Георги Иванов

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

Към профила на Георги Иванов

Резултати

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

Код

def rotate_left(image):
new_image = []
rows = len(image)
cols = len(image[0])
for i in range(cols - 1, -1, -1):
row = []
for j in range(0, rows):
row.append(image[j][i])
new_image.append(row)
return new_image
def rotate_right(image):
new_image = []
rows = len(image)
cols = len(image[0])
for i in range(0, cols):
row = []
for j in range(rows - 1, -1, -1):
row.append(image[j][i])
new_image.append(row)
return new_image
def invert(image):
new_image = []
rows = len(image)
cols = len(image[0])
for i in range(0, rows):
row = []
for j in range(0, cols):
row.append((255 - image[i][j][0],
255 - image[i][j][1],
255 - image[i][j][2]))
new_image.append(row)
return new_image
def lighten(image, coef):
new_image = []
rows = len(image)
cols = len(image[0])
for i in range(0, rows):
row = []
for j in range(0, cols):
row.append((image[i][j][0] + int(coef * (255 - image[i][j][0])),
image[i][j][1] + int(coef * (255 - image[i][j][1])),
image[i][j][2] + int(coef * (255 - image[i][j][2]))))
new_image.append(row)
return new_image
def darken(image, coef):
new_image = []
rows = len(image)
cols = len(image[0])
for i in range(0, rows):
row = []
for j in range(0, cols):
row.append((image[i][j][0] - int(coef * image[i][j][0]),
image[i][j][1] - int(coef * image[i][j][1]),
image[i][j][2] - int(coef * image[i][j][2])))
new_image.append(row)
return new_image
def create_histogram(image):
histogram = {
"red": {},
"green": {},
"blue": {},
}
rows = len(image)
cols = len(image[0])
for i in range(0, rows):
for j in range(0, cols):
if image[i][j][0] in histogram["red"].keys():
histogram["red"][image[i][j][0]] += 1
else:
histogram["red"][image[i][j][0]] = 1
if image[i][j][1] in histogram["green"].keys():
histogram["green"][image[i][j][1]] += 1
else:
histogram["green"][image[i][j][1]] = 1
if image[i][j][2] in histogram["blue"].keys():
histogram["blue"][image[i][j][2]] += 1
else:
histogram["blue"][image[i][j][2]] = 1
return histogram

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

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

OK

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

Георги обнови решението на 13.03.2016 14:51 (преди над 5 години)

+def rotate_left(image):
+ new_image = []
+
+ rows = len(image)
+ cols = len(image[0])
+
+ for i in range(cols - 1, -1, -1):
+ row = []
+ for j in range(0, rows):
+ row.append(image[j][i])
+ new_image.append(row)
+
+ return new_image
+
+
+def rotate_right(image):
+ new_image = []
+
+ rows = len(image)
+ cols = len(image[0])
+
+ for i in range(0, cols):
+ row = []
+ for j in range(rows - 1, -1, -1):
+ row.append(image[j][i])
+ new_image.append(row)
+
+ return new_image
+
+
+def invert(image):
+ new_image = []
+
+ rows = len(image)
+ cols = len(image[0])
+
+ for i in range(0, rows):
+ row = []
+ for j in range(0, cols):
+ row.append((255 - image[i][j][0],
+ 255 - image[i][j][1],
+ 255 - image[i][j][2]))
+ new_image.append(row)
+
+ return new_image
+
+
+def lighten(image, coef):
+ new_image = []
+
+ rows = len(image)
+ cols = len(image[0])
+
+ for i in range(0, rows):
+ row = []
+ for j in range(0, cols):
+ row.append((image[i][j][0] + int(coef * (255 - image[i][j][0])),
+ image[i][j][1] + int(coef * (255 - image[i][j][1])),
+ image[i][j][2] + int(coef * (255 - image[i][j][2]))))
+ new_image.append(row)
+
+ return new_image
+
+
+def darken(image, coef):
+ new_image = []
+
+ rows = len(image)
+ cols = len(image[0])
+
+ for i in range(0, rows):
+ row = []
+ for j in range(0, cols):
+ row.append((image[i][j][0] - int(coef * image[i][j][0]),
+ image[i][j][1] - int(coef * image[i][j][1]),
+ image[i][j][2] - int(coef * image[i][j][2])))
+ new_image.append(row)
+
+ return new_image
+
+
+def create_histogram(image):
+ histogram = {
+ "red": {},
+ "green": {},
+ "blue": {},
+ }
+
+ rows = len(image)
+ cols = len(image[0])
+
+ for i in range(0, rows):
+ for j in range(0, cols):
+ if image[i][j][0] in histogram["red"].keys():
+ histogram["red"][image[i][j][0]] += 1
+ else:
+ histogram["red"][image[i][j][0]] = 1
+
+ if image[i][j][1] in histogram["green"].keys():
+ histogram["green"][image[i][j][1]] += 1
+ else:
+ histogram["green"][image[i][j][1]] = 1
+
+ if image[i][j][2] in histogram["blue"].keys():
+ histogram["blue"][image[i][j][2]] += 1
+ else:
+ histogram["blue"][image[i][j][2]] = 1
+
+ return histogram