# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="tT0pR0l7DcwP"
# # **Color Pop Effect (RGB Color Family)**
# User can select a color family within red, green and blue to perform a pop effect. Pixels which belongs to another two color families will be turned into grayscale.
#
# The RGB color code is in the format (R, G, B). Each parameter (R, G, and G) defines the intensity of the color as an integer between 0 and 255. For example. the RGB color code of red is (255,0,0). Every pixels in the image will be checked to identify which family the pixel belongs to. If the pixel has RGB Color code (241,45,85), that means the pixel falls under red color family. 
#
# However, if the RGB color code is (65,174,26), that means it is under green color family. Therefore, the  RGB Color code will be modified by using formula below.
#
#
#
#
# > 1. value = *(0.290 x R) + (0.587 x G) + (0.114 x B)*
#
# > 2. new_RGB_code = (value, value, value)

# + [markdown] id="dbmVbSi-Ag-b"
# ### 1. Import necessary libraries

# + id="OJlCEfmR_zRX"
import os
import cv2
import numpy as np
import pandas as pd 
from PIL import Image
from google.colab.patches import cv2_imshow

# + [markdown] id="Hzzk1KtcAv7I"
# ### 2. Mount Google Drive

# + colab={"base_uri": "https://localhost:8080/"} id="yP9Ea85lAgT_" outputId="5c9fcf3f-6a09-4839-c2fc-bef4d28b0f68"
from google.colab import drive
drive.mount('/content/gdrive')

# + [markdown] id="p5Q8x1T4A6rX"
# ### 3. Set working path
# The image will be uploaded to this path

# + colab={"base_uri": "https://localhost:8080/"} id="K9RoQ0pQA6v3" outputId="f9bd7d4e-d6b5-4251-e9ac-3dcc162d6468"
# %cd /content/gdrive/MyDrive/

# + [markdown] id="wVyrmNqVBN9C"
# ### 4. Upload image file

# + colab={"resources": {"http://localhost:8080/nbextensions/google.colab/files.js": {"data": "<KEY>", "ok": true, "headers": [["content-type", "application/javascript"]], "status": 200, "status_text": ""}}, "base_uri": "https://localhost:8080/", "height": 91} id="P5IgjSTRBNi5" outputId="4d59f2ce-5443-4ca1-9dfe-e32e95d836dc"
from google.colab import files
uploaded = files.upload()
for key, value in uploaded.items():
  try:
    print(key, 'is successfully uploaded!')
  except:
    print('Your file is not successfully uploaded!')

# + [markdown] id="VeRr6GI9BVbF"
# ### 5. Display the uploaded image

# + colab={"base_uri": "https://localhost:8080/", "height": 130} id="Hi-yUChiBVh7" outputId="16602415-d29c-4f33-b873-93a14bcba369"
directory = os.getcwd()
img = cv2.imread('{0}/{1}'.format(directory,key))
cv2_imshow(img)

# + [markdown] id="0jhTJHFDBgKs"
# ### 6. Create function for process bar
# To monitor the progress of image processing

# + colab={"base_uri": "https://localhost:8080/", "height": 34} id="7KwTkv7SBgPe" outputId="faca7c87-304c-472f-e73e-c15b9afbeb2d"
from IPython.display import HTML, display
import time

def progress(value, max=100):
    return HTML("""
        <progress
            value='{value}'
            max='{max}',
            style='width: 100%'
        >
            {value}
        </progress>
    """.format(value=value, max=max))

out = display(progress(0, 100), display_id=True)

# + [markdown] id="Vt-CPZLuBtTc"
# ### 7. Choose a color family to be kept
#  
# *   Pick a color family among red, green and blue
# *   Another two color family will be turned into grayscale

# + colab={"base_uri": "https://localhost:8080/"} id="A6_YGs7dBtYx" outputId="64825420-7e83-4f15-b946-de4101e60a26"
color_dict = {'0': 'Blue', '1': 'Green', '2': 'Red'}
print('{0}\n{1}'.format(color_dict,'Fill in the blank with number only'))
color_picked = input("\nWhat color family to be shown? ")

print('{0} {1}'.format(color_dict[color_picked],'family is chosen to be shown.'))

# + [markdown] id="7CjZWsYjCfmL"
# ### 8. Image Processing
# This will take few minutes or more according to the size of image. 

# + colab={"base_uri": "https://localhost:8080/", "height": 34} id="JZ7F2MyACfsF" outputId="68add858-20bf-4268-8460-85efee6f3511"
height, width = img.shape[:2]
image_arr = np.zeros((height, width, 3), dtype=np.uint8)

for y_coor in range(0,height,1):
  for x_coor in range(0,width,1):
    #time.sleep(0.02)
    (b, g, r) = img[y_coor, x_coor] #image[y-coordinate, x-coordinate]
    max_rgb_value = max(b,g,r)
    max_rgb_index = [b,g,r].index(max_rgb_value)
    
    if int(color_picked) == int(max_rgb_index):
      image_arr[y_coor, x_coor] = [b, g, r] #[b,g,r]
    else:
      value = 0.299*r + 0.587*g + 0.114*b
      image_arr[y_coor, x_coor] = [value, value, value] #[b,g,r]
    out.update(progress(y_coor, height))

# + [markdown] id="B5mCUIkGCxXH"
# ### 9. Display and save the processed image.
# The output image will be saved to the same directory in Step 3..

# + colab={"base_uri": "https://localhost:8080/", "height": 130} id="1SRT0aZYCxcv" outputId="c22498b7-6c21-4049-cb31-c0eb95b7584c"
image_arr_final = cv2.cvtColor(image_arr, cv2.COLOR_BGR2RGB)
output = Image.fromarray(image_arr_final)
display(output)
output.save(key[:-4]+'_'+color_dict[color_picked]+'.png')

StarcoderdataJupyterScriptsDedupFiltered