Решение на Работа с картинки от Александра Матева

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

Към профила на Александра Матева

Резултати

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

Код

def rotate_right(image):
return [list(x)[::-1] for x in zip(*image)]
def rotate_left(image):
return [list(x) for x in zip(*[list(reversed(x)) for x in image])]
def inverting(row):
new_image = []
for pix in row:
new_image.append((255 - pix[0], 255 - pix[1], 255 - pix[2]))
return new_image
def invert(image):
return [x for x in (map(inverting, image))]
def darken(image, coeff):
new_image = []
for row in image:
new_image.append((list(map(lambda x: (int(x[0] - coeff*x[0]),
int(x[1] - coeff*x[1]),
int(x[2] - coeff*x[2])), row))))
return new_image
def lighten(image, coeff):
new_image = []
for row in image:
new_image.append((list(map(lambda x: (int(x[0] + coeff*(255 - x[0])),
int(x[1] + coeff*(255 - x[1])),
int(x[2] + coeff*(255 - x[2]))), row))))
return new_image
def create_histogram(image):
histogram = {'red': {x: 0 for x in range(0, 256)},
'green': {x: 0 for x in range(0, 256)},
'blue': {x: 0 for x in range(0, 256)}}
for row in image:
for pixel in row:
histogram['red'][pixel[0]] += 1
histogram['green'][pixel[1]] += 1
histogram['blue'][pixel[2]] += 1
for colour in histogram:
histogram[colour] = {key: value for key, value
in histogram[colour].items()
if value is not 0}
return histogram

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.126s

OK

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

Александра обнови решението на 12.03.2016 15:26 (преди над 5 години)

+def rotate_right(image):
+ return [list(x)[::-1] for x in zip(*image)]
+
+
+def rotate_left(image):
+ new_image = [list(reversed(x)) for x in image]
+ return [list(x) for x in zip(*new_image)]
+
+
+def inverting(row):
+ temp = []
+ for pix in row:
+ temp.append((255 - pix[0], 255 - pix[1], 255 - pix[2]))
+ return temp
+
+
+def invert(image):
+ return [x for x in (map(inverting, image))]
+
+
+def darken(image, coeff):
+ new_image = []
+ for row in image:
+ temp = (list(map(lambda x: (int(x[0] - coeff*x[0]),
+ int(x[1] - coeff*x[1]),
+ int(x[2] - coeff*x[2])), row)))
+ new_image.append(temp)
+ return new_image
+
+
+def lighten(image, coeff):
+ new_image = []
+ for row in image:
+ temp = (list(map(lambda x: (int(x[0] + coeff*(255 - x[0])),
+ int(x[1] + coeff*(255 - x[1])),
+ int(x[2] + coeff*(255 - x[2]))), row)))
+ new_image.append(temp)
+ return new_image
+
+
+def create_histogram(image):
+ histogram = {'red': {}, 'green': {}, 'blue': {}}
+ r, g, b = [], [], []
+ temp = list(zip(*[item for sublist in image for item in sublist]))
+
+ for x in range(0, len(temp[0])):
+ r.append((temp[0][x], 0))
+ g.append((temp[1][x], 0))
+ b.append((temp[2][x], 0))
+
+ histogram = {'red': dict(r), 'green': dict(g), 'blue': dict(b)}
+ for row in image:
+ for pixel in row:
+ histogram['red'][pixel[0]] += 1
+ histogram['green'][pixel[1]] += 1
+ histogram['blue'][pixel[2]] += 1
+ return histogram
  • Оправи си подравняването (използваме 4 интервала, а не два)
  • Можеш да дадеш по-описателни имена, temp нищо не значи
  • Дали не можеш да измислиш по-подходяща структура за create_histogram, която да има стойност по подразбиране?

Александра обнови решението на 14.03.2016 14:18 (преди над 5 години)

def rotate_right(image):
return [list(x)[::-1] for x in zip(*image)]
def rotate_left(image):
- new_image = [list(reversed(x)) for x in image]
- return [list(x) for x in zip(*new_image)]
+ return [list(x) for x in zip(*[list(reversed(x)) for x in image])]
def inverting(row):
- temp = []
+ new_image = []
for pix in row:
- temp.append((255 - pix[0], 255 - pix[1], 255 - pix[2]))
- return temp
+ new_image.append((255 - pix[0], 255 - pix[1], 255 - pix[2]))
+ return new_image
def invert(image):
return [x for x in (map(inverting, image))]
def darken(image, coeff):
new_image = []
for row in image:
- temp = (list(map(lambda x: (int(x[0] - coeff*x[0]),
- int(x[1] - coeff*x[1]),
- int(x[2] - coeff*x[2])), row)))
- new_image.append(temp)
+ new_image.append((list(map(lambda x: (int(x[0] - coeff*x[0]),
+ int(x[1] - coeff*x[1]),
+ int(x[2] - coeff*x[2])), row))))
return new_image
def lighten(image, coeff):
new_image = []
for row in image:
- temp = (list(map(lambda x: (int(x[0] + coeff*(255 - x[0])),
- int(x[1] + coeff*(255 - x[1])),
- int(x[2] + coeff*(255 - x[2]))), row)))
- new_image.append(temp)
+ new_image.append((list(map(lambda x: (int(x[0] + coeff*(255 - x[0])),
+ int(x[1] + coeff*(255 - x[1])),
+ int(x[2] + coeff*(255 - x[2]))), row))))
return new_image
def create_histogram(image):
- histogram = {'red': {}, 'green': {}, 'blue': {}}
- r, g, b = [], [], []
- temp = list(zip(*[item for sublist in image for item in sublist]))
-
- for x in range(0, len(temp[0])):
- r.append((temp[0][x], 0))
- g.append((temp[1][x], 0))
- b.append((temp[2][x], 0))
-
- histogram = {'red': dict(r), 'green': dict(g), 'blue': dict(b)}
+ histogram = {'red': {x: 0 for x in range(0, 256)},
+ 'green': {x: 0 for x in range(0, 256)},
+ 'blue': {x: 0 for x in range(0, 256)}}
for row in image:
for pixel in row:
histogram['red'][pixel[0]] += 1
histogram['green'][pixel[1]] += 1
histogram['blue'][pixel[2]] += 1
- return histogram
+ for colour in histogram:
+ histogram[colour] = {key: value for key, value
+ in histogram[colour].items()
+ if value is not 0}
+ return histogram