Skip to content

Instantly share code, notes, and snippets.

@beans42
Created April 24, 2020 02:42
Show Gist options
  • Save beans42/db25031b6e29a399ccae03799fe09882 to your computer and use it in GitHub Desktop.
Save beans42/db25031b6e29a399ccae03799fe09882 to your computer and use it in GitHub Desktop.
from PIL import Image
def greyscale(img: Image) -> Image:
img_width, img_height = img.size
pixels = img.load()
#for every pixel
for i in range(img_width):
for j in range(img_height):
rgb = pixels[i, j]
brightness = int(rgb[0] * 0.2126 + rgb[1] * 0.7152 + rgb[2] * 0.0722) #relative luminance = 0.2126R + 0.7152G + 0.0722B
pixels[i, j] = ( brightness, brightness, brightness )
return img
image = Image.open(input() + ".png")
image = greyscale(image)
image.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment