From aee57364dd8377ee6c86dfde0944f48f48ccbaeb Mon Sep 17 00:00:00 2001
From: qingxu fu <505030475@qq.com>
Date: Sun, 26 Nov 2023 00:24:51 +0800
Subject: [PATCH 1/2] edit image
---
crazy_functional.py | 12 +++++++++-
crazy_functions/图片生成.py | 48 ++++++++++++++++++++++++++-----------
2 files changed, 45 insertions(+), 15 deletions(-)
diff --git a/crazy_functional.py b/crazy_functional.py
index 3d4df718..b3dc5baf 100644
--- a/crazy_functional.py
+++ b/crazy_functional.py
@@ -354,7 +354,7 @@ def get_crazy_functions():
print('Load function plugin failed')
try:
- from crazy_functions.图片生成 import 图片生成_DALLE2, 图片生成_DALLE3
+ from crazy_functions.图片生成 import 图片生成_DALLE2, 图片生成_DALLE3, 图片修改_DALLE2
function_plugins.update({
"图片生成_DALLE2 (先切换模型到openai或api2d)": {
"Group": "对话",
@@ -377,6 +377,16 @@ def get_crazy_functions():
"Function": HotReload(图片生成_DALLE3)
},
})
+ # function_plugins.update({
+ # "图片修改_DALLE2 (先切换模型到openai或api2d)": {
+ # "Group": "对话",
+ # "Color": "stop",
+ # "AsButton": True,
+ # "AdvancedArgs": False, # 调用时,唤起高级参数输入区(默认False)
+ # # "Info": "使用DALLE2修改图片 | 输入参数字符串,提供图像的内容",
+ # "Function": HotReload(图片修改_DALLE2)
+ # },
+ # })
except:
print(trimmed_format_exc())
print('Load function plugin failed')
diff --git a/crazy_functions/图片生成.py b/crazy_functions/图片生成.py
index 642a9e22..5c56b79d 100644
--- a/crazy_functions/图片生成.py
+++ b/crazy_functions/图片生成.py
@@ -54,19 +54,23 @@ def edit_image(llm_kwargs, prompt, image_path, resolution="1024x1024", model="da
img_endpoint = chat_endpoint.replace('chat/completions','images/edits')
# # Generate the image
url = img_endpoint
+ n = 1
headers = {
'Authorization': f"Bearer {api_key}",
- 'Content-Type': 'application/json'
}
- data = {
- 'image': open(image_path, 'rb'),
- 'prompt': prompt,
- 'n': 1,
- 'size': resolution,
- 'model': model,
- 'response_format': 'url'
- }
- response = requests.post(url, headers=headers, json=data, proxies=proxies)
+ make_transparent(image_path, image_path+'.transparent.png')
+ image_path = image_path+'.transparent.png'
+ with open(image_path, 'rb') as f:
+ file_content = f.read()
+ files = {
+ 'image': (os.path.basename(image_path), file_content),
+ # 'mask': ('mask.png', open('mask.png', 'rb'))
+ 'prompt': (None, prompt),
+ "n": (None, str(n)),
+ 'size': (None, resolution),
+ }
+
+ response = requests.post(url, headers=headers, files=files, proxies=proxies)
print(response.content)
try:
image_url = json.loads(response.content.decode('utf8'))['data'][0]['url']
@@ -187,14 +191,14 @@ def 图片修改_DALLE2(prompt, llm_kwargs, plugin_kwargs, chatbot, history, sys
yield from update_ui(chatbot=chatbot, history=history)
return
- image_path = state.req[0]
- resolution = state.req[1]
- prompt = state.req[2]
+ image_path = state.req[0]['value']
+ resolution = state.req[1]['value']
+ prompt = state.req[2]['value']
chatbot.append(["图片修改, 执行中", f"图片:`{image_path}`
分辨率:`{resolution}`
修改需求:`{prompt}`"])
yield from update_ui(chatbot=chatbot, history=history)
image_url, image_path = edit_image(llm_kwargs, prompt, image_path, resolution)
- chatbot.append([state.prompt,
+ chatbot.append([prompt,
f'图像中转网址:
`{image_url}`
'+
f'中转网址预览:

'
f'本地文件地址:
`{image_path}`
'+
@@ -202,3 +206,19 @@ def 图片修改_DALLE2(prompt, llm_kwargs, plugin_kwargs, chatbot, history, sys
])
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 界面更新
+def make_transparent(input_image_path, output_image_path):
+ from PIL import Image
+ image = Image.open(input_image_path)
+ image = image.convert("RGBA")
+
+ data = image.getdata()
+
+ new_data = []
+ for item in data:
+ if item[0] == 255 and item[1] == 255 and item[2] == 255:
+ new_data.append((255, 255, 255, 0))
+ else:
+ new_data.append(item)
+
+ image.putdata(new_data)
+ image.save(output_image_path, "PNG")
From 2d2e02040d7d91d2f2a4c34f4d0bf677873b5f4d Mon Sep 17 00:00:00 2001
From: qingxu fu <505030475@qq.com>
Date: Sun, 26 Nov 2023 01:08:34 +0800
Subject: [PATCH 2/2] =?UTF-8?q?DALLE2=E4=BF=AE=E6=94=B9=E5=9B=BE=E5=83=8F?=
=?UTF-8?q?=E6=8F=92=E4=BB=B6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
crazy_functional.py | 20 +++---
.../multi_stage/multi_stage_utils.py | 6 +-
crazy_functions/图片生成.py | 64 +++++++++++++++----
3 files changed, 61 insertions(+), 29 deletions(-)
diff --git a/crazy_functional.py b/crazy_functional.py
index b3dc5baf..d2e75750 100644
--- a/crazy_functional.py
+++ b/crazy_functional.py
@@ -377,16 +377,16 @@ def get_crazy_functions():
"Function": HotReload(图片生成_DALLE3)
},
})
- # function_plugins.update({
- # "图片修改_DALLE2 (先切换模型到openai或api2d)": {
- # "Group": "对话",
- # "Color": "stop",
- # "AsButton": True,
- # "AdvancedArgs": False, # 调用时,唤起高级参数输入区(默认False)
- # # "Info": "使用DALLE2修改图片 | 输入参数字符串,提供图像的内容",
- # "Function": HotReload(图片修改_DALLE2)
- # },
- # })
+ function_plugins.update({
+ "图片修改_DALLE2 (先切换模型到openai或api2d)": {
+ "Group": "对话",
+ "Color": "stop",
+ "AsButton": False,
+ "AdvancedArgs": False, # 调用时,唤起高级参数输入区(默认False)
+ # "Info": "使用DALLE2修改图片 | 输入参数字符串,提供图像的内容",
+ "Function": HotReload(图片修改_DALLE2)
+ },
+ })
except:
print(trimmed_format_exc())
print('Load function plugin failed')
diff --git a/crazy_functions/multi_stage/multi_stage_utils.py b/crazy_functions/multi_stage/multi_stage_utils.py
index 60f07783..f85d35aa 100644
--- a/crazy_functions/multi_stage/multi_stage_utils.py
+++ b/crazy_functions/multi_stage/multi_stage_utils.py
@@ -21,11 +21,7 @@ class GptAcademicState():
def reset(self):
pass
- def lock_plugin(self, chatbot):
- chatbot._cookies['plugin_state'] = pickle.dumps(self)
-
- def unlock_plugin(self, chatbot):
- self.reset()
+ def dump_state(self, chatbot):
chatbot._cookies['plugin_state'] = pickle.dumps(self)
def set_state(self, chatbot, key, value):
diff --git a/crazy_functions/图片生成.py b/crazy_functions/图片生成.py
index 5c56b79d..134eb07a 100644
--- a/crazy_functions/图片生成.py
+++ b/crazy_functions/图片生成.py
@@ -58,8 +58,10 @@ def edit_image(llm_kwargs, prompt, image_path, resolution="1024x1024", model="da
headers = {
'Authorization': f"Bearer {api_key}",
}
- make_transparent(image_path, image_path+'.transparent.png')
- image_path = image_path+'.transparent.png'
+ make_transparent(image_path, image_path+'.tsp.png')
+ make_square_image(image_path+'.tsp.png', image_path+'.tspsq.png')
+ resize_image(image_path+'.tspsq.png', image_path+'.ready.png', max_size=1024)
+ image_path = image_path+'.ready.png'
with open(image_path, 'rb') as f:
file_content = f.read()
files = {
@@ -134,6 +136,7 @@ def 图片生成_DALLE3(prompt, llm_kwargs, plugin_kwargs, chatbot, history, sys
])
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 界面更新
+
class ImageEditState(GptAcademicState):
# 尚未完成
def get_image_file(self, x):
@@ -146,18 +149,27 @@ class ImageEditState(GptAcademicState):
file = None if not confirm else file_manifest[0]
return confirm, file
+ def lock_plugin(self, chatbot):
+ chatbot._cookies['lock_plugin'] = 'crazy_functions.图片生成->图片修改_DALLE2'
+ self.dump_state(chatbot)
+
+ def unlock_plugin(self, chatbot):
+ self.reset()
+ chatbot._cookies['lock_plugin'] = None
+ self.dump_state(chatbot)
+
def get_resolution(self, x):
return (x in ['256x256', '512x512', '1024x1024']), x
-
+
def get_prompt(self, x):
confirm = (len(x)>=5) and (not self.get_resolution(x)[0]) and (not self.get_image_file(x)[0])
return confirm, x
-
+
def reset(self):
self.req = [
- {'value':None, 'description': '请先上传图像(必须是.png格式), 然后再次点击本插件', 'verify_fn': self.get_image_file},
- {'value':None, 'description': '请输入分辨率,可选:256x256, 512x512 或 1024x1024', 'verify_fn': self.get_resolution},
- {'value':None, 'description': '请输入修改需求,建议您使用英文提示词', 'verify_fn': self.get_prompt},
+ {'value':None, 'description': '请先上传图像(必须是.png格式), 然后再次点击本插件', 'verify_fn': self.get_image_file},
+ {'value':None, 'description': '请输入分辨率,可选:256x256, 512x512 或 1024x1024, 然后再次点击本插件', 'verify_fn': self.get_resolution},
+ {'value':None, 'description': '请输入修改需求,建议您使用英文提示词, 然后再次点击本插件', 'verify_fn': self.get_prompt},
]
self.info = ""
@@ -167,7 +179,7 @@ class ImageEditState(GptAcademicState):
confirm, res = r['verify_fn'](prompt)
if confirm:
r['value'] = res
- self.set_state(chatbot, 'dummy_key', 'dummy_value')
+ self.dump_state(chatbot)
break
return self
@@ -186,8 +198,9 @@ def 图片修改_DALLE2(prompt, llm_kwargs, plugin_kwargs, chatbot, history, sys
history = [] # 清空历史
state = ImageEditState.get_state(chatbot, ImageEditState)
state = state.feed(prompt, chatbot)
+ state.lock_plugin(chatbot)
if not state.already_obtained_all_materials():
- chatbot.append(["图片修改(先上传图片,再输入修改需求,最后输入分辨率)", state.next_req()])
+ chatbot.append(["图片修改\n\n1. 上传图片(图片中需要修改的位置用橡皮擦擦除为纯白色,即RGB=255,255,255)\n2. 输入分辨率 \n3. 输入修改需求", state.next_req()])
yield from update_ui(chatbot=chatbot, history=history)
return
@@ -196,29 +209,52 @@ def 图片修改_DALLE2(prompt, llm_kwargs, plugin_kwargs, chatbot, history, sys
prompt = state.req[2]['value']
chatbot.append(["图片修改, 执行中", f"图片:`{image_path}`
分辨率:`{resolution}`
修改需求:`{prompt}`"])
yield from update_ui(chatbot=chatbot, history=history)
-
image_url, image_path = edit_image(llm_kwargs, prompt, image_path, resolution)
- chatbot.append([prompt,
+ chatbot.append([prompt,
f'图像中转网址:
`{image_url}`
'+
f'中转网址预览:

'
f'本地文件地址:
`{image_path}`
'+
f'本地文件预览:

'
])
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 界面更新
+ state.unlock_plugin(chatbot)
def make_transparent(input_image_path, output_image_path):
from PIL import Image
image = Image.open(input_image_path)
image = image.convert("RGBA")
-
data = image.getdata()
-
new_data = []
for item in data:
if item[0] == 255 and item[1] == 255 and item[2] == 255:
new_data.append((255, 255, 255, 0))
else:
new_data.append(item)
-
image.putdata(new_data)
image.save(output_image_path, "PNG")
+
+def resize_image(input_path, output_path, max_size=1024):
+ from PIL import Image
+ with Image.open(input_path) as img:
+ width, height = img.size
+ if width > max_size or height > max_size:
+ if width >= height:
+ new_width = max_size
+ new_height = int((max_size / width) * height)
+ else:
+ new_height = max_size
+ new_width = int((max_size / height) * width)
+
+ resized_img = img.resize(size=(new_width, new_height))
+ resized_img.save(output_path)
+ else:
+ img.save(output_path)
+
+def make_square_image(input_path, output_path):
+ from PIL import Image
+ with Image.open(input_path) as img:
+ width, height = img.size
+ size = max(width, height)
+ new_img = Image.new("RGBA", (size, size), color="black")
+ new_img.paste(img, ((size - width) // 2, (size - height) // 2))
+ new_img.save(output_path)
\ No newline at end of file