| | |
| | | import datetime |
| | | import random |
| | | import os |
| | | from io import BytesIO |
| | | |
| | | from auto_media_publisher.config.conf_base import ( |
| | | TRANSLATE_API_TEMPLATE, |
| | | POLLINATIONS_API_TEMPLATE, |
| | |
| | | |
| | | if not filename: |
| | | now = datetime.datetime.now() |
| | | filename = now.strftime("%Y%m%d%H%M%S") + ".jpg" |
| | | if not filename.lower().endswith(".jpg"): |
| | | filename += ".jpg" |
| | | filename = now.strftime("%Y%m%d%H%M%S") + ".png" |
| | | if not filename.lower().endswith(".png"): |
| | | filename += ".png" |
| | | |
| | | with open(filename, "wb") as f: |
| | | f.write(content) |
| | |
| | | abs_path = os.path.abspath(filename) |
| | | print(f"[✓] 图片已保存:{abs_path}") |
| | | return abs_path |
| | | |
| | | def generate_image_to_stream(self, prompt: str) -> BytesIO: |
| | | if not prompt.strip(): |
| | | raise ValueError("提示词不能为空") |
| | | |
| | | translated_prompt = self.translate_prompt(prompt) |
| | | url = self.build_url(translated_prompt) |
| | | |
| | | content = self._fetch_with_retry(url) |
| | | if content is None: |
| | | return None |
| | | |
| | | image_stream = BytesIO(content) |
| | | return image_stream |