Решение на Работа с картинки от Любослава Димитрова

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

Към профила на Любослава Димитрова

Резултати

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

Код

def rotate_left(image):
result = []
transposed = [list(i) for i in zip(*image)]
for row in transposed[::-1]:
result.append(row)
return result
def rotate_right(image):
return rotate_left(rotate_left(rotate_left(image)))
def edit(image, level, kind):
editions = {
'invert': (lambda x: (int(255 - x))),
'lighten': (lambda x: (int(x + level*(255 - x)))),
'darken': (lambda x: (int(x - level*(x - 0))))
}
result = []
for row in image:
new_row = []
for pixel in row:
new_pixel = tuple(map(editions[kind], pixel))
new_row.append(new_pixel)
result.append(new_row)
return result
def invert(image):
return edit(image, None, 'invert')
def lighten(image, level):
return edit(image, level, 'lighten')
def darken(image, level):
return edit(image, level, 'darken')
def extract_colors(image):
result = [[], [], []]
for item in result:
for row in image:
for pixel in row:
item.append(pixel[result.index(item)])
return result
def create_histogram(image):
result = {}
colors_info = extract_colors(image)
colors = ['red', 'green', 'blue']
for color in colors:
index = colors.index(color)
levels = {}
for number in range(0, 256):
if colors_info[index].count(number) != 0:
levels[number] = colors_info[index].count(number)
result[color] = levels
return result

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.086s

OK

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

Любослава обнови решението на 12.03.2016 12:52 (преди над 5 години)

+def rotate_left(image):
+ result = []
+ transposed = [list(i) for i in zip(*image)]
+ for row in transposed[::-1]:
+ result.append(row)
+ return result
+
+
+def rotate_right(image):
+ return rotate_left(rotate_left(rotate_left(image)))
+
+
+def invert(image):
+ result = []
+ for row in image:
+ new_row = []
+ for pixel in row:
+ new_pixel = ()
+ for color in pixel:
+ new_pixel = new_pixel + (int(255 - color),)
+ new_row.append(new_pixel)
+ result.append(new_row)
+ return result
+
+
+def lighten(image, level):
+ result = []
+ for row in image:
+ new_row = []
+ for pixel in row:
+ new_pixel = ()
+ for color in pixel:
+ new_pixel = new_pixel + (int(color + level*(255 - color)),)
+ new_row.append(new_pixel)
+ result.append(new_row)
+ return result
+
+
+def darken(image, level):
+ result = []
+ for row in image:
+ new_row = []
+ for pixel in row:
+ new_pixel = ()
+ for color in pixel:
+ new_pixel = new_pixel + (int(color - level*(color - 0)),)
+ new_row.append(new_pixel)
+ result.append(new_row)
+ return result
+
+
+def extract_colors(image):
+ result = [[], [], []]
+ for item in result:
+ for row in image:
+ for pixel in row:
+ item.append(pixel[result.index(item)])
+ return result
+
+
+def create_histogram(image):
+ result = {}
+ colors_info = extract_colors(image)
+ colors = ['red', 'green', 'blue']
+ for color in colors:
+ index = colors.index(color)
+ levels = {}
+ for number in range(0, 256):
+ if colors_info[index].count(number) != 0:
+ levels[number] = colors_info[index].count(number)
+ else:
+ continue
+ result[color] = levels
+ return result
  • invert, lighten и darken са почти еднакви. Измисли как да избегнеш това повторение
  • Няма добра причина да правиш това new_pixel = (). Няколко реда по-късно не го мутираш, а създаваш нов tuple
  • Опитай се да минеш без конкатенация на tuple-ли
  • Няма смисъл от това else: continue в create_histogram
  • Креативна имплементация на rotate_right :)

Любослава обнови решението на 14.03.2016 01:12 (преди над 5 години)

