juyam commited on
Commit
1120630
·
1 Parent(s): b0d3b35

Update app.py

Browse files

完全に作り直し。独自のマッピング辞書で変換する方針

Files changed (1) hide show
  1. app.py +31 -43
app.py CHANGED
@@ -1,46 +1,34 @@
 
1
  import cv2
2
  import numpy as np
3
- import gradio as gr
4
 
5
- def adjust_image_tone(input_image, brightness, contrast, gamma):
6
- image = input_image.astype(np.float32) / 255.0
7
-
8
- # Apply brightness and contrast adjustments
9
- image = cv2.addWeighted(image, 1 + contrast / 50.0, image, 0, brightness - 50)
10
-
11
- # Clip the pixel values between 0 and 1
12
- image = np.clip(image, 0, 1)
13
-
14
- # Apply gamma correction
15
- image = np.where(image <= 0, 1e-5, image) # ゼロ以下の値を極小値に置換
16
- image = np.power(image, gamma)
17
-
18
- # Scale the pixel values to the range [0, 255]
19
- adjusted_image = (image * 255)
20
-
21
- # Exclude invalid values and convert to uint8
22
- adjusted_image = np.where(np.isnan(adjusted_image), 0, adjusted_image)
23
- adjusted_image = np.where(adjusted_image > 255, 255, adjusted_image)
24
- adjusted_image = np.where(adjusted_image < 0, 0, adjusted_image)
25
-
26
- adjusted_image = adjusted_image.astype(np.uint8)
27
-
28
- return adjusted_image
29
-
30
- gr.Slider()
31
-
32
- image_input = gr.Image()
33
- brightness = gr.Slider(minimum=-50, maximum=50, value=0, label="Brightness")
34
- contrast = gr.Slider(minimum=-50, maximum=50, value=0, label="Contrast")
35
- gamma = gr.Slider(minimum=0.1, maximum=3.0, value=1.0, label="Gamma")
36
-
37
- image_output = gr.Image(type='pil')
38
-
39
- gr.Interface(
40
- fn=adjust_image_tone,
41
- inputs=[image_input, brightness, contrast, gamma],
42
- outputs=image_output,
43
- title="Image Tone Curve Adjustments",
44
- description="Adjust brightness, contrast, and gamma for an image.",
45
- live=True
46
- ).launch()
 
1
+ import gradio as gr
2
  import cv2
3
  import numpy as np
 
4
 
