Решение на Работа с картинки от Петър Иванов

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

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

Резултати

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

Код

from collections import defaultdict
COLORS = ['red', 'green', 'blue']
def get_number_rows_cols(image):
return len(image), len(image[0])
def create_new_image_matrix(rows, cols):
new_image = [
[() for x in range(cols)] for y in range(rows)
]
return new_image
def invert_pixel(pixel):
return [255 - x for x in pixel]
def light_pixel(pixel, number):
return [int(x + (255 - x) * number) for x in pixel]
def dark_pixel(pixel, number):
return [int(x - (x - 0) * number) for x in pixel]
def generate_histogram_numbers(pixel, histogram):
for i in range(len(COLORS)):
histogram[COLORS[i]][pixel[i]] += 1
return histogram
def rotate_left(image):
rows, cols = get_number_rows_cols(image)
rotated_image = create_new_image_matrix(cols, rows)
for i in range(cols):
for j in range(rows):
rotated_image[i][j] = image[j][- (i - cols + 1)]
return rotated_image
def rotate_right(image):
rows, cols = get_number_rows_cols(image)
rotated_image = create_new_image_matrix(cols, rows)
for i in range(cols):
for j in range(rows):
rotated_image[i][j] = image[- (j - rows + 1)][i]
return rotated_image
def invert(image):
rows, cols = get_number_rows_cols(image)
inverted_image = create_new_image_matrix(rows, cols)
for i in range(rows):
for j in range(cols):
inverted_image[i][j] = tuple(invert_pixel(image[i][j]))
return inverted_image
def lighten(image, number):
rows, cols = get_number_rows_cols(image)
lighten_image = create_new_image_matrix(rows, cols)
for i in range(rows):
for j in range(cols):
lighten_image[i][j] = tuple(light_pixel(image[i][j], number))
return lighten_image
def darken(image, number):
rows, cols = get_number_rows_cols(image)
darken_image = create_new_image_matrix(rows, cols)
for i in range(rows):
for j in range(cols):
darken_image[i][j] = tuple(dark_pixel(image[i][j], number))
return darken_image
def create_histogram(image):
rows, cols = get_number_rows_cols(image)
histogram = {
key: defaultdict(int) for key in COLORS
}
for i in range(rows):
for j in range(cols):
pixel = image[i][j]
generate_histogram_numbers(pixel, histogram)
return histogram

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.092s

OK

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

Петър обнови решението на 13.03.2016 18:04 (преди над 5 години)

+COLORS = ['red', 'green', 'blue']
+
+
+def get_number_rows_cols(image):
+ return len(image), len(image[0])
+
+
+def create_new_image_matrix(rows, cols):
+ new_image = [
+ [() for x in range(cols)] for y in range(rows)
+ ]
+ return new_image
+
+
+def invert_pixel(pixel):
+ return [255 - x for x in pixel]
+
+
+def light_pixel(pixel, number):
+ return [int(x + (255 - x) * number) for x in pixel]
+
+
+def dark_pixel(pixel, number):
+ return [int(x - (x - 0) * number) for x in pixel]
+
+
+def get_histogram_with_new_number(color, number, histogram):
+ if number not in histogram[color]:
+ histogram[color][number] = 1
+ else:
+ histogram[color][number] += 1
+ return histogram
+
+
+def generate_histogram_numbers(pixel, histogram):
+ for i in range(len(COLORS)):
+ get_histogram_with_new_number(COLORS[i], pixel[i], histogram)
+ return histogram
+
+
+def rotate_left(image):
+ rows, cols = get_number_rows_cols(image)
+ rotated_image = create_new_image_matrix(cols, rows)
+ for i in range(cols):
+ for j in range(rows):
+ rotated_image[i][j] = image[j][- (i - cols + 1)]
+ return rotated_image
+
+
+def rotate_right(image):
+ rows, cols = get_number_rows_cols(image)
+ rotated_image = create_new_image_matrix(cols, rows)
+ for i in range(cols):
+ for j in range(rows):
+ rotated_image[i][j] = image[- (j - rows + 1)][i]
+ return rotated_image
+
+
+def invert(image):
+ rows, cols = get_number_rows_cols(image)
+ inverted_image = create_new_image_matrix(rows, cols)
+ for i in range(rows):
+ for j in range(cols):
+ inverted_image[i][j] = tuple(invert_pixel(image[i][j]))
+ return inverted_image
+
+
+def lighten(image, number):
+ rows, cols = get_number_rows_cols(image)
+ lighten_image = create_new_image_matrix(rows, cols)
+ for i in range(rows):
+ for j in range(cols):
+ lighten_image[i][j] = tuple(light_pixel(image[i][j], number))
+ return lighten_image
+
+
+def darken(image, number):
+ rows, cols = get_number_rows_cols(image)
+ darken_image = create_new_image_matrix(rows, cols)
+ for i in range(rows):
+ for j in range(cols):
+ darken_image[i][j] = tuple(dark_pixel(image[i][j], number))
+ return darken_image
+
+
+def create_histogram(image):
+ rows, cols = get_number_rows_cols(image)
+ histogram = {
+ 'red': {},
+ 'green': {},
+ 'blue': {},
+ }
+ for i in range(rows):
+ for j in range(cols):
+ pixel = image[i][j]
+ generate_histogram_numbers(pixel, histogram)
+ return histogram

Петър обнови решението на 13.03.2016 18:06 (преди над 5 години)

COLORS = ['red', 'green', 'blue']
def get_number_rows_cols(image):
return len(image), len(image[0])
def create_new_image_matrix(rows, cols):
new_image = [
[() for x in range(cols)] for y in range(rows)
]
return new_image
def invert_pixel(pixel):
return [255 - x for x in pixel]
def light_pixel(pixel, number):
return [int(x + (255 - x) * number) for x in pixel]
def dark_pixel(pixel, number):
return [int(x - (x - 0) * number) for x in pixel]
def get_histogram_with_new_number(color, number, histogram):
if number not in histogram[color]:
histogram[color][number] = 1
else:
histogram[color][number] += 1
return histogram
def generate_histogram_numbers(pixel, histogram):
for i in range(len(COLORS)):
get_histogram_with_new_number(COLORS[i], pixel[i], histogram)
return histogram
def rotate_left(image):
rows, cols = get_number_rows_cols(image)
rotated_image = create_new_image_matrix(cols, rows)
for i in range(cols):
for j in range(rows):
rotated_image[i][j] = image[j][- (i - cols + 1)]
return rotated_image
def rotate_right(image):
rows, cols = get_number_rows_cols(image)
rotated_image = create_new_image_matrix(cols, rows)
for i in range(cols):
for j in range(rows):
rotated_image[i][j] = image[- (j - rows + 1)][i]
return rotated_image
def invert(image):
rows, cols = get_number_rows_cols(image)
inverted_image = create_new_image_matrix(rows, cols)
for i in range(rows):
for j in range(cols):
inverted_image[i][j] = tuple(invert_pixel(image[i][j]))
return inverted_image
def lighten(image, number):
rows, cols = get_number_rows_cols(image)
lighten_image = create_new_image_matrix(rows, cols)
for i in range(rows):
for j in range(cols):
lighten_image[i][j] = tuple(light_pixel(image[i][j], number))
return lighten_image
def darken(image, number):
rows, cols = get_number_rows_cols(image)
darken_image = create_new_image_matrix(rows, cols)
for i in range(rows):
for j in range(cols):
darken_image[i][j] = tuple(dark_pixel(image[i][j], number))
return darken_image
def create_histogram(image):
rows, cols = get_number_rows_cols(image)
histogram = {
- 'red': {},
- 'green': {},
- 'blue': {},
+ key: {} for key in COLORS
}
for i in range(rows):
for j in range(cols):
pixel = image[i][j]
generate_histogram_numbers(pixel, histogram)
return histogram

Петър обнови решението на 13.03.2016 18:27 (преди над 5 години)

+from collections import defaultdict
+
+
COLORS = ['red', 'green', 'blue']
def get_number_rows_cols(image):
return len(image), len(image[0])
def create_new_image_matrix(rows, cols):
new_image = [
[() for x in range(cols)] for y in range(rows)
]
return new_image
def invert_pixel(pixel):
return [255 - x for x in pixel]
def light_pixel(pixel, number):
return [int(x + (255 - x) * number) for x in pixel]
def dark_pixel(pixel, number):
return [int(x - (x - 0) * number) for x in pixel]
-def get_histogram_with_new_number(color, number, histogram):
- if number not in histogram[color]:
- histogram[color][number] = 1
- else:
- histogram[color][number] += 1
- return histogram
-
-
def generate_histogram_numbers(pixel, histogram):
for i in range(len(COLORS)):
- get_histogram_with_new_number(COLORS[i], pixel[i], histogram)
+ histogram[COLORS[i]][pixel[i]] += 1
return histogram
def rotate_left(image):
rows, cols = get_number_rows_cols(image)
rotated_image = create_new_image_matrix(cols, rows)
for i in range(cols):
for j in range(rows):
rotated_image[i][j] = image[j][- (i - cols + 1)]
return rotated_image
def rotate_right(image):
rows, cols = get_number_rows_cols(image)
rotated_image = create_new_image_matrix(cols, rows)
for i in range(cols):
for j in range(rows):
rotated_image[i][j] = image[- (j - rows + 1)][i]
return rotated_image
def invert(image):
rows, cols = get_number_rows_cols(image)
inverted_image = create_new_image_matrix(rows, cols)
for i in range(rows):
for j in range(cols):
inverted_image[i][j] = tuple(invert_pixel(image[i][j]))
return inverted_image
def lighten(image, number):
rows, cols = get_number_rows_cols(image)
lighten_image = create_new_image_matrix(rows, cols)
for i in range(rows):
for j in range(cols):
lighten_image[i][j] = tuple(light_pixel(image[i][j], number))
return lighten_image
def darken(image, number):
rows, cols = get_number_rows_cols(image)
darken_image = create_new_image_matrix(rows, cols)
for i in range(rows):
for j in range(cols):
darken_image[i][j] = tuple(dark_pixel(image[i][j], number))
return darken_image
def create_histogram(image):
rows, cols = get_number_rows_cols(image)
histogram = {
- key: {} for key in COLORS
+ key: defaultdict(int) for key in COLORS
}
for i in range(rows):
for j in range(cols):
pixel = image[i][j]
generate_histogram_numbers(pixel, histogram)
- return histogram
+ return histogram