From e7f4c804eb5bf6a08a0b91eda74c4896cc8f0ab9 Mon Sep 17 00:00:00 2001 From: Skyzayre <120616113+Skyzayre@users.noreply.github.com> Date: Fri, 1 Dec 2023 10:27:25 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E5=88=86=E7=B1=BB=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将原有分类 “对话” 更名为 “对话&作图” --- config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.py b/config.py index f170a2b..f1d2728 100644 --- a/config.py +++ b/config.py @@ -82,7 +82,7 @@ MAX_RETRY = 2 # 插件分类默认选项 -DEFAULT_FN_GROUPS = ['对话', '编程', '学术', '智能体'] +DEFAULT_FN_GROUPS = ['对话&作图', '编程', '学术', '智能体'] # 模型选择是 (注意: LLM_MODEL是默认选中的模型, 它*必须*被包含在AVAIL_LLM_MODELS列表中 ) From e8dd3c02f2f22d72cadce87b86c9cbab73e8f488 Mon Sep 17 00:00:00 2001 From: Skyzayre <120616113+Skyzayre@users.noreply.github.com> Date: Fri, 1 Dec 2023 10:30:25 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E5=AF=B9=E5=BA=94=E7=9A=84=E5=88=86=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crazy_functional.py | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/crazy_functional.py b/crazy_functional.py index 3b8b945..8e786e6 100644 --- a/crazy_functional.py +++ b/crazy_functional.py @@ -40,7 +40,7 @@ def get_crazy_functions(): function_plugins = { "虚空终端": { - "Group": "对话|编程|学术|智能体", + "Group": "对话&作图|编程|学术|智能体", "Color": "stop", "AsButton": True, "Function": HotReload(虚空终端) @@ -53,20 +53,20 @@ def get_crazy_functions(): "Function": HotReload(解析一个Python项目) }, "载入对话历史存档(先上传存档或输入路径)": { - "Group": "对话", + "Group": "对话&作图", "Color": "stop", "AsButton": False, "Info": "载入对话历史存档 | 输入参数为路径", "Function": HotReload(载入对话历史存档) }, "删除所有本地对话历史记录(谨慎操作)": { - "Group": "对话", + "Group": "对话&作图", "AsButton": False, "Info": "删除所有本地对话历史记录,谨慎操作 | 不需要输入参数", "Function": HotReload(删除所有本地对话历史记录) }, "清除所有缓存文件(谨慎操作)": { - "Group": "对话", + "Group": "对话&作图", "Color": "stop", "AsButton": False, # 加入下拉菜单中 "Info": "清除所有缓存文件,谨慎操作 | 不需要输入参数", @@ -180,19 +180,19 @@ def get_crazy_functions(): "Function": HotReload(批量生成函数注释) }, "保存当前的对话": { - "Group": "对话", + "Group": "对话&作图", "AsButton": True, "Info": "保存当前的对话 | 不需要输入参数", "Function": HotReload(对话历史存档) }, "[多线程Demo]解析此项目本身(源码自译解)": { - "Group": "对话|编程", + "Group": "对话&作图|编程", "AsButton": False, # 加入下拉菜单中 "Info": "多线程解析并翻译此项目的源码 | 不需要输入参数", "Function": HotReload(解析项目本身) }, "历史上的今天": { - "Group": "对话", + "Group": "对话&作图", "AsButton": True, "Info": "查看历史上的今天事件 (这是一个面向开发者的插件Demo) | 不需要输入参数", "Function": HotReload(高阶功能模板函数) @@ -205,7 +205,7 @@ def get_crazy_functions(): "Function": HotReload(批量翻译PDF文档) }, "询问多个GPT模型": { - "Group": "对话", + "Group": "对话&作图", "Color": "stop", "AsButton": True, "Function": HotReload(同时问询) @@ -300,7 +300,7 @@ def get_crazy_functions(): from crazy_functions.联网的ChatGPT import 连接网络回答问题 function_plugins.update({ "连接网络回答问题(输入问题后点击该插件,需要访问谷歌)": { - "Group": "对话", + "Group": "对话&作图", "Color": "stop", "AsButton": False, # 加入下拉菜单中 # "Info": "连接网络回答问题(需要访问谷歌)| 输入参数是一个问题", @@ -310,7 +310,7 @@ def get_crazy_functions(): from crazy_functions.联网的ChatGPT_bing版 import 连接bing搜索回答问题 function_plugins.update({ "连接网络回答问题(中文Bing版,输入问题后点击该插件)": { - "Group": "对话", + "Group": "对话&作图", "Color": "stop", "AsButton": False, # 加入下拉菜单中 "Info": "连接网络回答问题(需要访问中文Bing)| 输入参数是一个问题", @@ -341,7 +341,7 @@ def get_crazy_functions(): from crazy_functions.询问多个大语言模型 import 同时问询_指定模型 function_plugins.update({ "询问多个GPT模型(手动指定询问哪些模型)": { - "Group": "对话", + "Group": "对话&作图", "Color": "stop", "AsButton": False, "AdvancedArgs": True, # 调用时,唤起高级参数输入区(默认False) @@ -357,7 +357,7 @@ def get_crazy_functions(): from crazy_functions.图片生成 import 图片生成_DALLE2, 图片生成_DALLE3 function_plugins.update({ "图片生成_DALLE2 (先切换模型到openai或api2d)": { - "Group": "对话", + "Group": "对话&作图", "Color": "stop", "AsButton": False, "AdvancedArgs": True, # 调用时,唤起高级参数输入区(默认False) @@ -368,7 +368,7 @@ def get_crazy_functions(): }) function_plugins.update({ "图片生成_DALLE3 (先切换模型到openai或api2d)": { - "Group": "对话", + "Group": "对话&作图", "Color": "stop", "AsButton": False, "AdvancedArgs": True, # 调用时,唤起高级参数输入区(默认False) @@ -385,7 +385,7 @@ def get_crazy_functions(): from crazy_functions.总结音视频 import 总结音视频 function_plugins.update({ "批量总结音视频(输入路径或上传压缩包)": { - "Group": "对话", + "Group": "对话&作图", "Color": "stop", "AsButton": False, "AdvancedArgs": True, @@ -402,7 +402,7 @@ def get_crazy_functions(): from crazy_functions.数学动画生成manim import 动画生成 function_plugins.update({ "数学动画生成(Manim)": { - "Group": "对话", + "Group": "对话&作图", "Color": "stop", "AsButton": False, "Info": "按照自然语言描述生成一个动画 | 输入参数是一段话", @@ -433,7 +433,7 @@ def get_crazy_functions(): from crazy_functions.Langchain知识库 import 知识库问答 function_plugins.update({ "构建知识库(先上传文件素材,再运行此插件)": { - "Group": "对话", + "Group": "对话&作图", "Color": "stop", "AsButton": False, "AdvancedArgs": True, @@ -449,7 +449,7 @@ def get_crazy_functions(): from crazy_functions.Langchain知识库 import 读取知识库作答 function_plugins.update({ "知识库问答(构建知识库后,再运行此插件)": { - "Group": "对话", + "Group": "对话&作图", "Color": "stop", "AsButton": False, "AdvancedArgs": True, @@ -465,7 +465,7 @@ def get_crazy_functions(): from crazy_functions.交互功能函数模板 import 交互功能模板函数 function_plugins.update({ "交互功能模板Demo函数(查找wallhaven.cc的壁纸)": { - "Group": "对话", + "Group": "对话&作图", "Color": "stop", "AsButton": False, "Function": HotReload(交互功能模板函数) @@ -527,7 +527,7 @@ def get_crazy_functions(): from crazy_functions.语音助手 import 语音助手 function_plugins.update({ "实时语音对话": { - "Group": "对话", + "Group": "对话&作图", "Color": "stop", "AsButton": True, "Info": "这是一个时刻聆听着的语音对话助手 | 没有输入参数", From ef12d4f754bd955431063d14963f25709c174f20 Mon Sep 17 00:00:00 2001 From: Skyzayre <120616113+Skyzayre@users.noreply.github.com> Date: Fri, 1 Dec 2023 10:31:50 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9dalle3=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E8=BE=93=E5=85=A5=E5=8C=BA=E6=8F=90=E7=A4=BA=E8=AF=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crazy_functional.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crazy_functional.py b/crazy_functional.py index 8e786e6..dcf7f6b 100644 --- a/crazy_functional.py +++ b/crazy_functional.py @@ -372,7 +372,7 @@ def get_crazy_functions(): "Color": "stop", "AsButton": False, "AdvancedArgs": True, # 调用时,唤起高级参数输入区(默认False) - "ArgsReminder": "在这里输入分辨率, 如1024x1024(默认),支持 1024x1024, 1792x1024, 1024x1792。如需生成高清图像,请输入 1024x1024-HD, 1792x1024-HD, 1024x1792-HD。", # 高级参数输入区的显示提示 + "ArgsReminder": "在这里输入自定义参数“分辨率-质量(可选)-风格(可选)”, 参数示例“1024x1024-hd-vivid” || 分辨率支持 1024x1024(默认)//1792x1024//1024x1792 || 质量支持 -standard(默认)//-hd || 风格支持 -vivid(默认)//-natural", # 高级参数输入区的显示提示 "Info": "使用DALLE3生成图片 | 输入参数字符串,提供图像的内容", "Function": HotReload(图片生成_DALLE3) }, From 6126024f2c94e6e56accd013736d4c0427e9596e Mon Sep 17 00:00:00 2001 From: Skyzayre <120616113+Skyzayre@users.noreply.github.com> Date: Fri, 1 Dec 2023 10:36:59 +0800 Subject: [PATCH 4/8] =?UTF-8?q?dall-e-3=E6=B7=BB=E5=8A=A0=20'style'=20?= =?UTF-8?q?=E9=A3=8E=E6=A0=BC=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit dall-e-3添加 'style' 风格参数(参考 platform.openai.com/doc/api-reference),修改dall-e-3作图时的参数判断逻辑 --- crazy_functions/图片生成.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/crazy_functions/图片生成.py b/crazy_functions/图片生成.py index 642a9e2..104d403 100644 --- a/crazy_functions/图片生成.py +++ b/crazy_functions/图片生成.py @@ -2,7 +2,7 @@ from toolbox import CatchException, update_ui, get_conf, select_api_key, get_log from crazy_functions.multi_stage.multi_stage_utils import GptAcademicState -def gen_image(llm_kwargs, prompt, resolution="1024x1024", model="dall-e-2", quality=None): +def gen_image(llm_kwargs, prompt, resolution="1024x1024", model="dall-e-2", quality=None, style=None): import requests, json, time, os from request_llms.bridge_all import model_info @@ -25,7 +25,10 @@ def gen_image(llm_kwargs, prompt, resolution="1024x1024", model="dall-e-2", qual 'model': model, 'response_format': 'url' } - if quality is not None: data.update({'quality': quality}) + if quality is not None: + data['quality'] = quality + if style is not None: + data['style'] = style response = requests.post(url, headers=headers, json=data, proxies=proxies) print(response.content) try: @@ -115,13 +118,18 @@ def 图片生成_DALLE3(prompt, llm_kwargs, plugin_kwargs, chatbot, history, sys chatbot.append(("您正在调用“图像生成”插件。", "[Local Message] 生成图像, 请先把模型切换至gpt-*或者api2d-*。如果中文Prompt效果不理想, 请尝试英文Prompt。正在处理中 .....")) yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 由于请求gpt需要一段时间,我们先及时地做一次界面更新 if ("advanced_arg" in plugin_kwargs) and (plugin_kwargs["advanced_arg"] == ""): plugin_kwargs.pop("advanced_arg") - resolution = plugin_kwargs.get("advanced_arg", '1024x1024').lower() - if resolution.endswith('-hd'): - resolution = resolution.replace('-hd', '') - quality = 'hd' - else: - quality = 'standard' - image_url, image_path = gen_image(llm_kwargs, prompt, resolution, model="dall-e-3", quality=quality) + resolution_arg = plugin_kwargs.get("advanced_arg", '1024x1024-standard-vivid').lower() + parts = resolution_arg.split('-') + resolution = parts[0] # 解析分辨率 + quality = 'standard' # 质量与风格默认值 + style = 'vivid' + # 遍历检查是否有额外参数 + for part in parts[1:]: + if part in ['hd', 'standard']: + quality = part + elif part in ['vivid', 'natural']: + style = part + image_url, image_path = gen_image(llm_kwargs, prompt, resolution, model="dall-e-3", quality=quality, style=style) chatbot.append([prompt, f'图像中转网址:
`{image_url}`
'+ f'中转网址预览:
' @@ -201,4 +209,3 @@ def 图片修改_DALLE2(prompt, llm_kwargs, plugin_kwargs, chatbot, history, sys f'本地文件预览:
' ]) yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 界面更新 - From 1134723c80cb9a68bd60a84e28b666f8cbe8be3e Mon Sep 17 00:00:00 2001 From: Skyzayre <120616113+Skyzayre@users.noreply.github.com> Date: Fri, 1 Dec 2023 10:40:11 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9docs=E4=B8=AD=E6=8F=92?= =?UTF-8?q?=E4=BB=B6=E5=88=86=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/translate_english.json | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/docs/translate_english.json b/docs/translate_english.json index 955dcaf..400ec97 100644 --- a/docs/translate_english.json +++ b/docs/translate_english.json @@ -2183,9 +2183,8 @@ "找不到合适插件执行该任务": "Cannot find a suitable plugin to perform this task", "接驳VoidTerminal": "Connect to VoidTerminal", "**很好": "**Very good", - "对话|编程": "Conversation|Programming", - "对话|编程|学术": "Conversation|Programming|Academic", - "4. 建议使用 GPT3.5 或更强的模型": "4. It is recommended to use GPT3.5 or a stronger model", + "对话&作图|编程": "Conversation&ImageGenerating|Programming", + "对话&作图|编程|学术": "Conversation&ImageGenerating|Programming|Academic", "4. 建议使用 GPT3.5 或更强的模型": "4. It is recommended to use GPT3.5 or a stronger model", "「请调用插件翻译PDF论文": "Please call the plugin to translate the PDF paper", "3. 如果您使用「调用插件xxx」、「修改配置xxx」、「请问」等关键词": "3. If you use keywords such as 'call plugin xxx', 'modify configuration xxx', 'please', etc.", "以下是一篇学术论文的基本信息": "The following is the basic information of an academic paper", @@ -2630,7 +2629,7 @@ "已经被记忆": "Already memorized", "默认用英文的": "Default to English", "错误追踪": "Error tracking", - "对话|编程|学术|智能体": "Dialogue|Programming|Academic|Intelligent agent", + "对话&编程|编程|学术|智能体": "Conversation&ImageGenerating|Programming|Academic|Intelligent agent", "请检查": "Please check", "检测到被滞留的缓存文档": "Detected cached documents being left behind", "还有哪些场合允许使用代理": "What other occasions allow the use of proxies", @@ -2904,4 +2903,4 @@ "请配置ZHIPUAI_API_KEY": "Please configure ZHIPUAI_API_KEY", "单个azure模型": "Single Azure model", "预留参数 context 未实现": "Reserved parameter 'context' not implemented" -} \ No newline at end of file +} From 2aab6cb708c6f82de3bd181bab0232da7eb3ed9c Mon Sep 17 00:00:00 2001 From: Skyzayre <120616113+Skyzayre@users.noreply.github.com> Date: Fri, 1 Dec 2023 10:50:20 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=83=A8=E5=88=86?= =?UTF-8?q?=E7=BF=BB=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/translate_english.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/translate_english.json b/docs/translate_english.json index 400ec97..bf09f66 100644 --- a/docs/translate_english.json +++ b/docs/translate_english.json @@ -923,7 +923,7 @@ "的第": "The", "个片段": "fragment", "总结文章": "Summarize the article", - "根据以上的对话": "According to the above dialogue", + "根据以上的对话": "According to the conversation above", "的主要内容": "The main content of", "所有文件都总结完成了吗": "Are all files summarized?", "如果是.doc文件": "If it is a .doc file", @@ -1501,7 +1501,7 @@ "发送请求到OpenAI后": "After sending the request to OpenAI", "上下布局": "Vertical Layout", "左右布局": "Horizontal Layout", - "对话窗的高度": "Height of the Dialogue Window", + "对话窗的高度": "Height of the Conversation Window", "重试的次数限制": "Retry Limit", "gpt4现在只对申请成功的人开放": "GPT-4 is now only open to those who have successfully applied", "提高限制请查询": "Please check for higher limits", From d99b443b4cae5d7599b7060ae488ce14ab6d0a11 Mon Sep 17 00:00:00 2001 From: Skyzayre <120616113+Skyzayre@users.noreply.github.com> Date: Fri, 1 Dec 2023 10:51:04 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=83=A8=E5=88=86?= =?UTF-8?q?=E7=BF=BB=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/translate_traditionalchinese.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/translate_traditionalchinese.json b/docs/translate_traditionalchinese.json index 9ca7cba..4edc65d 100644 --- a/docs/translate_traditionalchinese.json +++ b/docs/translate_traditionalchinese.json @@ -1043,9 +1043,9 @@ "jittorllms响应异常": "jittorllms response exception", "在项目根目录运行这两个指令": "Run these two commands in the project root directory", "获取tokenizer": "Get tokenizer", - "chatbot 为WebUI中显示的对话列表": "chatbot is the list of dialogues displayed in WebUI", + "chatbot 为WebUI中显示的对话列表": "chatbot is the list of conversations displayed in WebUI", "test_解析一个Cpp项目": "test_parse a Cpp project", - "将对话记录history以Markdown格式写入文件中": "Write the dialogue record history to a file in Markdown format", + "将对话记录history以Markdown格式写入文件中": "Write the conversations record history to a file in Markdown format", "装饰器函数": "Decorator function", "玫瑰色": "Rose color", "将单空行": "刪除單行空白", @@ -2270,4 +2270,4 @@ "标注节点的行数范围": "標註節點的行數範圍", "默认 True": "默認 True", "将两个PDF拼接": "將兩個PDF拼接" -} \ No newline at end of file +} From 95504f0bb75a2835e168b033d5c945d58170a451 Mon Sep 17 00:00:00 2001 From: Skyzayre <120616113+Skyzayre@users.noreply.github.com> Date: Mon, 4 Dec 2023 10:31:12 +0800 Subject: [PATCH 8/8] Resolve conflicts --- crazy_functions/图片生成.py | 74 +++++++++++++++++++++++++++++++------ 1 file changed, 62 insertions(+), 12 deletions(-) diff --git a/crazy_functions/图片生成.py b/crazy_functions/图片生成.py index 104d403..d5c4eb0 100644 --- a/crazy_functions/图片生成.py +++ b/crazy_functions/图片生成.py @@ -150,18 +150,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 = "" @@ -171,7 +180,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 @@ -190,22 +199,63 @@ 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 - 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}`
'+ 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)