Решение на Работа с картинки от Мартин Терзийски

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

Към профила на Мартин Терзийски

Резултати

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

Код

def invert_pixel(pixel):
return (255 - pixel[0], 255 - pixel[1], 255 - pixel[2])
def invert(image):
return [[invert_pixel(pixel) for pixel in xs] for xs in image]
def lighten_pixel(number, pixel):
r = pixel[0] + int(number * (255 - pixel[0]))
g = pixel[1] + int(number * (255 - pixel[1]))
b = pixel[2] + int(number * (255 - pixel[2]))
return (r, g, b)
def lighten(image, number):
return [[lighten_pixel(number, pixel) for pixel in xs] for xs in image]
def darken_pixel(number, pixel):
r = pixel[0] - int(number * pixel[0])
g = pixel[1] - int(number * pixel[1])
b = pixel[2] - int(number * pixel[2])
return (r, g, b)
def darken(image, number):
return [[darken_pixel(number, pixel) for pixel in xs] for xs in image]
def rotate_left(image):
width = range(len(image[0]) - 1, -1, -1)
height = range(len(image))
return [[image[i][j] for i in height] for j in width]
def rotate_right(image):
width = range(len(image[0]))
height = range(len(image) - 1, -1, -1)
return [[image[i][j] for i in height] for j in width]
def create_histogram(image):
histogram = {"red": {}, "green": {}, "blue": {}}
for rows in image:
for pixel in rows:
if pixel[0] in histogram['red'].keys():
histogram['red'][pixel[0]] += 1
else:
histogram['red'][pixel[0]] = 1
if pixel[1] in histogram['green'].keys():
histogram['green'][pixel[1]] += 1
else:
histogram['green'][pixel[1]] = 1
if pixel[2] in histogram['blue'].keys():
histogram['blue'][pixel[2]] += 1
else:
histogram['blue'][pixel[2]] = 1
return histogram

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

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

OK

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

Мартин обнови решението на 12.03.2016 16:14 (преди над 5 години)

+def invert_pixel(pixel):
+ r = 255 - pixel[0]
+ g = 255 - pixel[1]
+ b = 255 - pixel[2]
+ return (r, g, b)
+
+
+def invert(image):
+ return [[invert_pixel(pixel) for pixel in xs] for xs in image]
+
+
+def lighten_pixel(number, pixel):
+ r = pixel[0] + int(number * (255 - pixel[0]))
+ g = pixel[1] + int(number * (255 - pixel[1]))
+ b = pixel[2] + int(number * (255 - pixel[2]))
+ return (r, g, b)
+
+
+def lighten(image, number):
+ return [[lighten_pixel(number, pixel) for pixel in xs] for xs in image]
+
+
+def darken_pixel(number, pixel):
+ r = pixel[0] - int(number * pixel[0])
+ g = pixel[1] - int(number * pixel[1])
+ b = pixel[2] - int(number * pixel[2])
+ return (r, g, b)
+
+
+def darken(image, number):
+ return [[darken_pixel(number, pixel) for pixel in xs] for xs in image]
+
+
+def rotate_left(image):
+ columns_r = range(len(image[0]) - 1, -1, -1)
+ rows_r = range(len(image))
+ return [[image[i][j] for i in rows_r] for j in columns_r]
+
+
+def rotate_right(image):
+ columns_r = range(len(image[0]))
+ rows_r = range(len(image) - 1, -1, -1)
+ return [[image[i][j] for i in rows_r] for j in columns_r]
+
+
+def create_histogram(image):
+ histogram = {"red": {}, "green": {}, "blue": {}}
+ for rows in image:
+ for pixel in rows:
+ if pixel[0] in histogram['red'].keys():
+ histogram['red'][pixel[0]] += 1
+ else:
+ histogram['red'][pixel[0]] = 1
+ if pixel[1] in histogram['green'].keys():
+ histogram['green'][pixel[1]] += 1
+ else:
+ histogram['green'][pixel[1]] = 1
+ if pixel[2] in histogram['blue'].keys():
+ histogram['blue'][pixel[2]] += 1
+ else:
+ histogram['blue'][pixel[2]] = 1
+ return histogram
  • Дали не можеш да използваш структура със стойности по подразбиране, за да се справиш с излишния код в create_histogram
  • Можеш да изнесеш по-смислени неща в променливи от columns_r, например width или height
  • Нямаш нужда от r, g и b, можеш директно да използваш стойността им

Мартин обнови решението на 14.03.2016 14:42 (преди над 5 години)

def invert_pixel(pixel):
- r = 255 - pixel[0]
- g = 255 - pixel[1]
- b = 255 - pixel[2]
- return (r, g, b)
+ return (255 - pixel[0], 255 - pixel[1], 255 - pixel[2])
def invert(image):
return [[invert_pixel(pixel) for pixel in xs] for xs in image]
def lighten_pixel(number, pixel):
r = pixel[0] + int(number * (255 - pixel[0]))
g = pixel[1] + int(number * (255 - pixel[1]))
b = pixel[2] + int(number * (255 - pixel[2]))
return (r, g, b)
def lighten(image, number):
return [[lighten_pixel(number, pixel) for pixel in xs] for xs in image]
def darken_pixel(number, pixel):
r = pixel[0] - int(number * pixel[0])
g = pixel[1] - int(number * pixel[1])
b = pixel[2] - int(number * pixel[2])
return (r, g, b)
def darken(image, number):
return [[darken_pixel(number, pixel) for pixel in xs] for xs in image]
def rotate_left(image):
- columns_r = range(len(image[0]) - 1, -1, -1)
- rows_r = range(len(image))
- return [[image[i][j] for i in rows_r] for j in columns_r]
+ width = range(len(image[0]) - 1, -1, -1)
+ height = range(len(image))
+ return [[image[i][j] for i in height] for j in width]
def rotate_right(image):
- columns_r = range(len(image[0]))
- rows_r = range(len(image) - 1, -1, -1)
- return [[image[i][j] for i in rows_r] for j in columns_r]
+ width = range(len(image[0]))
+ height = range(len(image) - 1, -1, -1)
+ return [[image[i][j] for i in height] for j in width]
def create_histogram(image):
histogram = {"red": {}, "green": {}, "blue": {}}
for rows in image:
for pixel in rows:
if pixel[0] in histogram['red'].keys():
histogram['red'][pixel[0]] += 1
else:
histogram['red'][pixel[0]] = 1
if pixel[1] in histogram['green'].keys():
histogram['green'][pixel[1]] += 1
else:
histogram['green'][pixel[1]] = 1
if pixel[2] in histogram['blue'].keys():
histogram['blue'][pixel[2]] += 1
else:
histogram['blue'][pixel[2]] = 1
return histogram