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

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

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

Резултати

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

Код

from collections import defaultdict
def column(matrix, n):
return [l[n] for l in matrix]
def rev(l):
return [l[i] for i in range(len(l) - 1, -1, -1)]
def rotate_left(img):
return [column(img, i) for i in range(len(img[0]) - 1, -1, -1)]
def rotate_right(img):
return[rev(column(img, i)) for i in range(0, len(img[0]))]
def invert_color(col):
return (int)(255-col)
def invert_pixel(pixel):
return tuple(invert_color(pixel[i]) for i in range(len(pixel)))
def invert(img):
return [[invert_pixel(img[i][j])
for j in range(len(img[0]))] for i in range(len(img))]
def proc_pixel(func):
def process(pixel, coef):
return tuple(func(pixel[i], coef) for i in range(len(pixel)))
return process
def proc_img(func):
def image(img, coef):
return [[func(img[i][j], coef) for j in range(len(img[0]))]
for i in range(len(img))]
return image
@proc_img
@proc_pixel
def lighten(col, coef):
return (int)(col + coef*(255-col))
@proc_img
@proc_pixel
def darken(col, coef):
return (int)(col - coef*(col - 0))
def create_histogram(img):
result = {'red': defaultdict(int),
'green': defaultdict(int),
'blue': defaultdict(int)}
h = len(img)
w = len(img[0])
for i in range(h):
for j in range(w):
result['red'][img[i][j][0]] += 1
result['green'][img[i][j][1]] += 1
result['blue'][img[i][j][2]] += 1
return result

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

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

OK

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

Димо обнови решението на 11.03.2016 10:50 (преди над 5 години)

