Skip to content

Instantly share code, notes, and snippets.

@TuringNPcomplete
Last active December 28, 2021 06:33
Show Gist options
  • Save TuringNPcomplete/8eada133d25556877d4ffb95aa81795a to your computer and use it in GitHub Desktop.
Save TuringNPcomplete/8eada133d25556877d4ffb95aa81795a to your computer and use it in GitHub Desktop.
!pip install -Uqq fastbook
import fastbook
fastbook.setup_book()
#importing all the packages and functions from fastai's vision
from fastai.vision import *
from fastbook import
#loading the color image
eye_img = Image.open("/content/eye.jpg")
#converting to numpy array
eye_arr = array(eye_img)
#A function that does color to grayscale conversion
def transf_RBG(image_arr, RBG_conv_met):
"""
Takes an image array and a conversion method as inputs
Transforms the color image to grayscale based on the specified method
"""
import cv2
blue, green, red = cv2.split(image_arr)
if RBG_conv_met == "Average":
grayscale_im = (np.round(3**-1*blue + 3**-1*green + 3**-1*red)).astype('uint8')
return grayscale_im
elif RBG_conv_met == "Weighted_average":
grayscale_im = (np.round(0.299*red + 0.587*green + 0.114*blue)).astype('uint8')
return grayscale_im
else:
grayscale_im = (np.round(0.21*red + 0.72*green + 0.07*blue)).astype('uint8')
return grayscale_im
#converting to grayscale and printing the outcomes
grayscale_avr, grayscale_w, grayscale_lu = [transf_RBG(eye_arr, RBG_conv_met) for RBG_conv_met in \
["Average", "Weighted_average", "Luminosity"] ]
show_images([grayscale_avr, grayscale_w, grayscale_lu], suptitle = \
"Grayscale Images based on: Average, Weighted Average, and Luminosity methods", \
titles = ["Average", "Weighted Average", "Luminosity"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment