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

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

Към профила на Антонио Илиев

Резултати

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

Код

def rotate_left(image):
return(list(zip(*image)))[::-1]
def rotate_right(image):
return(list(zip(*image[::-1])))
def invert(image):
tmp = image[:]
for i in range(len(image)):
for j in range(len(image[0])):
pixel = image[i][j]
pixel2 = (255 - pixel[0], 255 - pixel[1], 255 - pixel[2])
tmp[i][j] = pixel2
return tmp
def lighten(image, percent):
tmp = image[:]
for i in range(len(image)):
for j in range(len(image[0])):
pixel = image[i][j]
pixel2 = (
int(pixel[0] + percent*(255 - pixel[0])),
(int(pixel[1] + percent*(255 - pixel[1]))),
(int(pixel[2] + percent*(255 - pixel[2]))))
tmp[i][j] = pixel2
return tmp
def darken(image, percent):
tmp = image[:]
for i in range(len(image)):
for j in range(len(image[0])):
pixel = image[i][j]
pixel2 = (
int(pixel[0] - percent*(pixel[0]-0)),
int(pixel[1] - percent*(pixel[1]-0)),
int(pixel[2] - percent*(pixel[2]-0)))
tmp[i][j] = pixel2
return tmp
def create_histogram(image):
colors = {'red': {}, 'green': {}, 'blue': {}}
red = colors['red']
green = colors['green']
blue = colors['blue']
for i in range(len(image)):
for j in range(len(image[0])):
pixel = image[i][j]
if pixel[0] in red:
red[pixel[0]] = red[pixel[0]] + 1
else:
red[pixel[0]] = 1
if pixel[1] in green:
green[pixel[1]] = green[pixel[1]] + 1
else:
green[pixel[1]] = 1
if pixel[2] in blue:
blue[pixel[2]] = blue[pixel[2]] + 1
else:
blue[pixel[2]] = 1
colors['red'] = red
colors['green'] = green
colors['blue'] = blue
return colors

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

....FF......
======================================================================
FAIL: test_invert (test.TestImages)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: Tuples differ: (255, 255, 0) != (0, 0, 255)

First differing element 0:
255
0

- (255, 255, 0)
+ (0, 0, 255)

======================================================================
FAIL: test_invert_rectangle (test.TestImages)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: Tuples differ: (155, 255, 0) != (100, 0, 255)

First differing element 0:
155
100

- (155, 255, 0)
+ (100, 0, 255)

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

FAILED (failures=2)

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

Антонио обнови решението на 14.03.2016 00:06 (преди над 5 години)

+def rotate_left(image):
+ return(list(zip(*image)))[::-1]
+
+
+def rotate_right(image):
+ return(list(zip(*image[::-1])))
+
+
+def invert(image):
+ tmp = image[:]
+ for i in range(len(image)):
+ for j in range(len(image[0])):
+ pixel = image[i][j]
+ pixel2 = (255 - pixel[0], 255 - pixel[1], 255 - pixel[2])
+ tmp[i][j] = pixel2
+ return tmp
+
+
+def lighten(image, percent):
+ tmp = image[:]
+ for i in range(len(image)):
+ for j in range(len(image[0])):
+ pixel = image[i][j]
+ pixel2 = (
+ int(pixel[0] + percent*(255 - pixel[0])),
+ (int(pixel[1] + percent*(255 - pixel[1]))),
+ (int(pixel[2] + percent*(255 - pixel[2]))))
+ tmp[i][j] = pixel2
+ return tmp
+
+
+def darken(image, percent):
+ tmp = image[:]
+ for i in range(len(image)):
+ for j in range(len(image[0])):
+ pixel = image[i][j]
+ pixel2 = (
+ int(pixel[0] - percent*(pixel[0]-0)),
+ int(pixel[1] - percent*(pixel[1]-0)),
+ int(pixel[2] - percent*(pixel[2]-0)))
+ tmp[i][j] = pixel2
+ return tmp
+
+
+def create_histogram(image):
+ colors = {'red': {}, 'green': {}, 'blue': {}}
+ red = colors['red']
+ green = colors['green']
+ blue = colors['blue']
+ for i in range(len(image)):
+ for j in range(len(image[0])):
+ pixel = image[i][j]
+ if pixel[0] in red:
+ red[pixel[0]] = red[pixel[0]] + 1
+ else:
+ red[pixel[0]] = 1
+ if pixel[1] in green:
+ green[pixel[1]] = green[pixel[1]] + 1
+ else:
+ green[pixel[1]] = 1
+ if pixel[2] in blue:
+ blue[pixel[2]] = blue[pixel[2]] + 1
+ else:
+ blue[pixel[2]] = 1
+ colors['red'] = red
+ colors['green'] = green
+ colors['blue'] = blue
+ return colors