+def column(matrix,n) :
+ length = len(matrix)
+ result = []
+ index = 0
+ while index < length :
+ result.append(matrix[index][n])
+ index += 1
+ return result
+
+def rotate_left(img) :
+ if len(img) == 0 :
+ return []
+ index = len(img[0]) - 1
+ result = []
+ while index >= 0 :
+ result.append(column(img,index))
+ index -= 1
+ return result
+
+def rotate_right(img) :
+ if len(img) == 0 :
+ return []
+ length = len(img[0])
+ index = 0
+ result = []
+ while index < length :
+ col = column(img,index)
+ col.reverse()
+ result.append(col)
+ index += 1
+ return result
+
+def invert(pixel) :
+ red = 255-pixel[0]
+ green = 255-pixel[1]
+ blue = 255-pixel[2]
+ return int(red),int(green),int(blue)
+
+def lighten(pixel,x) :
+ red = pixel[0] + x*(255-pixel[0])
+ green = pixel[1] + x*(255-pixel[1])
+ blue = pixel[2] + x*(255-pixel[2])
+ return int(red),int(green),int(blue)
+
+def darken(pixel,x) :
+ red = pixel[0] - x*(pixel[0] - 0)
+ green = pixel[1] - x*(pixel[1] - 0)
+ blue = pixel[2] - x*(pixel[2] - 0)
+ return int(red),int(green),int(blue)
+
+def gen_func(func) :
+ def process_img(img,*x) :
+ if(img == []) :
+ return []
+ h = len(img)
+ w = len(img[0])
+ result = [[() for _ in range(w)] for _ in range(h)]
+ for i in range(h) :
+ for j in range(w) :
+ result[i][j] = func(img[i][j],*x)
+ return result
+ return process_img
+
+invert = gen_func(invert)
+lighten = gen_func(lighten)
+darken = gen_func(darken)
+
+def add_to_dict(d,col) :
+ if col in d :
+ d[col] += 1
+ else :
+ d[col] = 1
+
+def create_histogram(img) :
+ result = {'red' : dict(),'green' : dict(),'blue' : dict()}
+ red = result['red']
+ green = result['green']
+ blue = result['blue']
+ h = len(img)
+ w = len(img[0])
+ for i in range(h) :
+ for j in range(w) :
+ pixel = img[i][j]
+ add_to_dict(red,pixel[0])
+ add_to_dict(green,pixel[1])
+ add_to_dict(blue,pixel[2])
+ return result
  • add_to_dict болезнено много прилича на това, което би постигнал с defaultdict
  • lighten и darken изглеждат много, много, много еднакви. Повтаряш доста код, което означава, че има по-подреден начин да го направиш.
  • същото се отнася за rotate_left и rotate_rigt, където е още -видимо.
  • gen_func е яка идея, така че да map-неш функция върху цяло изображение, обаче реализацията с вложените for цикли далеч не е красива.
  • използваш я като декоратор(но без да ползваш синтаксиса с @?), но това не е много добра идея, понеже функцията, която връщаш приема различни аргументи от функцията, която „декорираш“. Това е страшно объркващо за човек, който чете кода ти.
  • цикли, били те for или while, които единствено конструират колекции не ти ли напомнят много силно на comprehension–и?
  • на няколко места създаваш имена, които сочат към обекти, които можеш просто да използваш без да създаваш още променливи за тях(примерно red, green и blue в create_histogram

Димо обнови решението на 12.03.2016 09:52 (преди над 5 години)

def column(matrix,n) :
- length = len(matrix)
- result = []
- index = 0
- while index < length :
- result.append(matrix[index][n])
- index += 1
- return result
+ return [l[n] for l in matrix]
+def rev(l) :
+ return [l[i] for i in range(len(l) - 1,-1,-1)]
+
def rotate_left(img) :
- if len(img) == 0 :
- return []
- index = len(img[0]) - 1
- result = []
- while index >= 0 :
- result.append(column(img,index))
- index -= 1
- return result
+ return [column(img,i) for i in range(len(img[0]) - 1,-1,-1)]
def rotate_right(img) :
- if len(img) == 0 :
- return []
- length = len(img[0])
- index = 0
- result = []
- while index < length :
- col = column(img,index)
- col.reverse()
- result.append(col)
- index += 1
- return result
+ return[rev(column(img,i)) for i in range(0,len(img[0]))]
-def invert(pixel) :
- red = 255-pixel[0]
- green = 255-pixel[1]
- blue = 255-pixel[2]
- return int(red),int(green),int(blue)
+def invert_color(col) :
+ return (int)(255-col)
-def lighten(pixel,x) :
- red = pixel[0] + x*(255-pixel[0])
- green = pixel[1] + x*(255-pixel[1])
- blue = pixel[2] + x*(255-pixel[2])
- return int(red),int(green),int(blue)
+def invert_pixel(pixel) :
+ return tuple(invert_color(pixel[i]) for i in range(len(pixel)))
-def darken(pixel,x) :
- red = pixel[0] - x*(pixel[0] - 0)
- green = pixel[1] - x*(pixel[1] - 0)
- blue = pixel[2] - x*(pixel[2] - 0)
- return int(red),int(green),int(blue)
+def invert(img) :
+ return [[invert_pixel(img[i][j])
+ for j in range(len(img[0]))] for i in range(len(img))]
-def gen_func(func) :
- def process_img(img,*x) :
- if(img == []) :
- return []
- h = len(img)
- w = len(img[0])
- result = [[() for _ in range(w)] for _ in range(h)]
- for i in range(h) :
- for j in range(w) :
- result[i][j] = func(img[i][j],*x)
- return result
- return process_img
+def proc_pixel(func) :
+ def process(pixel,coef) :
+ return tuple(func(pixel[i],coef) for i in range(len(pixel)))
+ return process
-invert = gen_func(invert)
-lighten = gen_func(lighten)
-darken = gen_func(darken)
+def proc_img(func) :
+ def image(img,coef) :
+ return [[func(img[i][j],coef) for j in range(len(img[0]))]
+ for i in range(len(img))]
+ return image
-def add_to_dict(d,col) :
- if col in d :
- d[col] += 1
- else :
- d[col] = 1
+@proc_img
+@proc_pixel
+def lighten(col,coef) :
+ return (int)(col + coef*(255-col))
+@proc_img
+@proc_pixel
+def darken(col,coef) :
+ return (int)(col - coef*(col - 0))
+
+from collections import defaultdict
+
def create_histogram(img) :
- result = {'red' : dict(),'green' : dict(),'blue' : dict()}
- red = result['red']
- green = result['green']
- blue = result['blue']
+ result = {'red' : defaultdict(int),
+ 'green' : defaultdict(int),
+ 'blue' : defaultdict(int)}
h = len(img)
w = len(img[0])
for i in range(h) :
for j in range(w) :
- pixel = img[i][j]
- add_to_dict(red,pixel[0])
- add_to_dict(green,pixel[1])
- add_to_dict(blue,pixel[2])
+ result['red'][img[i][j][0]] += 1
+ result['green'][img[i][j][1]] += 1
+ result['blue'][img[i][j][2]] += 1
return result

Димо обнови решението на 13.03.2016 10:28 (преди над 5 години)

-def column(matrix,n) :
+from collections import defaultdict
+
+
+def column(matrix, n):
return [l[n] for l in matrix]
-def rev(l) :
- return [l[i] for i in range(len(l) - 1,-1,-1)]
-def rotate_left(img) :
- return [column(img,i) for i in range(len(img[0]) - 1,-1,-1)]
+def rev(l):
+ return [l[i] for i in range(len(l) - 1, -1, -1)]
-def rotate_right(img) :
- return[rev(column(img,i)) for i in range(0,len(img[0]))]
-def invert_color(col) :
+def rotate_left(img):
+ return [column(img, i) for i in range(len(img[0]) - 1, -1, -1)]
+
+
+def rotate_right(img):
+ return[rev(column(img, i)) for i in range(0, len(img[0]))]
+
+
+def invert_color(col):
return (int)(255-col)
-def invert_pixel(pixel) :
+
+def invert_pixel(pixel):
return tuple(invert_color(pixel[i]) for i in range(len(pixel)))
-def invert(img) :
+
+def invert(img):
return [[invert_pixel(img[i][j])
for j in range(len(img[0]))] for i in range(len(img))]
-def proc_pixel(func) :
- def process(pixel,coef) :
- return tuple(func(pixel[i],coef) for i in range(len(pixel)))
+
+def proc_pixel(func):
+ def process(pixel, coef):
+ return tuple(func(pixel[i], coef) for i in range(len(pixel)))
return process
-def proc_img(func) :
- def image(img,coef) :
- return [[func(img[i][j],coef) for j in range(len(img[0]))]
+
+def proc_img(func):
+ def image(img, coef):
+ return [[func(img[i][j], coef) for j in range(len(img[0]))]
for i in range(len(img))]
return image
+
@proc_img
@proc_pixel
-def lighten(col,coef) :
+def lighten(col, coef):
return (int)(col + coef*(255-col))
+
@proc_img
@proc_pixel
-def darken(col,coef) :
+def darken(col, coef):
return (int)(col - coef*(col - 0))
-from collections import defaultdict
-def create_histogram(img) :
- result = {'red' : defaultdict(int),
- 'green' : defaultdict(int),
- 'blue' : defaultdict(int)}
+def create_histogram(img):
+ result = {'red': defaultdict(int),
+ 'green': defaultdict(int),
+ 'blue': defaultdict(int)}
h = len(img)
w = len(img[0])
- for i in range(h) :
- for j in range(w) :
+ for i in range(h):
+ for j in range(w):
result['red'][img[i][j][0]] += 1
result['green'][img[i][j][1]] += 1
result['blue'][img[i][j][2]] += 1
- return result
+ return result