def rotate_left(image):
result = []
transposed = [list(i) for i in zip(*image)]
for row in transposed[::-1]:
result.append(row)
return result
def rotate_right(image):
return rotate_left(rotate_left(rotate_left(image)))
def invert(image):
result = []
for row in image:
new_row = []
for pixel in row:
- new_pixel = ()
- for color in pixel:
- new_pixel = new_pixel + (int(255 - color),)
+ new_pixel = tuple(map((lambda x: (int(255 - x))), pixel))
new_row.append(new_pixel)
result.append(new_row)
return result
def lighten(image, level):
result = []
for row in image:
new_row = []
for pixel in row:
- new_pixel = ()
- for color in pixel:
- new_pixel = new_pixel + (int(color + level*(255 - color)),)
- new_row.append(new_pixel)
+ a = tuple(map((lambda x: (int(x + level*(255 - x)))), pixel))
+ new_row.append(a)
result.append(new_row)
return result
def darken(image, level):
result = []
for row in image:
new_row = []
for pixel in row:
- new_pixel = ()
- for color in pixel:
- new_pixel = new_pixel + (int(color - level*(color - 0)),)
+ new_pixel = tuple(map((lambda x: (int(x - level*(x - 0)))), pixel))
new_row.append(new_pixel)
result.append(new_row)
return result
def extract_colors(image):
result = [[], [], []]
for item in result:
for row in image:
for pixel in row:
item.append(pixel[result.index(item)])
return result
def create_histogram(image):
result = {}
colors_info = extract_colors(image)
colors = ['red', 'green', 'blue']
for color in colors:
index = colors.index(color)
levels = {}
for number in range(0, 256):
if colors_info[index].count(number) != 0:
levels[number] = colors_info[index].count(number)
else:
continue
result[color] = levels
- return result
+ return result

Любослава обнови решението на 14.03.2016 01:14 (преди над 5 години)

def rotate_left(image):
result = []
transposed = [list(i) for i in zip(*image)]
for row in transposed[::-1]:
result.append(row)
return result
def rotate_right(image):
return rotate_left(rotate_left(rotate_left(image)))
def invert(image):
result = []
for row in image:
new_row = []
for pixel in row:
new_pixel = tuple(map((lambda x: (int(255 - x))), pixel))
new_row.append(new_pixel)
result.append(new_row)
return result
def lighten(image, level):
result = []
for row in image:
new_row = []
for pixel in row:
a = tuple(map((lambda x: (int(x + level*(255 - x)))), pixel))
new_row.append(a)
result.append(new_row)
return result
def darken(image, level):
result = []
for row in image:
new_row = []
for pixel in row:
new_pixel = tuple(map((lambda x: (int(x - level*(x - 0)))), pixel))
new_row.append(new_pixel)
result.append(new_row)
return result
def extract_colors(image):
result = [[], [], []]
for item in result:
for row in image:
for pixel in row:
item.append(pixel[result.index(item)])
return result
def create_histogram(image):
result = {}
colors_info = extract_colors(image)
colors = ['red', 'green', 'blue']
for color in colors:
index = colors.index(color)
levels = {}
for number in range(0, 256):
if colors_info[index].count(number) != 0:
levels[number] = colors_info[index].count(number)
- else:
- continue
result[color] = levels
- return result
+ return result

Любослава обнови решението на 14.03.2016 01:25 (преди над 5 години)

def rotate_left(image):
result = []
transposed = [list(i) for i in zip(*image)]
for row in transposed[::-1]:
result.append(row)
return result
def rotate_right(image):
return rotate_left(rotate_left(rotate_left(image)))
-def invert(image):
+def edit(image, level, kind):
+ editions = {
+ 'invert': (lambda x: (int(255 - x))),
+ 'lighten': (lambda x: (int(x + level*(255 - x)))),
+ 'darken': (lambda x: (int(x - level*(x - 0))))
+ }
result = []
for row in image:
new_row = []
for pixel in row:
- new_pixel = tuple(map((lambda x: (int(255 - x))), pixel))
+ new_pixel = tuple(map(editions[kind], pixel))
new_row.append(new_pixel)
result.append(new_row)
return result
+def invert(image):
+ return edit(image, None, 'invert')
+
+
def lighten(image, level):
- result = []
- for row in image:
- new_row = []
- for pixel in row:
- a = tuple(map((lambda x: (int(x + level*(255 - x)))), pixel))
- new_row.append(a)
- result.append(new_row)
- return result
+ return edit(image, level, 'lighten')
def darken(image, level):
- result = []
- for row in image:
- new_row = []
- for pixel in row:
- new_pixel = tuple(map((lambda x: (int(x - level*(x - 0)))), pixel))
- new_row.append(new_pixel)
- result.append(new_row)
- return result
+ return edit(image, level, 'darken')
def extract_colors(image):
result = [[], [], []]
for item in result:
for row in image:
for pixel in row:
item.append(pixel[result.index(item)])
return result
def create_histogram(image):
result = {}
colors_info = extract_colors(image)
colors = ['red', 'green', 'blue']
for color in colors:
index = colors.index(color)
levels = {}
for number in range(0, 256):
if colors_info[index].count(number) != 0:
levels[number] = colors_info[index].count(number)
result[color] = levels
- return result
+ return result