Skip to content

Instantly share code, notes, and snippets.

@003random
Created April 1, 2019 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 003random/84fab50e6707cc9f0b847d371a465bce to your computer and use it in GitHub Desktop.
Save 003random/84fab50e6707cc9f0b847d371a465bce to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import os, math
def neighbors(matrix, rowNumber, colNumber):
result = []
for rowAdd in range(-1, 2):
newRow = rowNumber + rowAdd
if newRow >= 0 and newRow <= len(matrix)-1:
for colAdd in range(-1, 2):
newCol = colNumber + colAdd
if newCol >= 0 and newCol <= len(matrix)-1:
if newCol == colNumber and newRow == rowNumber:
continue
result.append(matrix[newCol][newRow])
return result
def grayscale(image):
for i in range(len(image)):
for j in range(len(image[i])):
image[i][j] = list(image[i][j])
avg = math.ceil(sum(image[i][j]) / len(image[i][j]))
for k in range(0, 3):
image[i][j][k] = avg
return image
def monochrome(image, color):
for i in range(len(image)):
for j in range(len(image[i])):
image[i][j] = list(image[i][j])
avg = math.ceil(sum(image[i][j]) / len(image[i][j]))
for k in range(0, 3):
image[i][j][k] = math.ceil(list(color)[k] * avg / 255)
return image
def blur(image):
oldImage = image
for i in range(len(image)):
for j in range(len(image[i])):
image[i][j] = list(image[i][j])
avg = math.ceil(sum(image[i][j]) / len(image[i][j]))
n = neighbors(oldImage, i, j)
for k in range(0, 3):
newColor = math.ceil((len(n)*image[i][j][k]+sum([x[k] for x in n]))/16)
image[i][j][k] = newColor
return image
def main():
image = [[[255,0,0],[255,0,0],[255,0,0]], [[255,0,0],[255,0,0],[255,0,0]], [[255,0,0],[255,0,0],[255,0,0]]]
print("Image:")
print(image)
print("Grayscale:")
print(grayscale(image))
print("Monochrome:")
print(monochrome(image, (255, 0, 0)))
print("Blur:")
print(blur(image))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment