Решение на Работа с картинки от Иван Димитров

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

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

Резултати

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

Код

def rotate_right(image):
image_rows = len(image)
image_cols = len(image[0])
rotated_image = [[image[image_rows - 1 - j][i] for j in range(image_cols)]
for i in range(image_rows)]
return rotated_image
def rotate_left(image):
# or simply call rotate_right three times :D
image_rows = len(image)
image_cols = len(image[0])
rotated_image = [[image[j][image_cols - 1 - i] for j in range(image_cols)]
for i in range(image_rows)]
return rotated_image
def invert(image):
inverted_image = [[(255 - pixel[0], 255 - pixel[1], 255 - pixel[2])
for pixel in row] for row in image]
return inverted_image
def darken(image, coeff):
darkened_image = [[(int(pixel[0] - coeff * (pixel[0] - 0)),
int(pixel[1] - coeff * (pixel[1] - 0)),
int(pixel[2] - coeff * (pixel[2] - 0)))
for pixel in row] for row in image]
return darkened_image
def lighten(image, coeff):
lightened_image = [[(int(pixel[0] + coeff * (255 - pixel[0])),
int(pixel[1] + coeff * (255 - pixel[1])),
int(pixel[2] + coeff * (255 - pixel[2])))
for pixel in row] for row in image]
return lightened_image
def create_histogram(image):
reds = {}
greens = {}
blues = {}
for i in range(len(image)):
for j in range(len(image[0])):
pixel = image[i][j]
if pixel[0] in reds:
reds[pixel[0]] += 1
else:
reds[pixel[0]] = 1
if pixel[1] in greens:
greens[pixel[1]] += 1
else:
greens[pixel[1]] = 1
if pixel[2] in blues:
blues[pixel[2]] += 1
else:
blues[pixel[2]] = 1
histogram = {'red': reds,
'green': greens,
'blue': blues}
return histogram

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

.........E.E
======================================================================
ERROR: test_rotate_left_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
IndexError: list index out of range

======================================================================
ERROR: test_rotate_right_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
IndexError: list index out of range

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

FAILED (errors=2)

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

Иван обнови решението на 09.03.2016 17:01 (преди над 5 години)

+def rotate_right(image):
+ tup = (0, 0, 0)
+ image_rows = len(image)
+ image_cols = len(image[0])
+ rotated_image = [[tup for i in range(image_rows)]
+ for j in range(image_cols)]
+
+ for i in range(image_rows):
+ for j in range(image_cols):
+ rotated_image[i][j] = image[image_rows - 1 - j][i]
+
+ return rotated_image
+
+
+def rotate_left(image):
+ # or simply rotate_right(rotate_right(rotate_right(image)))) :D
+ tup = (0, 0, 0)
+ image_rows = len(image)
+ image_cols = len(image[0])
+ rotated_image = [[tup for i in range(image_rows)]
+ for j in range(image_cols)]
+
+ for i in range(image_rows):
+ for j in range(image_cols):
+ rotated_image[i][j] = image[j][image_cols - 1 - i]
+
+ return rotated_image
+
+
+def invert(image):
+ tup = (0, 0, 0)
+ image_rows = len(image)
+ image_cols = len(image[0])
+ inverted_image = [[tup for i in range(image_rows)]
+ for j in range(image_cols)]
+ for i in range(image_rows):
+ for j in range(image_cols):
+ new_red = 255 - image[i][j][0]
+ new_green = 255 - image[i][j][1]
+ new_blue = 255 - image[i][j][2]
+ new_pixel = (new_red, new_green, new_blue)
+ inverted_image[i][j] = new_pixel
+ return inverted_image
+
+
+def darken(image, coeff):
+ tup = (0, 0, 0)
+ image_rows = len(image)
+ image_cols = len(image[0])
+ darkened_image = [[tup for i in range(image_rows)]
+ for j in range(image_cols)]
+ for i in range(image_rows):
+ for j in range(image_cols):
+ new_red = int(image[i][j][0] - coeff * (image[i][j][0] - 0))
+ new_green = int(image[i][j][1] - coeff * (image[i][j][1] - 0))
+ new_blue = int(image[i][j][2] - coeff * (image[i][j][2] - 0))
+ new_pixel = (new_red, new_green, new_blue)
+ darkened_image[i][j] = new_pixel
+
+ return darkened_image
+
+
+def lighten(image, coeff):
+ tup = (0, 0, 0)
+ image_rows = len(image)
+ image_cols = len(image[0])
+ lightened_image = [[tup for i in range(image_rows)]
+ for j in range(image_cols)]
+ for i in range(image_rows):
+ for j in range(image_cols):
+ new_red = int(image[i][j][0] + coeff * (255 - image[i][j][0]))
+ new_green = int(image[i][j][1] + coeff * (255 - image[i][j][1]))
+ new_blue = int(image[i][j][2] + coeff * (255 - image[i][j][2]))
+ new_pixel = (new_red, new_green, new_blue)
+ lightened_image[i][j] = new_pixel
+
+ return lightened_image
+
+
+def create_histogram(image):
+ red_dict = {}
+ green_dict = {}
+ blue_dict = {}
+
+ for i in range(len(image)):
+ for j in range(len(image[0])):
+ pixel = image[i][j]
+ if pixel[0] in red_dict:
+ red_dict[pixel[0]] += 1
+ else:
+ red_dict[pixel[0]] = 1
+ if pixel[1] in green_dict:
+ green_dict[pixel[1]] += 1
+ else:
+ green_dict[pixel[1]] = 1
+ if pixel[2] in blue_dict:
+ blue_dict[pixel[2]] += 1
+ else:
+ blue_dict[pixel[2]] = 1
+
+ histogram = {'red': red_dict,
+ 'green': green_dict,
+ 'blue': blue_dict}
+
+ return histogram
  • Помисли дали не можеш да ползваш нещо от последната лекция, което да ти спести вложените for-ове :)
  • Името tup е доста лош избор, имаш ли изобщо нужда от този ред tup = (0, 0, 0)?
  • Не е добра практика имената да съдържат тип (red_dict, green_dict, blue_dict)

