Решение на Работа с картинки от Николай Лазаров

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

Към профила на Николай Лазаров

Резултати

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

Код

from collections import Counter
def rotate_left(image):
height = len(image)
width = len(image[0])
rotated = [
[image[x][y] for x in range(height)] for y in range(width-1, -1, -1)
]
return rotated
def rotate_right(image):
height = len(image)
width = len(image[0])
rotated = [
[image[x][y] for x in range(height-1, -1, -1)] for y in range(width)
]
return rotated
def invert(image):
return [[(255-r, 255-g, 255-b) for (r, g, b) in row] for row in image]
def lighten(image, coef):
def transform(p):
return (p[0] + int(coef*(255-p[0])),
p[1] + int(coef*(255-p[1])),
p[2] + int(coef*(255-p[2])))
return [[transform(p) for p in row] for row in image]
def darken(image, coef):
def transform(p):
return (p[0] - int(coef*p[0]),
p[1] - int(coef*p[1]),
p[2] - int(coef*p[2]))
return [[transform(p) for p in row] for row in image]
def create_histogram(image):
histogram = {
'red': Counter(),
'green': Counter(),
'blue': Counter(),
}
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

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.085s

OK

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

Николай обнови решението на 08.03.2016 12:56 (преди над 5 години)

+import unittest
+
+import solution
+
+
+class TestImages(unittest.TestCase):
+ image = [
+ [(0, 0, 255), (0, 255, 0), (0, 0, 255)],
+ [(255, 0, 0), (0, 0, 255), (0, 255, 0)],
+ [(0, 255, 0), (0, 255, 0), (255, 0, 0)]]
+
+ def test_rotate_left(self):
+ self.assertEqual(
+ solution.rotate_left(self.image),
+ [[(0, 0, 255), (0, 255, 0), (255, 0, 0)],
+ [(0, 255, 0), (0, 0, 255), (0, 255, 0)],
+ [(0, 0, 255), (255, 0, 0), (0, 255, 0)]])
+
+ def test_lighten(self):
+ self.assertEqual(
+ solution.lighten(self.image, 0.5),
+ [[(127, 127, 255), (127, 255, 127), (127, 127, 255)],
+ [(255, 127, 127), (127, 127, 255), (127, 255, 127)],
+ [(127, 255, 127), (127, 255, 127), (255, 127, 127)]])
+
+ def test_invert(self):
+ self.assertEqual(
+ solution.invert(self.image),
+ [[(255, 255, 0), (255, 0, 255), (255, 255, 0)],
+ [(0, 255, 255), (255, 255, 0), (255, 0, 255)],
+ [(255, 0, 255), (255, 0, 255), (0, 255, 255)]])
+
+ def test_create_histogram(self):
+ self.assertEqual(
+ solution.create_histogram(self.image),
+ {'blue': {0: 6, 255: 3},
+ 'green': {0: 5, 255: 4},
+ 'red': {0: 7, 255: 2}})
+
+
+if __name__ == '__main__':
+ unittest.main()

Николай обнови решението на 08.03.2016 14:50 (преди над 5 години)

-import unittest
+from collections import Counter
-import solution
+def rotate_left(image):
+ height = len(image)
+ width = len(image[0])
+ rotated = [
+ [image[x][y] for x in range(height)] for y in range(width-1, -1, -1)
+ ]
-class TestImages(unittest.TestCase):
- image = [
- [(0, 0, 255), (0, 255, 0), (0, 0, 255)],
- [(255, 0, 0), (0, 0, 255), (0, 255, 0)],
- [(0, 255, 0), (0, 255, 0), (255, 0, 0)]]
+ return rotated
- def test_rotate_left(self):
- self.assertEqual(
- solution.rotate_left(self.image),
- [[(0, 0, 255), (0, 255, 0), (255, 0, 0)],
- [(0, 255, 0), (0, 0, 255), (0, 255, 0)],
- [(0, 0, 255), (255, 0, 0), (0, 255, 0)]])
- def test_lighten(self):
- self.assertEqual(
- solution.lighten(self.image, 0.5),
- [[(127, 127, 255), (127, 255, 127), (127, 127, 255)],
- [(255, 127, 127), (127, 127, 255), (127, 255, 127)],
- [(127, 255, 127), (127, 255, 127), (255, 127, 127)]])
+def rotate_right(image):
+ height = len(image)
+ width = len(image[0])
+ rotated = [
+ [image[x][y] for x in range(height-1, -1, -1)] for y in range(width)
+ ]
- def test_invert(self):
- self.assertEqual(
- solution.invert(self.image),
- [[(255, 255, 0), (255, 0, 255), (255, 255, 0)],
- [(0, 255, 255), (255, 255, 0), (255, 0, 255)],
- [(255, 0, 255), (255, 0, 255), (0, 255, 255)]])
+ return rotated
- def test_create_histogram(self):
- self.assertEqual(
- solution.create_histogram(self.image),
- {'blue': {0: 6, 255: 3},
- 'green': {0: 5, 255: 4},
- 'red': {0: 7, 255: 2}})
+def invert(image):
+ return [[(255-r, 255-g, 255-b) for (r, g, b) in row] for row in image]
-if __name__ == '__main__':
- unittest.main()
+
+def lighten(image, coef):
+ def transform(p):
+ return (p[0] + int(coef*(255-p[0])),
+ p[1] + int(coef*(255-p[1])),
+ p[2] + int(coef*(255-p[2])))
+
+ return [[transform(p) for p in row] for row in image]
+
+
+def darken(image, coef):
+ def transform(p):
+ return (p[0] - int(coef*p[0]),
+ p[1] - int(coef*p[1]),
+ p[2] - int(coef*p[2]))
+
+ return [[transform(p) for p in row] for row in image]
+
+
+def create_histogram(image):
+ histogram = {
+ 'red': Counter(),
+ 'green': Counter(),
+ 'blue': Counter(),
+ }
+
+ 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
  • Не изпозлваш предимствата на Counter в create_histogram, каква ще бъде разликата, ако използваш defaultdict в твоя случай?
  • Еднобуквените имена рядко са добра идея (когато говорим за координати x и y са хубави имена, но p може да бъде по-описателно)