File size: 7,025 Bytes
2e0f012
 
2235ebb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b473941
 
 
2235ebb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b473941
 
 
2235ebb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34b32a1
 
 
2235ebb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b473941
2235ebb
 
b473941
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import gradio as gr
import openai
import os
import json
from PIL import Image, ImageDraw
import io
import requests

canvas_width = 500
canvas_height = 400

html = f"""
<head>
  <style>
    #selectRect {{
      position: absolute;
      border: 1px dashed red;
      background-color: rgba(255, 0, 0, 0.3);
    }}
  </style>
</head>
<body>
  <canvas id="canvas-root", width="{canvas_width}", height="{canvas_height}"></canvas>
  <div id="selectRect"></div>
</body>
"""

scripts = """
async () => {
    let isSelecting = false;
    let startX, startY, endX, endY;

    const canvas = document.getElementById('canvas-root');
    const ctx = canvas.getContext('2d');
    const canvasRect = canvas.getBoundingClientRect();

    const selectRect = document.getElementById('selectRect');

    const coordinatesElement = document.querySelector('#rectangle textarea');

    function handleMouseDown(event) {
      startX = event.clientX - canvasRect.left;
      startY = event.clientY - canvasRect.top;
      if (startX >= 0 && startY >= 0 && startX <= canvasRect.width && startY <= canvasRect.height) {
        isSelecting = true;
      }
    }

    function handleMouseMove(event) {
      if (isSelecting) {
        endX = Math.min(event.clientX - canvasRect.left, canvasRect.width);
        endY = Math.min(event.clientY - canvasRect.top, canvasRect.height);
        endX = Math.max(0, endX);
        endY = Math.max(0, endY);

        const left = Math.min(startX, endX);
        const top = Math.min(startY, endY);
        const width = Math.abs(endX - startX);
        const height = Math.abs(endY - startY);

        selectRect.style.left = left + 'px';
        selectRect.style.top = top + 'px';
        selectRect.style.width = width + 'px';
        selectRect.style.height = height + 'px';

        coordinatesElement.value = `{"left": ${left}, "top": ${top}, "width": ${width}, "height": ${height}}`;
        coordinatesElement.dispatchEvent(new CustomEvent("input"))
      }
    }

    function handleMouseUp() {
      isSelecting = false;
    }

    document.addEventListener('mousedown', handleMouseDown);
    document.addEventListener('mousemove', handleMouseMove);
    document.addEventListener('mouseup', handleMouseUp);
}
"""

image_change = """
async () => {
    const canvas = document.getElementById('canvas-root');
    const ctx= canvas.getContext('2d');
    const canvasRect = canvas.getBoundingClientRect();
    const selectRect = document.getElementById('selectRect');

    selectRect.style.left = 0;
    selectRect.style.top = 0;
    selectRect.style.width = 0;
    selectRect.style.height = 0;
    ctx.clearRect(0, 0, canvasRect.width, canvasRect.height);

    var img = document.querySelector('#input_image img');

    img.onload = function(){
        if ((img.naturalWidth / canvasRect.width) > (img.naturalHeight / canvasRect.height)) {
            width = canvasRect.width;
            height = img.naturalHeight * (width / img.naturalWidth);
        } else {
            height = canvasRect.height;
            width = img.naturalWidth * (height / img.naturalHeight);
        }
        ctx.drawImage(img, 0, 0, width, height);
    }
}
"""

def pil_to_bytes(pil_image, format='PNG'):
    image_bytes = io.BytesIO()
    pil_image.save(image_bytes, format=format)
    return image_bytes.getvalue()

def expand2square(image, background_color):
    width, height = image.size
    longest = max(width, height)
    result = Image.new(image.mode, (longest, longest), background_color)
    result.paste(image, (0, 0))
    return result.resize((2048, 2048))

def gen_mask(image, left, top, right, bottom):
    mask = Image.new("RGBA", image.size, (0, 0, 0, 255))
    width = image.size[0]
    height = image.size[1]
    draw = ImageDraw.Draw(mask)
    draw.rectangle(
        [(left*width, top*height), (right*width, bottom*height)], fill=(255, 255, 255, 0)
    )
    return mask

def create_edit(image, rect, prompt, api_key, api_organization=None):
    openai.organization = api_organization
    openai.api_key = api_key
    rect = json.loads(rect)
    image.putalpha(alpha=255)
    square_image = expand2square(image, "black")
    left, top, width, height = rect["left"], rect["top"], rect["width"], rect["height"]
    left, top, right, bottom = left / canvas_width, top / canvas_height, (left + width) / canvas_width, (top + height) / canvas_height

    response = openai.Image.create_edit(
      image=pil_to_bytes(square_image),
      mask=pil_to_bytes(gen_mask(square_image, left, top, right, bottom)),
      prompt=prompt,
      n=1,
      size="512x512"
    )

    edited_image_url = response['data'][0]['url']
    edited_image = requests.get(edited_image_url)
    edited_image = Image.open(io.BytesIO(edited_image.content))

    raw_width, raw_height = image.size
    raw_longest = max(raw_width, raw_height)
    crop_width = raw_width * edited_image.size[0] / raw_longest
    crop_height = raw_height * edited_image.size[1] / raw_longest
    croped_edited_image = edited_image.crop((0,0,crop_width, crop_height))

    return croped_edited_image

with gr.Blocks() as demo:
    with gr.Accordion("OpenAI API Settings", open=False):
        api_key = gr.Textbox(label="OpenAI API key", placeholder="OpenAI API key")
        api_organization = gr.Textbox(label="OpenAI API organization", placeholder="OpenAI API organization (optional)")
    with gr.Column():
        with gr.Row():
            with gr.Column():
                prompt_text = gr.Textbox(label="Prompt")
                prompt_examples = gr.Examples(
                            examples=[
                                "White plate.",
                                "A cherry on top of the pasta.",
                                "Curry.",
                            ],
                            inputs=[prompt_text],
                            outputs=None,
                        )
                in_image = gr.Image(label="Input", elem_id="input_image", type="pil")
                image_examples = gr.Examples(
                            examples=[
                                "images/001.jpg",
                                "images/002.jpg",
                                "images/003.jpg",
                            ],
                            inputs=[in_image],
                            outputs=None,
                        )
                out_image = gr.Image(label="Output")

            with gr.Column():
                gr.Markdown(
                """
                # Edit領域の指定
                ドラッグで編集対象のマスクの領域を指定してください。
                """)
                input_mic = gr.HTML(html)
                btn = gr.Button(value="Image Edit")
        rect_text = gr.Textbox(elem_id="rectangle", visible=False)
    in_image.change(None, inputs=None, outputs=None, _js=image_change)
    btn.click(create_edit, inputs=[in_image, rect_text, prompt_text, api_key, api_organization], outputs=[out_image])
    demo.load(_js=scripts)

demo.launch()