johnhelf commited on
Commit
5f0d916
·
1 Parent(s): 9b6c202

Update app.py

Browse files

try image background filter

Files changed (1) hide show
  1. app.py +36 -12
app.py CHANGED
@@ -4,11 +4,27 @@ import numpy as np
4
  import gradio as gr
5
 
6
  import os
7
- from PIL import Image
8
 
9
  from codeinterpreterapi import CodeInterpreterSession
10
 
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  def codeinterpreter(openai_key,prompt, files):
14
 
@@ -35,21 +51,29 @@ def codeinterpreter(openai_key,prompt, files):
35
 
36
 
37
  with gr.Blocks() as app:
38
- with gr.Row():
39
- with gr.Column():
40
- inp1=gr.Textbox(label="openai_key")
41
- inp2=gr.Textbox(label="prompt",info="input the prompt")
42
- inp3=gr.Files()
43
- btn = gr.Button(value="Submit")
44
- with gr.Column():
45
- out1=gr.Textbox(label="result")
46
- out2=gr.Gallery()
47
-
48
- gr.Examples([["Plot the nvidea stock vs microsoft stock over the last 6 months."],
49
  ["Plot a sin wave and show it to me."],
50
  ["贵州茅台最近半年走势"],
51
  ["Plot the bitcoin chart of 2023 YTD"]],
52
  [inp2])
 
 
 
 
 
 
 
53
  btn.click(codeinterpreter, inputs=[inp1,inp2,inp3], outputs=[out1,out2],api_name="getresult")
 
54
 
55
  app.launch()
 
4
  import gradio as gr
5
 
6
  import os
7
+ from PIL import Image, ImageDraw
8
 
9
  from codeinterpreterapi import CodeInterpreterSession
10
 
11
 
12
+ def imGreyAlpha(im):
13
+ grey = im.convert('L') # 转成灰度
14
+ px = grey.load() # 获取灰度数组
15
+
16
+ im2 = Image.new(mode="RGBA", size=(w, h), color=(255,255,255,0)) # 新建图片
17
+ draw = ImageDraw.Draw(im2) # 获取绘制句柄
18
+
19
+ w,h = im.size # 获取图片大小
20
+
21
+ # 遍历像素点
22
+ for i in range(w):
23
+ for j in range(h):
24
+ alpha = px[i, j] # 获取灰度作为alpha值
25
+ draw.point((i, j), fill=(0, 0, 0, 255 - alpha)) # 填充像素点
26
+ return im2 # 返回图像
27
+
28
 
29
  def codeinterpreter(openai_key,prompt, files):
30
 
 
51
 
52
 
53
  with gr.Blocks() as app:
54
+ with gr.Tab(label="codeinterpreter"):
55
+ with gr.Row():
56
+ with gr.Column():
57
+ inp1=gr.Textbox(label="openai_key")
58
+ inp2=gr.Textbox(label="prompt",info="input the prompt")
59
+ inp3=gr.Files()
60
+ btn = gr.Button(value="Submit")
61
+ with gr.Column():
62
+ out1=gr.Textbox(label="result")
63
+ out2=gr.Gallery()
64
+ gr.Examples([["Plot the nvidea stock vs microsoft stock over the last 6 months."],
65
  ["Plot a sin wave and show it to me."],
66
  ["贵州茅台最近半年走势"],
67
  ["Plot the bitcoin chart of 2023 YTD"]],
68
  [inp2])
69
+ with gr.Tab(label="image background filter"):
70
+ with gr.Row():
71
+ with gr.Column():
72
+ inp10=gr.Image(label="original image")
73
+ btn10 = gr.Button(value="Submit")
74
+ with gr.Column():
75
+ out10=gr.Image(label="result")
76
  btn.click(codeinterpreter, inputs=[inp1,inp2,inp3], outputs=[out1,out2],api_name="getresult")
77
+ btn10.click(imGreyAlpha,inputs[inp10],outputs=[out10],api_name="imageFilter")
78
 
79
  app.launch()