Иван обнови решението на 10.03.2016 00:44 (преди над 5 години)

def rotate_right(image):
- tup = (0, 0, 0)
image_rows = len(image)
image_cols = len(image[0])
- rotated_image = [[tup for i in range(image_rows)]
+ rotated_image = [[(0, 0, 0) for i in range(image_rows)]
for j in range(image_cols)]
for i in range(image_rows):
for j in range(image_cols):
rotated_image[i][j] = image[image_rows - 1 - j][i]
return rotated_image
def rotate_left(image):
- # or simply rotate_right(rotate_right(rotate_right(image)))) :D
- tup = (0, 0, 0)
+ # or simply call rotate_right three times :D
image_rows = len(image)
image_cols = len(image[0])
- rotated_image = [[tup for i in range(image_rows)]
+ rotated_image = [[(0, 0, 0) for i in range(image_rows)]
for j in range(image_cols)]
for i in range(image_rows):
for j in range(image_cols):
rotated_image[i][j] = image[j][image_cols - 1 - i]
return rotated_image
def invert(image):
- tup = (0, 0, 0)
- image_rows = len(image)
- image_cols = len(image[0])
- inverted_image = [[tup for i in range(image_rows)]
- for j in range(image_cols)]
- for i in range(image_rows):
- for j in range(image_cols):
- new_red = 255 - image[i][j][0]
- new_green = 255 - image[i][j][1]
- new_blue = 255 - image[i][j][2]
- new_pixel = (new_red, new_green, new_blue)
- inverted_image[i][j] = new_pixel
+ inverted_image = [[(255 - pixel[0], 255 - pixel[1], 255 - pixel[2])
+ for pixel in row] for row in image]
return inverted_image
def darken(image, coeff):
- tup = (0, 0, 0)
- image_rows = len(image)
- image_cols = len(image[0])
- darkened_image = [[tup for i in range(image_rows)]
- for j in range(image_cols)]
- for i in range(image_rows):
- for j in range(image_cols):
- new_red = int(image[i][j][0] - coeff * (image[i][j][0] - 0))
- new_green = int(image[i][j][1] - coeff * (image[i][j][1] - 0))
- new_blue = int(image[i][j][2] - coeff * (image[i][j][2] - 0))
- new_pixel = (new_red, new_green, new_blue)
- darkened_image[i][j] = new_pixel
-
+ darkened_image = [[(int(pixel[0] - coeff * (pixel[0] - 0)),
+ int(pixel[1] - coeff * (pixel[1] - 0)),
+ int(pixel[2] - coeff * (pixel[2] - 0)))
+ for pixel in row] for row in image]
return darkened_image
def lighten(image, coeff):
- tup = (0, 0, 0)
- image_rows = len(image)
- image_cols = len(image[0])
- lightened_image = [[tup for i in range(image_rows)]
- for j in range(image_cols)]
- for i in range(image_rows):
- for j in range(image_cols):
- new_red = int(image[i][j][0] + coeff * (255 - image[i][j][0]))
- new_green = int(image[i][j][1] + coeff * (255 - image[i][j][1]))
- new_blue = int(image[i][j][2] + coeff * (255 - image[i][j][2]))
- new_pixel = (new_red, new_green, new_blue)
- lightened_image[i][j] = new_pixel
-
+ lightened_image = [[(int(pixel[0] + coeff * (255 - pixel[0])),
+ int(pixel[1] + coeff * (255 - pixel[1])),
+ int(pixel[2] + coeff * (255 - pixel[2])))
+ for pixel in row] for row in image]
return lightened_image
def create_histogram(image):
- red_dict = {}
- green_dict = {}
- blue_dict = {}
+ reds = {}
+ greens = {}
+ blues = {}
for i in range(len(image)):
for j in range(len(image[0])):
pixel = image[i][j]
- if pixel[0] in red_dict:
- red_dict[pixel[0]] += 1
+ if pixel[0] in reds:
+ reds[pixel[0]] += 1
else:
- red_dict[pixel[0]] = 1
- if pixel[1] in green_dict:
- green_dict[pixel[1]] += 1
+ reds[pixel[0]] = 1
+ if pixel[1] in greens:
+ greens[pixel[1]] += 1
else:
- green_dict[pixel[1]] = 1
- if pixel[2] in blue_dict:
- blue_dict[pixel[2]] += 1
+ greens[pixel[1]] = 1
+ if pixel[2] in blues:
+ blues[pixel[2]] += 1
else:
- blue_dict[pixel[2]] = 1
+ blues[pixel[2]] = 1
- histogram = {'red': red_dict,
- 'green': green_dict,
- 'blue': blue_dict}
+ histogram = {'red': reds,
+ 'green': greens,
+ 'blue': blues}
return histogram

Иван обнови решението на 10.03.2016 20:56 (преди над 5 години)

def rotate_right(image):
image_rows = len(image)
image_cols = len(image[0])
- rotated_image = [[(0, 0, 0) for i in range(image_rows)]
- for j in range(image_cols)]
- for i in range(image_rows):
- for j in range(image_cols):
- rotated_image[i][j] = image[image_rows - 1 - j][i]
-
+ rotated_image = [[image[image_rows - 1 - j][i] for j in range(image_cols)]
+ for i in range(image_rows)]
return rotated_image
def rotate_left(image):
# or simply call rotate_right three times :D
image_rows = len(image)
image_cols = len(image[0])
- rotated_image = [[(0, 0, 0) for i in range(image_rows)]
- for j in range(image_cols)]
- for i in range(image_rows):
- for j in range(image_cols):
- rotated_image[i][j] = image[j][image_cols - 1 - i]
+ rotated_image = [[image[j][image_cols - 1 - i] for j in range(image_cols)]
+ for i in range(image_rows)]
return rotated_image
def invert(image):
inverted_image = [[(255 - pixel[0], 255 - pixel[1], 255 - pixel[2])
for pixel in row] for row in image]
return inverted_image
def darken(image, coeff):
darkened_image = [[(int(pixel[0] - coeff * (pixel[0] - 0)),
int(pixel[1] - coeff * (pixel[1] - 0)),
int(pixel[2] - coeff * (pixel[2] - 0)))
for pixel in row] for row in image]
return darkened_image
def lighten(image, coeff):
lightened_image = [[(int(pixel[0] + coeff * (255 - pixel[0])),
int(pixel[1] + coeff * (255 - pixel[1])),
int(pixel[2] + coeff * (255 - pixel[2])))
for pixel in row] for row in image]
return lightened_image
def create_histogram(image):
reds = {}
greens = {}
blues = {}
for i in range(len(image)):
for j in range(len(image[0])):
pixel = image[i][j]
if pixel[0] in reds:
reds[pixel[0]] += 1
else:
reds[pixel[0]] = 1
if pixel[1] in greens:
greens[pixel[1]] += 1
else:
greens[pixel[1]] = 1
if pixel[2] in blues:
blues[pixel[2]] += 1
else:
blues[pixel[2]] = 1
histogram = {'red': reds,
'green': greens,
'blue': blues}
return histogram