From 09c10c0db052deb582a40f92bbd5ccacfb0420a9 Mon Sep 17 00:00:00 2001 From: liuwei Date: Wed, 27 Aug 2025 09:55:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E6=8E=89AIlogo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/ai_gen_image/main.py | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/plugins/ai_gen_image/main.py b/plugins/ai_gen_image/main.py index 1d1f01b..95763a5 100644 --- a/plugins/ai_gen_image/main.py +++ b/plugins/ai_gen_image/main.py @@ -71,18 +71,20 @@ class AIGenImagePlugin(MessagePluginInterface): self._commands = self._config.get("AIGenImage", {}).get("command", ["AI绘图", "绘图", "画图", "生成图片"]) self.command_format = self._config.get("AIGenImage", {}).get("command-format", "AI绘图 描述文字") self.enable = self._config.get("AIGenImage", {}).get("enable", True) - + # API配置 - self.image_api_url = self._config.get("AIGenImage", {}).get("image_api_url", "https://image.pollinations.ai/prompt/{prompt}") + self.image_api_url = self._config.get("AIGenImage", {}).get("image_api_url", + "https://image.pollinations.ai/prompt/{prompt}") self.default_width = self._config.get("AIGenImage", {}).get("default_width", 1024) self.default_height = self._config.get("AIGenImage", {}).get("default_height", 1024) self.default_model = self._config.get("AIGenImage", {}).get("default_model", "turbo") self.default_timeout = self._config.get("AIGenImage", {}).get("default_timeout", 300) - + # 确保临时目录存在 - self.temp_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))), 'temp') + self.temp_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))), + 'temp') os.makedirs(self.temp_dir, exist_ok=True) - + self.LOG.info(f"[{self.name}] 插件初始化完成,指令:{self._commands}") return True @@ -136,7 +138,7 @@ class AIGenImagePlugin(MessagePluginInterface): try: # 发送提示消息 await bot.send_text_message((roomid if roomid else sender), f"🎨正在生成图片,请稍候...", sender) - + # 生成图片 image_path = self._generate_image(prompt) if not image_path or not os.path.exists(image_path): @@ -160,29 +162,30 @@ class AIGenImagePlugin(MessagePluginInterface): "width": self.default_width, "height": self.default_height, "model": self.default_model, - "seed": int(time.time()) % 1000000 # 使用时间戳作为随机种子 + "seed": int(time.time()) % 1000000, # 使用时间戳作为随机种子 + "nologo": "true" # Optional, set to "true" for registered referrers/tokens } - + # 编码提示词 encoded_prompt = urllib.parse.quote(prompt) url = self.image_api_url.format(prompt=encoded_prompt) - + self.LOG.info(f"正在生成图片,提示词: {prompt[:30]}...") - + # 发送请求 response = requests.get(url, params=params, timeout=self.default_timeout) response.raise_for_status() - + # 保存图片 image_filename = f"ai_image_{uuid.uuid4().hex[:8]}.jpg" image_path = os.path.join(self.temp_dir, image_filename) - + with open(image_path, 'wb') as f: f.write(response.content) - + self.LOG.info(f"图片生成成功,保存至: {image_path}") return image_path - + except Exception as e: self.LOG.error(f"生成图片出错: {e}") - return "" \ No newline at end of file + return ""