WKTSHNN/simplify_color_values
1
1import gradio as gr2from PIL import Image3import numpy as np4 5 6def brightness(image):7 8 # 変数名の設定9 tmp_dir = "./tmp" 10 img_filename= "image"11 ext = image.split('.')[1]12 13 14 #画像取り込み(白黒)15 im_gray = np.array(Image.open(f'{image}').convert('L'), np.float64)16 17 18 #9分割19 im_gray_copy = im_gray.copy()20 for num in range(im_gray_copy.shape[0]):21 22 target = im_gray_copy[num]23 target[(target>=216.75)&(target<=255)] = 229.524 target[(target>=191.25)&(target<216.75)] = 20425 target[(target>=165.75)&(target<191.25)] = 178.526 target[(target>=140.25)&(target<165.75)] = 15327 target[(target>=114.75)&(target<140.25)] = 127.528 target[(target>=89.25)&(target<114.75)] = 10229 target[(target>=63.75)&(target<89.25)] = 76.530 target[(target>=38.25)&(target<63.75)] = 5131 target[(target>=0)&(target<38.25)] = 25.532 33 pil_img = Image.fromarray(im_gray_copy.astype(np.uint8))34 35 36 #5分割37 im_gray_copy2 = im_gray.copy()38 for num2 in range(im_gray_copy2.shape[0]):39 40 target = im_gray_copy2[num2]41 target[(target>=204)&(target<=255)] = 229.542 target[(target>=153)&(target<204)] = 178.543 target[(target>=102)&(target<153)] = 127.544 target[(target>=51)&(target<102)] = 76.545 target[(target>=0)&(target<51)] = 25.546 47 pil_img2 = Image.fromarray(im_gray_copy2.astype(np.uint8))48 49 50 #3分割51 im_gray_copy3 = im_gray.copy()52 for num3 in range(im_gray_copy2.shape[0]):53 54 target = im_gray_copy3[num3]55 target[(target>=178.5)&(target<=255)] = 229.556 target[(target>=76.5)&(target<178.5)] = 127.557 target[(target>=0)&(target<76.5)] = 25.558 59 pil_img3 = Image.fromarray(im_gray_copy3.astype(np.uint8))60 61 62 #明部・中部・暗部の割合を求める63 target2 = im_gray_copy364 area_bright = np.count_nonzero((target2>=178.5)&(target2<=255))65 area_middle = np.count_nonzero((target2>=76.5)&(target2<178.5))66 area_dark = np.count_nonzero((target2>=0)&(target2<76.5))67 68 ratio_bright = round(area_bright/target2.size*100)69 ratio_middle = round(area_middle/target2.size*100)70 ratio_dark = round(area_dark/target2.size*100)71 72 73 return f'{ratio_bright}%', f'{ratio_middle}%', f'{ratio_dark}%', pil_img, pil_img2, pil_img374 75 76 77with gr.Blocks() as demo:78 gr.Markdown("**This tool simplifies the colors in your image into 3-color, 5-color, and 9-color monochrome.**")79 gr.Markdown("Start selecting your picture and then click **Run** to see the output.")80 with gr.Row():81 inp = gr.Image(label="select your picture", type='filepath')82 83 with gr.Column(scale=1):84 outputs1 = gr.Textbox(label='明部 / bright tone')85 outputs2 = gr.Textbox(label='中間調 / middle tone')86 outputs3 = gr.Textbox(label='暗部 / dark tone')87 88 with gr.Row():89 outputs4 = gr.Image(label='9-color')90 outputs5 = gr.Image(label='5-color')91 outputs6 = gr.Image(label='3-color')92 93 btn = gr.Button("Run")94 btn.click(fn=brightness, inputs=inp, outputs=[outputs1, outputs2, outputs3, outputs4, outputs5, outputs6])95 96 gr.Markdown("**Description**")97 gr.Markdown("In paint software, brightness is represented by numbers ranging from 0 (black) to 100 (white). In the case of monochrome images, brightness directly corresponds to variations in shades of gray. We divide this brightness range as shown in the diagram. By grouping specific brightness ranges into a single color, we aim to simplify the brightness levels.")98 gr.HTML("<img src='/file=color value.jpg'>") 99 100demo.launch()101 102 103 