Skip to main content
Tweeted twitter.com/StackCodeReview/status/1085778866340282368
edited tags
Link
added 263 characters in body; edited tags; edited title
Source Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

Blurring A Given Imagea given image using Moving Averagemoving average in Python 3

So, I've tried solving the following question, given to me through HackerRank:

Question on HackerRankthe following HackerRank question:

Write a function blur_image() that takes as a parameter an image in the form of a nested list A and blurs it by averaging each pixel value with four of its neighboring pixels (Top, Bottom, Right, and Left). Note: not all of the neighbors are available in boundary cases. You have to write suitable conditions accordingly.

My Solution:

import ast
A = input()
A = ast.literal_eval(A)

def blur_image(a):
    result = []
    for i in range(len(a)):
        row = []
        for j in range(len(a[i])):
            total, count = a[i][j], 1
            if i + 1 < len(a): total, count = total + a[i+1][j], count + 1 
            if j + 1 < len(a[i]): total, count = total + a[i][j+1], count + 1
            if i - 1 > -1: total, count = total + a[i-1][j], count + 1 
            if j - 1 > -1: total, count = total + a[i][j-1], count + 1
            row.append(round(total/count, 2))
        result.append(row)
    return result

print(blur_image(A))

I would appreciate any suggestions and advice you can give me to improve this solution. Please note that my focus is to solve this without using any modules. Thank you!

Blurring A Given Image using Moving Average in Python 3

So, I've tried solving the following question, given to me through HackerRank:

Question on HackerRank

My Solution:

import ast
A = input()
A = ast.literal_eval(A)

def blur_image(a):
    result = []
    for i in range(len(a)):
        row = []
        for j in range(len(a[i])):
            total, count = a[i][j], 1
            if i + 1 < len(a): total, count = total + a[i+1][j], count + 1 
            if j + 1 < len(a[i]): total, count = total + a[i][j+1], count + 1
            if i - 1 > -1: total, count = total + a[i-1][j], count + 1 
            if j - 1 > -1: total, count = total + a[i][j-1], count + 1
            row.append(round(total/count, 2))
        result.append(row)
    return result

print(blur_image(A))

I would appreciate any suggestions and advice you can give me to improve this solution. Please note that my focus is to solve this without using any modules. Thank you!

Blurring a given image using moving average in Python 3

I've tried solving the following HackerRank question:

Write a function blur_image() that takes as a parameter an image in the form of a nested list A and blurs it by averaging each pixel value with four of its neighboring pixels (Top, Bottom, Right, and Left). Note: not all of the neighbors are available in boundary cases. You have to write suitable conditions accordingly.

My Solution:

import ast
A = input()
A = ast.literal_eval(A)

def blur_image(a):
    result = []
    for i in range(len(a)):
        row = []
        for j in range(len(a[i])):
            total, count = a[i][j], 1
            if i + 1 < len(a): total, count = total + a[i+1][j], count + 1 
            if j + 1 < len(a[i]): total, count = total + a[i][j+1], count + 1
            if i - 1 > -1: total, count = total + a[i-1][j], count + 1 
            if j - 1 > -1: total, count = total + a[i][j-1], count + 1
            row.append(round(total/count, 2))
        result.append(row)
    return result

print(blur_image(A))

I would appreciate any suggestions and advice you can give me to improve this solution. Please note that my focus is to solve this without using any modules.

Source Link

Blurring A Given Image using Moving Average in Python 3

So, I've tried solving the following question, given to me through HackerRank:

Question on HackerRank

My Solution:

import ast
A = input()
A = ast.literal_eval(A)

def blur_image(a):
    result = []
    for i in range(len(a)):
        row = []
        for j in range(len(a[i])):
            total, count = a[i][j], 1
            if i + 1 < len(a): total, count = total + a[i+1][j], count + 1 
            if j + 1 < len(a[i]): total, count = total + a[i][j+1], count + 1
            if i - 1 > -1: total, count = total + a[i-1][j], count + 1 
            if j - 1 > -1: total, count = total + a[i][j-1], count + 1
            row.append(round(total/count, 2))
        result.append(row)
    return result

print(blur_image(A))

I would appreciate any suggestions and advice you can give me to improve this solution. Please note that my focus is to solve this without using any modules. Thank you!