5
+ # リストのR、G、Bの値
6
+ remapped_R = [39, 44, 44, 40, 43, 45, 46, 46, 50, 49, 48, 52, 49, 51, 51, 52, 53, 54, 54, 54, 57, 59, 57, 59, 58, 63, 61, 28, 64, 66, 71, 69, 71, 71, 75, 78, 79, 79, 78, 81, 82, 82, 88, 85, 93, 92, 91, 92, 92, 94, 94, 94, 97, 98, 100, 104, 103, 87, 69, 115, 116, 114, 115, 65, 115, 118, 118, 121, 121, 122, 124, 123, 127, 128, 127, 128, 132, 131, 132, 135, 137, 136, 139, 140, 141, 143, 144, 145, 145, 148, 163, 150, 171, 167, 153, 154, 156, 155, 158, 158, 158, 161, 161, 161, 162, 164, 167, 165, 49, 166, 167, 168, 169, 170, 171, 172, 171, 173, 172, 175, 175, 176, 176, 177, 182, 182, 180, 183, 181, 184, 196, 186, 188, 186, 186, 186, 201, 188, 202, 203, 203, 204, 205, 205, 195, 205, 205, 196, 197, 208, 210, 205, 198, 198, 202, 212, 202, 213, 213, 203, 214, 214, 215, 215, 216, 216, 212, 212, 219, 211, 211, 215, 215, 215, 214, 216, 218, 219, 215, 217, 220, 221, 221, 221, 221, 221, 221, 224, 224, 223, 223, 225, 224, 226, 227, 228, 226, 228, 227, 227, 229, 228, 229, 231, 230, 231, 232, 232, 234, 234, 235, 235, 236, 236, 238, 237, 237, 239, 238, 239, 239, 240, 240, 241, 241, 242, 237, 237, 243, 244, 244, 241, 244, 246, 246, 247, 248, 249, 247, 248, 249, 250, 250, 255, 255, 255, 252, 255, 255, 255, 255, 255, 255, 255, 255, 255]
7
+ remapped_G = [28, 29, 29, 32, 32, 34, 35, 35, 35, 37, 35, 36, 38, 40, 40, 41, 41, 43, 43, 43, 45, 47, 47, 48, 48, 50, 51, 52, 52, 53, 40, 58, 58, 59, 60, 60, 63, 63, 65, 65, 66, 70, 68, 71, 49, 50, 51, 51, 51, 52, 52, 53, 86, 88, 55, 55, 56, 57, 96, 98, 100, 100, 100, 106, 106, 107, 110, 111, 112, 113, 115, 116, 118, 119, 121, 122, 123, 125, 126, 126, 128, 129, 130, 131, 132, 134, 135, 136, 136, 138, 137, 137, 140, 138, 143, 143, 145, 145, 147, 147, 148, 150, 150, 151, 150, 154, 152, 152, 156, 153, 156, 159, 156, 158, 158, 160, 160, 160, 162, 162, 159, 164, 166, 164, 167, 167, 169, 168, 168, 171, 144, 171, 172, 174, 173, 174, 174, 178, 176, 177, 178, 180, 180, 180, 180, 181, 182, 182, 183, 183, 184, 186, 186, 187, 188, 187, 190, 191, 191, 192, 192, 191, 194, 193, 193, 196, 197, 196, 197, 197, 199, 199, 199, 200, 203, 202, 201, 203, 204, 205, 205, 206, 198, 207, 207, 208, 209, 208, 210, 211, 212, 212, 213, 213, 214, 213, 215, 215, 216, 216, 218, 218, 210, 219, 219, 221, 221, 221, 222, 223, 224, 224, 224, 226, 225, 226, 226, 227, 228, 228, 230, 230, 230, 232, 232, 232, 233, 234, 234, 235, 236, 237, 240, 240, 238, 238, 239, 240, 241, 242, 241, 242, 244, 244, 244, 245, 245, 246, 247, 248, 249, 249, 250, 251, 251, 251]
8
+ remapped_B = [26, 30, 29, 30, 32, 30, 32, 30, 34, 35, 32, 36, 35, 38, 38, 38, 40, 41, 40, 41, 43, 46, 41, 46, 49, 49, 47, 32, 51, 34, 34, 34, 58, 36, 38, 39, 40, 40, 42, 65, 43, 70, 74, 70, 73, 75, 50, 79, 81, 82, 83, 86, 90, 90, 90, 91, 94, 96, 95, 100, 98, 100, 105, 105, 97, 97, 111, 110, 112, 115, 116, 118, 119, 121, 122, 122, 124, 125, 126, 129, 129, 130, 133, 133, 134, 135, 136, 137, 137, 137, 139, 140, 141, 143, 143, 144, 145, 147, 150, 150, 151, 151, 154, 155, 152, 155, 155, 158, 158, 158, 159, 160, 162, 162, 164, 162, 164, 165, 165, 166, 168, 168, 168, 169, 172, 172, 172, 173, 173, 174, 176, 176, 176, 177, 177, 178, 180, 180, 180, 182, 183, 183, 184, 186, 185, 185, 186, 188, 188, 186, 190, 189, 183, 190, 191, 194, 194, 193, 195, 195, 197, 197, 198, 198, 200, 200, 200, 176, 203, 202, 203, 204, 205, 203, 203, 206, 208, 208, 208, 208, 209, 210, 212, 212, 212, 212, 213, 214, 215, 215, 216, 216, 216, 217, 217, 218, 218, 218, 222, 222, 222, 222, 222, 223, 221, 224, 224, 225, 225, 227, 227, 227, 228, 228, 230, 230, 231, 231, 232, 232, 232, 234, 234, 235, 235, 237, 237, 238, 234, 234, 238, 239, 240, 240, 241, 241, 242, 243, 243, 242, 245, 246, 246, 246, 246, 247, 248, 248, 249, 249, 250, 250, 251, 251, 252, 252]
9
+
10
+ def remap_image(input_image):
11
+ # 画像を読み込む
12
+ image = cv2.imread(input_image.name, cv2.IMREAD_COLOR)
13
+
14
+ # 画像と同じサイズの黒い画像を作成
15
+ remapped_image = np.zeros_like(image)
16
+
17
+ # 画像のサイズを取得
18
+ height, width, _ = image.shape
19
+
20
+ # 画像の各ピクセルの色をリマップ
21
+ for r in range(height):
22
+ for c in range(width):
23
+ b1, g1, r1 = image[r][c]
24
+ remapped_image[r][c] = (remapped_B[b1], remapped_G[g1], remapped_R[r1])
25
+
26
+ # 画像を出力
27
+ return remapped_image
28
+
29
+ # Gradio UI を定義
30
+ image_input = gr.inputs.Image(label="Input Image")
31
+ image_output = gr.outputs.Image(label="Remapped Image")
32
+
33
+ # Gradioインターフェースを作成
34
+ gr.Interface(fn=remap_image, inputs=image_input, outputs=image_output, title="Image Remapper").launch()